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.

144 lines
4.1 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. vfsStream::setup();
  27. $root = vfsStream::create(array());
  28. vfsStreamWrapper::setRoot($root);
  29. $views_dir = vfsStream::newDirectory('views');
  30. $this->template = new vfsStreamFile('test.phtml');
  31. $this->template->setContent('<?php echo $a ." " . $b . " " . $c; ?>');
  32. $views_dir->addChild($this->template);
  33. $root->addChild($views_dir);
  34. $this->view = new PHPView(array('path' => vfsStream::url('root/views/')));
  35. }
  36. public function testPHPViewConstructor()
  37. {
  38. $this->assertInstanceOf('PHPView', $this->view);
  39. $this->assertSame('vfs://root/views/', $this->view->getPath());
  40. }
  41. public function testPHPViewNullConstructor()
  42. {
  43. $this->setExpectedException('InitializationException', 'Configuration must have a "path" set.');
  44. $view = new PHPView(null);
  45. }
  46. public function testAssign()
  47. {
  48. $this->view->assign('a', 'c');
  49. $this->view->append('b', 'b');
  50. $this->view->prepend('c', 'a');
  51. $this->assertStringStartsWith('c b a', $this->view->fetch('test'));
  52. }
  53. public function testAssignObject()
  54. {
  55. $obj = $this->getMock('NewClass');
  56. $obj->a = 'one';
  57. $obj->b = 'two';
  58. $obj->c = 'three';
  59. $this->view->assignObject($obj);
  60. $this->assertSame('one two three', $this->view->fetch('test'));
  61. }
  62. public function testEscape()
  63. {
  64. $result = $this->view->escape('"<>"');
  65. $this->assertSame('&quot;&lt;&gt;&quot;', $result);
  66. }
  67. public function testCall()
  68. {
  69. $this->view->title('New title');
  70. $this->assertContains('New title', Registry::get('TitleViewHelper'));
  71. }
  72. public function testIllegalCall()
  73. {
  74. $this->setExpectedException('GeneralException', 'View helper "WriteViewHelper" not found.');
  75. $this->view->write('tony');
  76. }
  77. /**
  78. * @TODO: PHPView: check views path for ending slash in getTemplatePath()
  79. */
  80. public function testFetch()
  81. {
  82. $this->assertNotSame('test phtml view ', $this->template->getContent());
  83. $this->view->assign('a', 'some');
  84. $this->view->assign('b', 'some');
  85. $this->view->assign('c', 'some');
  86. $result = $this->view->fetch('test');
  87. $this->assertNotEmpty($result);
  88. $this->assertSame('some some some', $result);
  89. }
  90. public function testAppend()
  91. {
  92. $this->view->assign('a', 'some');
  93. $this->view->append('b', 'some');
  94. $this->view->assign('c', 'some');
  95. $this->view->append('c', 'end');
  96. $result = $this->view->fetch('test');
  97. $this->assertSame('some some someend', $result);
  98. }
  99. public function testPrepend()
  100. {
  101. $this->view->assign('a', 'some');
  102. $this->view->prepend('b', 'some');
  103. $this->view->assign('c', 'some');
  104. $this->view->prepend('c', 'start');
  105. $result = $this->view->fetch('test');
  106. $this->assertSame('some some startsome', $result);
  107. }
  108. public function testErrorTemplate()
  109. {
  110. $view = new PHPView(array('path' => 'error_path/'));
  111. $this->setExpectedException('GeneralException', 'Template');
  112. $result = $view->fetch('test');
  113. }
  114. }