PartialLoopTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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-2009 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_PartialLoopTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_PartialLoopTest::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_PartialLoop */
  30. require_once 'Zend/View/Helper/PartialLoop.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_PartialLoop.
  37. *
  38. * @category Zend
  39. * @package Zend_View
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2009 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_PartialLoopTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * @var Zend_View_Helper_PartialLoop
  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_PartialLoopTest");
  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_PartialLoop();
  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 testPartialLoopIteratesOverArray()
  93. {
  94. $data = array(
  95. array('message' => 'foo'),
  96. array('message' => 'bar'),
  97. array('message' => 'baz'),
  98. array('message' => 'bat')
  99. );
  100. $view = new Zend_View(array(
  101. 'scriptPath' => $this->basePath . '/default/views/scripts'
  102. ));
  103. $this->helper->setView($view);
  104. $result = $this->helper->partialLoop('partialLoop.phtml', $data);
  105. foreach ($data as $item) {
  106. $string = 'This is an iteration: ' . $item['message'];
  107. $this->assertContains($string, $result);
  108. }
  109. }
  110. /**
  111. * @return void
  112. */
  113. public function testPartialLoopIteratesOverIterator()
  114. {
  115. $data = array(
  116. array('message' => 'foo'),
  117. array('message' => 'bar'),
  118. array('message' => 'baz'),
  119. array('message' => 'bat')
  120. );
  121. $o = new Zend_View_Helper_PartialLoop_IteratorTest($data);
  122. $view = new Zend_View(array(
  123. 'scriptPath' => $this->basePath . '/default/views/scripts'
  124. ));
  125. $this->helper->setView($view);
  126. $result = $this->helper->partialLoop('partialLoop.phtml', $o);
  127. foreach ($data as $item) {
  128. $string = 'This is an iteration: ' . $item['message'];
  129. $this->assertContains($string, $result);
  130. }
  131. }
  132. /**
  133. * @return void
  134. */
  135. public function testPartialLoopIteratesOverRecursiveIterator()
  136. {
  137. $rIterator = new Zend_View_Helper_PartialLoop_RecursiveIteratorTest();
  138. for ($i = 0; $i < 5; ++$i) {
  139. $data = array(
  140. 'message' => 'foo' . $i,
  141. );
  142. $rIterator->addItem(new Zend_View_Helper_PartialLoop_IteratorTest($data));
  143. }
  144. $view = new Zend_View(array(
  145. 'scriptPath' => $this->basePath . '/default/views/scripts'
  146. ));
  147. $this->helper->setView($view);
  148. $result = $this->helper->partialLoop('partialLoop.phtml', $rIterator);
  149. foreach ($rIterator as $item) {
  150. foreach ($item as $key => $value) {
  151. $this->assertContains($value, $result, var_export($value, 1));
  152. }
  153. }
  154. }
  155. /**
  156. * @return void
  157. */
  158. public function testPartialLoopThrowsExceptionWithBadIterator()
  159. {
  160. $data = array(
  161. array('message' => 'foo'),
  162. array('message' => 'bar'),
  163. array('message' => 'baz'),
  164. array('message' => 'bat')
  165. );
  166. $o = new Zend_View_Helper_PartialLoop_BogusIteratorTest($data);
  167. $view = new Zend_View(array(
  168. 'scriptPath' => $this->basePath . '/default/views/scripts'
  169. ));
  170. $this->helper->setView($view);
  171. try {
  172. $result = $this->helper->partialLoop('partialLoop.phtml', $o);
  173. $this->fail('PartialLoop should only work with arrays and iterators');
  174. } catch (Exception $e) {
  175. }
  176. }
  177. /**
  178. * @return void
  179. */
  180. public function testPartialLoopFindsModule()
  181. {
  182. Zend_Controller_Front::getInstance()->addModuleDirectory($this->basePath);
  183. $data = array(
  184. array('message' => 'foo'),
  185. array('message' => 'bar'),
  186. array('message' => 'baz'),
  187. array('message' => 'bat')
  188. );
  189. $view = new Zend_View(array(
  190. 'scriptPath' => $this->basePath . '/default/views/scripts'
  191. ));
  192. $this->helper->setView($view);
  193. $result = $this->helper->partialLoop('partialLoop.phtml', 'foo', $data);
  194. foreach ($data as $item) {
  195. $string = 'This is an iteration in the foo module: ' . $item['message'];
  196. $this->assertContains($string, $result);
  197. }
  198. }
  199. public function testPassingNoArgsReturnsHelperInstance()
  200. {
  201. $test = $this->helper->partialLoop();
  202. $this->assertSame($this->helper, $test);
  203. }
  204. public function testShouldAllowIteratingOverTraversableObjects()
  205. {
  206. $data = array(
  207. array('message' => 'foo'),
  208. array('message' => 'bar'),
  209. array('message' => 'baz'),
  210. array('message' => 'bat')
  211. );
  212. $o = new ArrayObject($data);
  213. $view = new Zend_View(array(
  214. 'scriptPath' => $this->basePath . '/default/views/scripts'
  215. ));
  216. $this->helper->setView($view);
  217. $result = $this->helper->partialLoop('partialLoop.phtml', $o);
  218. foreach ($data as $item) {
  219. $string = 'This is an iteration: ' . $item['message'];
  220. $this->assertContains($string, $result);
  221. }
  222. }
  223. public function testShouldAllowIteratingOverObjectsImplementingToArray()
  224. {
  225. $data = array(
  226. array('message' => 'foo'),
  227. array('message' => 'bar'),
  228. array('message' => 'baz'),
  229. array('message' => 'bat')
  230. );
  231. $o = new Zend_View_Helper_PartialLoop_ToArrayTest($data);
  232. $view = new Zend_View(array(
  233. 'scriptPath' => $this->basePath . '/default/views/scripts'
  234. ));
  235. $this->helper->setView($view);
  236. $result = $this->helper->partialLoop('partialLoop.phtml', $o);
  237. foreach ($data as $item) {
  238. $string = 'This is an iteration: ' . $item['message'];
  239. $this->assertContains($string, $result, $result);
  240. }
  241. }
  242. /**
  243. * @see ZF-3350
  244. * @see ZF-3352
  245. */
  246. public function testShouldNotCastToArrayIfObjectIsTraversable()
  247. {
  248. $data = array(
  249. new Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer(array('message' => 'foo')),
  250. new Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer(array('message' => 'bar')),
  251. new Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer(array('message' => 'baz')),
  252. new Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer(array('message' => 'bat')),
  253. );
  254. $o = new Zend_View_Helper_PartialLoop_IteratorWithToArrayTest($data);
  255. $view = new Zend_View(array(
  256. 'scriptPath' => $this->basePath . '/default/views/scripts'
  257. ));
  258. $this->helper->setView($view);
  259. $this->helper->setObjectKey('obj');
  260. $result = $this->helper->partialLoop('partialLoopObject.phtml', $o);
  261. foreach ($data as $item) {
  262. $string = 'This is an iteration: ' . $item->message;
  263. $this->assertContains($string, $result, $result);
  264. }
  265. }
  266. /**
  267. * @see ZF-3083
  268. */
  269. public function testEmptyArrayPassedToPartialLoopShouldNotThrowException()
  270. {
  271. $view = new Zend_View(array(
  272. 'scriptPath' => $this->basePath . '/default/views/scripts'
  273. ));
  274. $this->helper->setView($view);
  275. try {
  276. $result = $this->helper->partialLoop('partialLoop.phtml', array());
  277. } catch (Exception $e) {
  278. $this->fail('Empty array should not cause partialLoop to throw exception');
  279. }
  280. try {
  281. $result = $this->helper->partialLoop('partialLoop.phtml', null, array());
  282. } catch (Exception $e) {
  283. $this->fail('Empty array should not cause partialLoop to throw exception');
  284. }
  285. }
  286. /**
  287. * @see ZF-2737
  288. * @link http://framework.zend.com/issues/browse/ZF-2737
  289. */
  290. public function testPartialLoopIncramentsPartialCounter()
  291. {
  292. $data = array(
  293. array('message' => 'foo'),
  294. array('message' => 'bar'),
  295. array('message' => 'baz'),
  296. array('message' => 'bat')
  297. );
  298. $view = new Zend_View(array(
  299. 'scriptPath' => $this->basePath . '/default/views/scripts'
  300. ));
  301. $this->helper->setView($view);
  302. $result = $this->helper->partialLoop('partialLoopCouter.phtml', $data);
  303. foreach ($data as $key=>$item) {
  304. $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1);
  305. $this->assertContains($string, $result);
  306. }
  307. }
  308. /**
  309. * @see ZF-5174
  310. * @link http://framework.zend.com/issues/browse/ZF-5174
  311. */
  312. public function testPartialLoopPartialCounterResets()
  313. {
  314. $data = array(
  315. array('message' => 'foo'),
  316. array('message' => 'bar'),
  317. array('message' => 'baz'),
  318. array('message' => 'bat')
  319. );
  320. $view = new Zend_View(array(
  321. 'scriptPath' => $this->basePath . '/default/views/scripts'
  322. ));
  323. $this->helper->setView($view);
  324. $result = $this->helper->partialLoop('partialLoopCouter.phtml', $data);
  325. foreach ($data as $key=>$item) {
  326. $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1);
  327. $this->assertContains($string, $result);
  328. }
  329. $result = $this->helper->partialLoop('partialLoopCouter.phtml', $data);
  330. foreach ($data as $key=>$item) {
  331. $string = 'This is an iteration: ' . $item['message'] . ', pointer at ' . ($key+1);
  332. $this->assertContains($string, $result);
  333. }
  334. }
  335. }
  336. class Zend_View_Helper_PartialLoop_IteratorTest implements Iterator
  337. {
  338. public $items;
  339. public function __construct(array $array)
  340. {
  341. $this->items = $array;
  342. }
  343. public function current()
  344. {
  345. return current($this->items);
  346. }
  347. public function key()
  348. {
  349. return key($this->items);
  350. }
  351. public function next()
  352. {
  353. return next($this->items);
  354. }
  355. public function rewind()
  356. {
  357. return reset($this->items);
  358. }
  359. public function valid()
  360. {
  361. return (current($this->items) !== false);
  362. }
  363. public function toArray()
  364. {
  365. return $this->items;
  366. }
  367. }
  368. class Zend_View_Helper_PartialLoop_RecursiveIteratorTest implements Iterator
  369. {
  370. public $items;
  371. public function __construct()
  372. {
  373. $this->items = array();
  374. }
  375. public function addItem(Iterator $iterator)
  376. {
  377. $this->items[] = $iterator;
  378. return $this;
  379. }
  380. public function current()
  381. {
  382. return current($this->items);
  383. }
  384. public function key()
  385. {
  386. return key($this->items);
  387. }
  388. public function next()
  389. {
  390. return next($this->items);
  391. }
  392. public function rewind()
  393. {
  394. return reset($this->items);
  395. }
  396. public function valid()
  397. {
  398. return (current($this->items) !== false);
  399. }
  400. }
  401. class Zend_View_Helper_PartialLoop_BogusIteratorTest
  402. {
  403. }
  404. class Zend_View_Helper_PartialLoop_ToArrayTest
  405. {
  406. public function __construct(array $data)
  407. {
  408. $this->data = $data;
  409. }
  410. public function toArray()
  411. {
  412. return $this->data;
  413. }
  414. }
  415. class Zend_View_Helper_PartialLoop_IteratorWithToArrayTest implements Iterator
  416. {
  417. public $items;
  418. public function __construct(array $array)
  419. {
  420. $this->items = $array;
  421. }
  422. public function toArray()
  423. {
  424. return $this->items;
  425. }
  426. public function current()
  427. {
  428. return current($this->items);
  429. }
  430. public function key()
  431. {
  432. return key($this->items);
  433. }
  434. public function next()
  435. {
  436. return next($this->items);
  437. }
  438. public function rewind()
  439. {
  440. return reset($this->items);
  441. }
  442. public function valid()
  443. {
  444. return (current($this->items) !== false);
  445. }
  446. }
  447. class Zend_View_Helper_PartialLoop_IteratorWithToArrayTestContainer
  448. {
  449. protected $_info;
  450. public function __construct(array $info)
  451. {
  452. foreach ($info as $key => $value) {
  453. $this->$key = $value;
  454. }
  455. $this->_info = $info;
  456. }
  457. public function toArray()
  458. {
  459. return $this->_info;
  460. }
  461. }
  462. // Call Zend_View_Helper_PartialLoopTest::main() if this source file is executed directly.
  463. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_PartialLoopTest::main") {
  464. Zend_View_Helper_PartialLoopTest::main();
  465. }