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.

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