PartialTest.php 8.5 KB

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