PartialLoopTest.php 15 KB

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