PartialLoopTest.php 14 KB

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