You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.2 KiB

  1. <?php
  2. /*
  3. * @copyright NetMonsters <team@netmonsters.ru>
  4. * @link http://netmonsters.ru
  5. * @package Majestic
  6. * @subpackage UnitTests
  7. * @since 2011-10-11
  8. *
  9. * Unit tests for PHPView class
  10. */
  11. require_once dirname(__FILE__) . '/../../view/iView.php';
  12. require_once dirname(__FILE__) . '/../../Registry.php';
  13. require_once dirname(__FILE__) . '/../../view/helpers/ViewHelper.php';
  14. require_once dirname(__FILE__) . '/../../view/helpers/TitleViewHelper.php';
  15. require_once dirname(__FILE__) . '/../../view/PHPView.php';
  16. require_once dirname(__FILE__) . '/../../exception/GeneralException.php';
  17. require_once dirname(__FILE__) . '/../../exception/InitializationException.php';
  18. require_once 'vfsStream/vfsStream.php';
  19. class PHPViewTest extends PHPUnit_Framework_TestCase
  20. {
  21. private $view;
  22. private $template;
  23. public function setUp()
  24. {
  25. vfsStreamWrapper::register();
  26. $root = vfsStream::create(array());
  27. vfsStreamWrapper::setRoot($root);
  28. $views_dir = vfsStream::newDirectory('views');
  29. $this->template = new vfsStreamFile('test.phtml');
  30. $this->template->setContent('<?php echo $a ." " . $b . " " . $c; ?>');
  31. $views_dir->addChild($this->template);
  32. $root->addChild($views_dir);
  33. $this->view = new PHPView(array('path' => vfsStream::url('root/views/')));
  34. }
  35. public function testPHPViewConstructor()
  36. {
  37. $this->assertInstanceOf('PHPView', $this->view);
  38. $this->assertEquals('vfs://root/views/', $this->view->getPath());
  39. }
  40. /**
  41. * @expectedException InitializationException
  42. * @expectedExceptionMessage Configuration must have a "path" set.
  43. */
  44. public function testPHPViewNullConstructor()
  45. {
  46. $view = new PHPView(null);
  47. }
  48. public function testAssign()
  49. {
  50. $this->view->assign('a', 'c');
  51. $this->view->append('b', 'b');
  52. $this->view->prepend('c', 'a');
  53. $this->assertStringStartsWith('c b a', $this->view->fetch('test'));
  54. }
  55. public function testAssignObject()
  56. {
  57. $obj = $this->getMock('NewClass');
  58. $obj->a = 'one';
  59. $obj->b = 'two';
  60. $obj->c = 'three';
  61. $this->view->assignObject($obj);
  62. $this->assertSame('one two three', $this->view->fetch('test'));
  63. }
  64. public function testEscape()
  65. {
  66. $result = $this->view->escape('"<>"');
  67. $this->assertEquals('&quot;&lt;&gt;&quot;', $result);
  68. }
  69. public function testCall()
  70. {
  71. $this->view->title('New title');
  72. $this->assertContains('New title', Registry::get('TitleViewHelper'));
  73. }
  74. /**
  75. * @expectedException GeneralException
  76. * @expectedExceptionMessage View helper "WriteViewHelper" not found.
  77. */
  78. public function testIllegalCall()
  79. {
  80. $this->view->write('tony');
  81. }
  82. /**
  83. * @TODO: PHPView: check views path for ending slash in getTemplatePath()
  84. */
  85. public function testFetch()
  86. {
  87. $this->assertNotSame('test phtml view ', $this->template->getContent());
  88. $this->view->assign('a', 'some');
  89. $this->view->assign('b', 'some');
  90. $this->view->assign('c', 'some');
  91. $result = $this->view->fetch('test');
  92. $this->assertNotEmpty($result);
  93. $this->assertSame('some some some', $result);
  94. }
  95. public function testAppend()
  96. {
  97. $this->view->assign('a', 'some');
  98. $this->view->append('b', 'some');
  99. $this->view->assign('c', 'some');
  100. $this->view->append('c', 'end');
  101. $result = $this->view->fetch('test');
  102. $this->assertSame('some some someend', $result);
  103. }
  104. public function testPrepend()
  105. {
  106. $this->view->assign('a', 'some');
  107. $this->view->prepend('b', 'some');
  108. $this->view->assign('c', 'some');
  109. $this->view->prepend('c', 'start');
  110. $result = $this->view->fetch('test');
  111. $this->assertSame('some some startsome', $result);
  112. }
  113. /**
  114. * @expectedException GeneralException
  115. * @expectedExceptionMessage Template
  116. */
  117. public function testErrorTemplate()
  118. {
  119. $view = new PHPView(array('path' => 'error_path/'));
  120. $result = $view->fetch('test');
  121. }
  122. }