PartialTest.php 8.8 KB

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