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.

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