PartialTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. // Call Zend_View_Helper_PartialTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_PartialTest::main");
  5. }
  6. require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/TestHelper.php';
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. /** Zend_View_Helper_Partial */
  10. require_once 'Zend/View/Helper/Partial.php';
  11. /** Zend_View */
  12. require_once 'Zend/View.php';
  13. /** Zend_Controller_Front */
  14. require_once 'Zend/Controller/Front.php';
  15. /**
  16. * Test class for Zend_View_Helper_Partial.
  17. *
  18. * @category Zend
  19. * @package Zend_View
  20. * @subpackage UnitTests
  21. */
  22. class Zend_View_Helper_PartialTest extends PHPUnit_Framework_TestCase
  23. {
  24. /**
  25. * @var Zend_View_Helper_Partial
  26. */
  27. public $helper;
  28. /**
  29. * @var string
  30. */
  31. public $basePath;
  32. /**
  33. * Runs the test methods of this class.
  34. *
  35. * @return void
  36. */
  37. public static function main()
  38. {
  39. require_once "PHPUnit/TextUI/TestRunner.php";
  40. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_PartialTest");
  41. $result = PHPUnit_TextUI_TestRunner::run($suite);
  42. }
  43. /**
  44. * Sets up the fixture, for example, open a network connection.
  45. * This method is called before a test is executed.
  46. *
  47. * @return void
  48. */
  49. public function setUp()
  50. {
  51. $this->basePath = dirname(__FILE__) . '/_files/modules';
  52. $this->helper = new Zend_View_Helper_Partial();
  53. Zend_Controller_Front::getInstance()->resetInstance();
  54. }
  55. /**
  56. * Tears down the fixture, for example, close a network connection.
  57. * This method is called after a test is executed.
  58. *
  59. * @return void
  60. */
  61. public function tearDown()
  62. {
  63. unset($this->helper);
  64. }
  65. /**
  66. * @return void
  67. */
  68. public function testPartialRendersScript()
  69. {
  70. $view = new Zend_View(array(
  71. 'scriptPath' => $this->basePath . '/default/views/scripts'
  72. ));
  73. $this->helper->setView($view);
  74. $return = $this->helper->partial('partialOne.phtml');
  75. $this->assertContains('This is the first test partial', $return);
  76. }
  77. /**
  78. * @return void
  79. */
  80. public function testPartialRendersScriptWithVars()
  81. {
  82. $view = new Zend_View(array(
  83. 'scriptPath' => $this->basePath . '/default/views/scripts'
  84. ));
  85. $view->message = 'This should never be read';
  86. $this->helper->setView($view);
  87. $return = $this->helper->partial('partialThree.phtml', array('message' => 'This message should be read'));
  88. $this->assertNotContains($view->message, $return);
  89. $this->assertContains('This message should be read', $return, $return);
  90. }
  91. /**
  92. * @return void
  93. */
  94. public function testPartialRendersScriptInDifferentModuleWhenRequested()
  95. {
  96. Zend_Controller_Front::getInstance()->addModuleDirectory($this->basePath);
  97. $view = new Zend_View(array(
  98. 'scriptPath' => $this->basePath . '/default/views/scripts'
  99. ));
  100. $this->helper->setView($view);
  101. $return = $this->helper->partial('partialTwo.phtml', 'foo');
  102. $this->assertContains('This is the second partial', $return, $return);
  103. }
  104. /**
  105. * @return void
  106. */
  107. public function testPartialThrowsExceptionWithInvalidModule()
  108. {
  109. Zend_Controller_Front::getInstance()->addModuleDirectory($this->basePath);
  110. $view = new Zend_View(array(
  111. 'scriptPath' => $this->basePath . '/default/views/scripts'
  112. ));
  113. $this->helper->setView($view);
  114. try {
  115. $return = $this->helper->partial('partialTwo.phtml', 'barbazbat');
  116. $this->fail('Partial should throw exception if module does not exist');
  117. } catch (Exception $e) {
  118. }
  119. }
  120. /**
  121. * @return void
  122. */
  123. public function testSetViewSetsViewProperty()
  124. {
  125. $view = new Zend_View();
  126. $this->helper->setView($view);
  127. $this->assertSame($view, $this->helper->view);
  128. }
  129. /**
  130. * @return void
  131. */
  132. public function testCloneViewReturnsDifferentViewInstance()
  133. {
  134. $view = new Zend_View();
  135. $this->helper->setView($view);
  136. $clone = $this->helper->cloneView();
  137. $this->assertNotSame($view, $clone);
  138. $this->assertTrue($clone instanceof Zend_View);
  139. }
  140. /**
  141. * @return void
  142. */
  143. public function testCloneViewClearsViewVariables()
  144. {
  145. $view = new Zend_View();
  146. $view->foo = 'bar';
  147. $this->helper->setView($view);
  148. $clone = $this->helper->cloneView();
  149. $clonedVars = $clone->getVars();
  150. $this->assertTrue(empty($clonedVars));
  151. $this->assertNull($clone->foo);
  152. }
  153. public function testObjectModelWithPublicPropertiesSetsViewVariables()
  154. {
  155. $model = new stdClass();
  156. $model->foo = 'bar';
  157. $model->bar = 'baz';
  158. $view = new Zend_View(array(
  159. 'scriptPath' => $this->basePath . '/default/views/scripts'
  160. ));
  161. $this->helper->setView($view);
  162. $return = $this->helper->partial('partialVars.phtml', $model);
  163. foreach (get_object_vars($model) as $key => $value) {
  164. $string = sprintf('%s: %s', $key, $value);
  165. $this->assertContains($string, $return);
  166. }
  167. }
  168. public function testObjectModelWithToArraySetsViewVariables()
  169. {
  170. $model = new Zend_View_Helper_PartialTest_Aggregate();
  171. $view = new Zend_View(array(
  172. 'scriptPath' => $this->basePath . '/default/views/scripts'
  173. ));
  174. $this->helper->setView($view);
  175. $return = $this->helper->partial('partialVars.phtml', $model);
  176. foreach ($model->toArray() as $key => $value) {
  177. $string = sprintf('%s: %s', $key, $value);
  178. $this->assertContains($string, $return);
  179. }
  180. }
  181. public function testObjectModelSetInObjectKeyWhenKeyPresent()
  182. {
  183. $this->helper->setObjectKey('foo');
  184. $model = new stdClass();
  185. $model->footest = 'bar';
  186. $model->bartest = 'baz';
  187. $view = new Zend_View(array(
  188. 'scriptPath' => $this->basePath . '/default/views/scripts'
  189. ));
  190. $this->helper->setView($view);
  191. $return = $this->helper->partial('partialObj.phtml', $model);
  192. $this->assertNotContains('No object model passed', $return);
  193. foreach (get_object_vars($model) as $key => $value) {
  194. $string = sprintf('%s: %s', $key, $value);
  195. $this->assertContains($string, $return);
  196. }
  197. }
  198. public function testPassingNoArgsReturnsHelperInstance()
  199. {
  200. $test = $this->helper->partial();
  201. $this->assertSame($this->helper, $test);
  202. }
  203. public function testObjectKeyIsNullByDefault()
  204. {
  205. $this->assertNull($this->helper->getObjectKey());
  206. }
  207. public function testCanSetObjectKey()
  208. {
  209. $this->testObjectKeyIsNullByDefault();
  210. $this->helper->setObjectKey('foo');
  211. $this->assertEquals('foo', $this->helper->getObjectKey());
  212. }
  213. public function testCanSetObjectKeyToNullValue()
  214. {
  215. $this->testCanSetObjectKey();
  216. $this->helper->setObjectKey(null);
  217. $this->assertNull($this->helper->getObjectKey());
  218. }
  219. public function testSetObjectKeyImplementsFluentInterface()
  220. {
  221. $test = $this->helper->setObjectKey('foo');
  222. $this->assertSame($this->helper, $test);
  223. }
  224. }
  225. class Zend_View_Helper_PartialTest_Aggregate
  226. {
  227. public $vars = array(
  228. 'foo' => 'bar',
  229. 'bar' => 'baz'
  230. );
  231. public function toArray()
  232. {
  233. return $this->vars;
  234. }
  235. }
  236. // Call Zend_View_Helper_PartialTest::main() if this source file is executed directly.
  237. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_PartialTest::main") {
  238. Zend_View_Helper_PartialTest::main();
  239. }