ContainerTest.php 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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_Navigation
  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. require_once 'Zend/Navigation.php';
  23. require_once 'Zend/Config.php';
  24. /**
  25. * Tests the class Zend_Navigation_Container
  26. *
  27. * @category Zend
  28. * @package Zend_Navigation
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Navigation
  33. */
  34. class Zend_Navigation_ContainerTest extends PHPUnit_Framework_TestCase
  35. {
  36. /**
  37. * Prepares the environment before running a test.
  38. *
  39. */
  40. protected function setUp()
  41. {
  42. }
  43. /**
  44. * Tear down the environment after running a test
  45. *
  46. */
  47. protected function tearDown()
  48. {
  49. }
  50. public function testConstructWithArray()
  51. {
  52. $argument = array(
  53. array(
  54. 'label' => 'Page 1',
  55. 'uri' => 'page1.html'
  56. ),
  57. array(
  58. 'label' => 'Page 2',
  59. 'uri' => 'page2.html'
  60. ),
  61. array(
  62. 'label' => 'Page 3',
  63. 'uri' => 'page3.html'
  64. )
  65. );
  66. $container = new Zend_Navigation($argument);
  67. $this->assertEquals(3, $container->count());
  68. }
  69. public function testConstructWithConfig()
  70. {
  71. $argument = new Zend_Config(array(
  72. array(
  73. 'label' => 'Page 1',
  74. 'uri' => 'page1.html'
  75. ),
  76. array(
  77. 'label' => 'Page 2',
  78. 'uri' => 'page2.html'
  79. ),
  80. array(
  81. 'label' => 'Page 3',
  82. 'uri' => 'page3.html'
  83. )
  84. ));
  85. $container = new Zend_Navigation($argument);
  86. $this->assertEquals(3, $container->count());
  87. }
  88. public function testConstructorShouldThrowExceptionOnInvalidArgument()
  89. {
  90. try {
  91. $nav = new Zend_Navigation('ok');
  92. $this->fail('An invalid argument was given to the constructor, ' .
  93. 'but a Zend_Navigation_Exception was not thrown');
  94. } catch (Zend_Navigation_Exception $e) {
  95. $this->assertContains('Invalid argument: $pages', $e->getMessage());
  96. }
  97. try {
  98. $nav = new Zend_Navigation(1337);
  99. $this->fail('An invalid argument was given to the constructor, ' .
  100. 'but a Zend_Navigation_Exception was not thrown');
  101. } catch (Zend_Navigation_Exception $e) {
  102. $this->assertContains('Invalid argument: $pages', $e->getMessage());
  103. }
  104. try {
  105. $nav = new Zend_Navigation(new stdClass());
  106. $this->fail('An invalid argument was given to the constructor, ' .
  107. 'but a Zend_Navigation_Exception was not thrown');
  108. } catch (Zend_Navigation_Exception $e) {
  109. $this->assertContains('Invalid argument: $pages', $e->getMessage());
  110. }
  111. }
  112. public function testIterationShouldBeOrderAware()
  113. {
  114. $nav = new Zend_Navigation(array(
  115. array(
  116. 'label' => 'Page 1',
  117. 'uri' => '#'
  118. ),
  119. array(
  120. 'label' => 'Page 2',
  121. 'uri' => '#',
  122. 'order' => -1
  123. ),
  124. array(
  125. 'label' => 'Page 3',
  126. 'uri' => '#'
  127. ),
  128. array(
  129. 'label' => 'Page 4',
  130. 'uri' => '#',
  131. 'order' => 100
  132. ),
  133. array(
  134. 'label' => 'Page 5',
  135. 'uri' => '#'
  136. )
  137. ));
  138. $expected = array('Page 2', 'Page 1', 'Page 3', 'Page 5', 'Page 4');
  139. $actual = array();
  140. foreach ($nav as $page) {
  141. $actual[] = $page->getLabel();
  142. }
  143. $this->assertEquals($expected, $actual);
  144. }
  145. public function testRecursiveIteration()
  146. {
  147. $nav = new Zend_Navigation(array(
  148. array(
  149. 'label' => 'Page 1',
  150. 'uri' => '#',
  151. 'pages' => array(
  152. array(
  153. 'label' => 'Page 1.1',
  154. 'uri' => '#',
  155. 'pages' => array(
  156. array(
  157. 'label' => 'Page 1.1.1',
  158. 'uri' => '#'
  159. ),
  160. array(
  161. 'label' => 'Page 1.1.2',
  162. 'uri' => '#'
  163. )
  164. )
  165. ),
  166. array(
  167. 'label' => 'Page 1.2',
  168. 'uri' => '#'
  169. )
  170. )
  171. ),
  172. array(
  173. 'label' => 'Page 2',
  174. 'uri' => '#',
  175. 'pages' => array(
  176. array(
  177. 'label' => 'Page 2.1',
  178. 'uri' => '#'
  179. )
  180. )
  181. ),
  182. array(
  183. 'label' => 'Page 3',
  184. 'uri' => '#'
  185. )
  186. ));
  187. $actual = array();
  188. $expected = array(
  189. 'Page 1',
  190. 'Page 1.1',
  191. 'Page 1.1.1',
  192. 'Page 1.1.2',
  193. 'Page 1.2',
  194. 'Page 2',
  195. 'Page 2.1',
  196. 'Page 3'
  197. );
  198. $iterator = new RecursiveIteratorIterator($nav,
  199. RecursiveIteratorIterator::SELF_FIRST);
  200. foreach ($iterator as $page) {
  201. $actual[] = $page->getLabel();
  202. }
  203. $this->assertEquals($expected, $actual);
  204. }
  205. public function testSettingPageOrderShouldUpdateContainerOrder()
  206. {
  207. $nav = new Zend_Navigation(array(
  208. array(
  209. 'label' => 'Page 1',
  210. 'uri' => '#'
  211. ),
  212. array(
  213. 'label' => 'Page 2',
  214. 'uri' => '#'
  215. )
  216. ));
  217. $page3 = Zend_Navigation_Page::factory(array(
  218. 'label' => 'Page 3',
  219. 'uri' => '#'
  220. ));
  221. $nav->addPage($page3);
  222. $expected = array(
  223. 'before' => array('Page 1', 'Page 2', 'Page 3'),
  224. 'after' => array('Page 3', 'Page 1', 'Page 2')
  225. );
  226. $actual = array(
  227. 'before' => array(),
  228. 'after' => array()
  229. );
  230. foreach ($nav as $page) {
  231. $actual['before'][] = $page->getLabel();
  232. }
  233. $page3->setOrder(-1);
  234. foreach ($nav as $page) {
  235. $actual['after'][] = $page->getLabel();
  236. }
  237. $this->assertEquals($expected, $actual);
  238. }
  239. public function testAddPageShouldWorkWithArray()
  240. {
  241. $pageOptions = array(
  242. 'label' => 'From array',
  243. 'uri' => '#array'
  244. );
  245. $nav = new Zend_Navigation();
  246. $nav->addPage($pageOptions);
  247. $this->assertEquals(1, count($nav));
  248. }
  249. public function testAddPageShouldWorkWithConfig()
  250. {
  251. $pageOptions = array(
  252. 'label' => 'From config',
  253. 'uri' => '#config'
  254. );
  255. $pageOptions = new Zend_Config($pageOptions);
  256. $nav = new Zend_Navigation();
  257. $nav->addPage($pageOptions);
  258. $this->assertEquals(1, count($nav));
  259. }
  260. public function testAddPageShouldWorkWithPageInstance()
  261. {
  262. $pageOptions = array(
  263. 'label' => 'From array 1',
  264. 'uri' => '#array'
  265. );
  266. $nav = new Zend_Navigation(array($pageOptions));
  267. $page = Zend_Navigation_Page::factory($pageOptions);
  268. $nav->addPage($page);
  269. $this->assertEquals(2, count($nav));
  270. }
  271. public function testAddPagesShouldWorkWithArray()
  272. {
  273. $nav = new Zend_Navigation();
  274. $nav->addPages(array(
  275. array(
  276. 'label' => 'Page 1',
  277. 'uri' => '#'
  278. ),
  279. array(
  280. 'label' => 'Page 2',
  281. 'action' => 'index',
  282. 'controller' => 'index'
  283. )
  284. ));
  285. $this->assertEquals(2, count($nav),
  286. 'Expected 2 pages, found ' . count($nav));
  287. }
  288. public function testAddPagesShouldWorkWithConfig()
  289. {
  290. $nav = new Zend_Navigation();
  291. $nav->addPages(new Zend_Config(array(
  292. array(
  293. 'label' => 'Page 1',
  294. 'uri' => '#'
  295. ),
  296. array(
  297. 'label' => 'Page 2',
  298. 'action' => 'index',
  299. 'controller' => 'index'
  300. )
  301. )));
  302. $this->assertEquals(2, count($nav),
  303. 'Expected 2 pages, found ' . count($nav));
  304. }
  305. public function testAddPagesShouldWorkWithMixedArray()
  306. {
  307. $nav = new Zend_Navigation();
  308. $nav->addPages(new Zend_Config(array(
  309. array(
  310. 'label' => 'Page 1',
  311. 'uri' => '#'
  312. ),
  313. new Zend_Config(array(
  314. 'label' => 'Page 2',
  315. 'action' => 'index',
  316. 'controller' => 'index'
  317. )),
  318. Zend_Navigation_Page::factory(array(
  319. 'label' => 'Page 3',
  320. 'uri' => '#'
  321. ))
  322. )));
  323. $this->assertEquals(3, count($nav),
  324. 'Expected 3 pages, found ' . count($nav));
  325. }
  326. public function testAddPagesShouldThrowExceptionWhenGivenString()
  327. {
  328. $nav = new Zend_Navigation();
  329. try {
  330. $nav->addPages('this is a string');
  331. $this->fail('An invalid argument was given to addPages(), ' .
  332. 'but a Zend_Navigation_Exception was not thrown');
  333. } catch (Zend_Navigation_Exception $e) {
  334. $this->assertContains('Invalid argument: $pages must be', $e->getMessage());
  335. }
  336. }
  337. public function testAddPagesShouldThrowExceptionWhenGivenAnArbitraryObject()
  338. {
  339. $nav = new Zend_Navigation();
  340. try {
  341. $nav->addPages($pages = new stdClass());
  342. $this->fail('An invalid argument was given to addPages(), ' .
  343. 'but a Zend_Navigation_Exception was not thrown');
  344. } catch (Zend_Navigation_Exception $e) {
  345. $this->assertContains('Invalid argument: $pages must be', $e->getMessage());
  346. }
  347. }
  348. public function testRemovingAllPages()
  349. {
  350. $nav = new Zend_Navigation();
  351. $nav->addPages(array(
  352. array(
  353. 'label' => 'Page 1',
  354. 'uri' => '#'
  355. ),
  356. array(
  357. 'label' => 'Page 2',
  358. 'uri' => '#'
  359. )
  360. ));
  361. $nav->removePages();
  362. $this->assertEquals(0, count($nav),
  363. 'Expected 0 pages, found ' . count($nav));
  364. }
  365. public function testSettingPages()
  366. {
  367. $nav = new Zend_Navigation();
  368. $nav->addPages(array(
  369. array(
  370. 'label' => 'Page 1',
  371. 'uri' => '#'
  372. ),
  373. array(
  374. 'label' => 'Page 2',
  375. 'uri' => '#'
  376. )
  377. ));
  378. $nav->setPages(array(
  379. array(
  380. 'label' => 'Page 3',
  381. 'uri' => '#'
  382. )
  383. ));
  384. $this->assertEquals(1, count($nav),
  385. 'Expected 1 page, found ' . count($nav));
  386. }
  387. public function testGetPagesShouldReturnAnArrayOfPages()
  388. {
  389. $nav = new Zend_Navigation(array(
  390. array(
  391. 'uri' => 'Page 1'
  392. ),
  393. array(
  394. 'uri' => 'Page 2'
  395. )
  396. ));
  397. $pages = $nav->getPages();
  398. $expected = array(
  399. 'type' => 'array',
  400. 'count' => 2
  401. );
  402. $actual = array(
  403. 'type' => gettype($pages),
  404. 'count' => count($pages)
  405. );
  406. $this->assertEquals($expected, $actual);
  407. $this->assertContainsOnly('Zend_Navigation_Page_Uri', $pages, false);
  408. }
  409. public function testGetPagesShouldReturnUnorderedPages()
  410. {
  411. $nav = new Zend_Navigation(array(
  412. array(
  413. 'label' => 'Page 2',
  414. 'uri' => '#',
  415. 'order' => -1
  416. ),
  417. array(
  418. 'label' => 'Page 4',
  419. 'uri' => '#',
  420. 'order' => 100
  421. ),
  422. array(
  423. 'label' => 'Page 1',
  424. 'uri' => '#'
  425. ),
  426. array(
  427. 'label' => 'Page 5',
  428. 'uri' => '#'
  429. ),
  430. array(
  431. 'label' => 'Page 3',
  432. 'uri' => '#'
  433. )
  434. ));
  435. $expected = array('Page 2', 'Page 4', 'Page 1', 'Page 5', 'Page 3');
  436. $actual = array();
  437. foreach ($nav->getPages() as $page) {
  438. $actual[] = $page->getLabel();
  439. }
  440. $this->assertEquals($expected, $actual);
  441. }
  442. public function testRemovingPageByOrder()
  443. {
  444. $nav = new Zend_Navigation(array(
  445. array(
  446. 'label' => 'Page 1',
  447. 'uri' => '#'
  448. ),
  449. array(
  450. 'label' => 'Page 2',
  451. 'uri' => '#',
  452. 'order' => 32
  453. ),
  454. array(
  455. 'label' => 'Page 3',
  456. 'uri' => '#'
  457. ),
  458. array(
  459. 'label' => 'Page 4',
  460. 'uri' => '#'
  461. )
  462. ));
  463. $expected = array(
  464. 'remove0' => true,
  465. 'remove32' => true,
  466. 'remove0again' => true,
  467. 'remove1000' => false,
  468. 'count' => 1,
  469. 'current' => 'Page 4'
  470. );
  471. $actual = array(
  472. 'remove0' => $nav->removePage(0),
  473. 'remove32' => $nav->removePage(32),
  474. 'remove0again' => $nav->removePage(0),
  475. 'remove1000' => $nav->removePage(1000),
  476. 'count' => $nav->count(),
  477. 'current' => $nav->current()->getLabel()
  478. );
  479. $this->assertEquals($expected, $actual);
  480. }
  481. public function testRemovingPageByInstance()
  482. {
  483. $nav = new Zend_Navigation(array(
  484. array(
  485. 'label' => 'Page 1',
  486. 'uri' => '#'
  487. ),
  488. array(
  489. 'label' => 'Page 2',
  490. 'uri' => '#'
  491. )
  492. ));
  493. $page3 = Zend_Navigation_Page::factory(array(
  494. 'label' => 'Page 3',
  495. 'uri' => '#'
  496. ));
  497. $nav->addPage($page3);
  498. $this->assertEquals(true, $nav->removePage($page3));
  499. }
  500. public function testRemovingPageByInstanceShouldReturnFalseIfPageIsNotInContainer()
  501. {
  502. $nav = new Zend_Navigation(array(
  503. array(
  504. 'label' => 'Page 1',
  505. 'uri' => '#'
  506. ),
  507. array(
  508. 'label' => 'Page 2',
  509. 'uri' => '#'
  510. )
  511. ));
  512. $page = Zend_Navigation_Page::factory(array(
  513. 'label' => 'Page lol',
  514. 'uri' => '#'
  515. ));
  516. $this->assertEquals(false, $nav->removePage($page));
  517. }
  518. public function testHasPage()
  519. {
  520. $page0 = Zend_Navigation_Page::factory(array(
  521. 'label' => 'Page 0',
  522. 'uri' => '#'
  523. ));
  524. $page1 = Zend_Navigation_Page::factory(array(
  525. 'label' => 'Page 1',
  526. 'uri' => '#'
  527. ));
  528. $page1_1 = Zend_Navigation_Page::factory(array(
  529. 'label' => 'Page 1.1',
  530. 'uri' => '#'
  531. ));
  532. $page1_2 = Zend_Navigation_Page::factory(array(
  533. 'label' => 'Page 1.2',
  534. 'uri' => '#'
  535. ));
  536. $page1_2_1 = Zend_Navigation_Page::factory(array(
  537. 'label' => 'Page 1.2.1',
  538. 'uri' => '#'
  539. ));
  540. $page1_3 = Zend_Navigation_Page::factory(array(
  541. 'label' => 'Page 1.3',
  542. 'uri' => '#'
  543. ));
  544. $page2 = Zend_Navigation_Page::factory(array(
  545. 'label' => 'Page 2',
  546. 'uri' => '#'
  547. ));
  548. $page3 = Zend_Navigation_Page::factory(array(
  549. 'label' => 'Page 3',
  550. 'uri' => '#'
  551. ));
  552. $nav = new Zend_Navigation(array($page1, $page2, $page3));
  553. $page1->addPage($page1_1);
  554. $page1->addPage($page1_2);
  555. $page1_2->addPage($page1_2_1);
  556. $page1->addPage($page1_3);
  557. $expected = array(
  558. 'haspage0' => false,
  559. 'haspage2' => true,
  560. 'haspage1_1' => false,
  561. 'haspage1_1recursive' => true
  562. );
  563. $actual = array(
  564. 'haspage0' => $nav->hasPage($page0),
  565. 'haspage2' => $nav->hasPage($page2),
  566. 'haspage1_1' => $nav->hasPage($page1_1),
  567. 'haspage1_1recursive' => $nav->hasPage($page1_1, true)
  568. );
  569. $this->assertEquals($expected, $actual);
  570. }
  571. public function testHasPages()
  572. {
  573. $nav1 = new Zend_Navigation();
  574. $nav2 = new Zend_Navigation();
  575. $nav2->addPage(array(
  576. 'label' => 'Page 1',
  577. 'uri' => '#'
  578. ));
  579. $expected = array(
  580. 'empty' => false,
  581. 'notempty' => true
  582. );
  583. $actual = array(
  584. 'empty' => $nav1->hasPages(),
  585. 'notempty' => $nav2->hasPages()
  586. );
  587. $this->assertEquals($expected, $actual);
  588. }
  589. public function testSetParentShouldWorkWithPage()
  590. {
  591. $page1 = Zend_Navigation_Page::factory(array(
  592. 'label' => 'Page 1',
  593. 'uri' => '#'
  594. ));
  595. $page2 = Zend_Navigation_Page::factory(array(
  596. 'label' => 'Page 2',
  597. 'uri' => '#'
  598. ));
  599. $page2->setParent($page1);
  600. $expected = array(
  601. 'parent' => 'Page 1',
  602. 'hasPages' => true
  603. );
  604. $actual = array(
  605. 'parent' => $page2->getParent()->getLabel(),
  606. 'hasPages' => $page1->hasPages()
  607. );
  608. $this->assertEquals($expected, $actual);
  609. }
  610. public function testSetParentShouldWorkWithNull()
  611. {
  612. $page1 = Zend_Navigation_Page::factory(array(
  613. 'label' => 'Page 1',
  614. 'uri' => '#'
  615. ));
  616. $page2 = Zend_Navigation_Page::factory(array(
  617. 'label' => 'Page 2',
  618. 'uri' => '#'
  619. ));
  620. $page2->setParent($page1);
  621. $page2->setParent(null);
  622. $this->assertEquals(null, $page2->getParent());
  623. }
  624. public function testSetParentShouldRemoveFromOldParentPage()
  625. {
  626. $page1 = Zend_Navigation_Page::factory(array(
  627. 'label' => 'Page 1',
  628. 'uri' => '#'
  629. ));
  630. $page2 = Zend_Navigation_Page::factory(array(
  631. 'label' => 'Page 2',
  632. 'uri' => '#'
  633. ));
  634. $page2->setParent($page1);
  635. $page2->setParent(null);
  636. $expected = array(
  637. 'parent' => null,
  638. 'haspages' => false
  639. );
  640. $actual = array(
  641. 'parent' => $page2->getParent(),
  642. 'haspages' => $page2->hasPages()
  643. );
  644. $this->assertEquals($expected, $actual);
  645. }
  646. public function testFinderMethodsShouldWorkWithCustomProperties()
  647. {
  648. $nav = $this->_getFindByNavigation();
  649. $found = $nav->findOneBy('page2', 'page2');
  650. $this->assertType('Zend_Navigation_Page', $found);
  651. $this->assertEquals('Page 2', $found->getLabel());
  652. }
  653. public function testFindOneByShouldReturnOnlyOnePage()
  654. {
  655. $nav = $this->_getFindByNavigation();
  656. $found = $nav->findOneBy('id', 'page_2_and_3');
  657. $this->assertType('Zend_Navigation_Page', $found);
  658. $this->assertEquals('Page 2', $found->getLabel());
  659. }
  660. public function testFindOneByShouldReturnNullIfNotFound()
  661. {
  662. $nav = $this->_getFindByNavigation();
  663. $found = $nav->findOneBy('id', 'non-existant');
  664. $this->assertNull($found);
  665. }
  666. public function testFindAllByShouldReturnAllMatchingPages()
  667. {
  668. $nav = $this->_getFindByNavigation();
  669. $found = $nav->findAllBy('id', 'page_2_and_3');
  670. $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
  671. $expected = array('Page 2', 'Page 3');
  672. $actual = array();
  673. foreach ($found as $page) {
  674. $actual[] = $page->getLabel();
  675. }
  676. $this->assertEquals($expected, $actual);
  677. }
  678. public function testFindAllByShouldReturnEmptyArrayifNotFound()
  679. {
  680. $nav = $this->_getFindByNavigation();
  681. $found = $nav->findAllBy('id', 'non-existant');
  682. $expected = array('type' => 'array', 'count' => 0);
  683. $actual = array('type' => gettype($found), 'count' => count($found));
  684. $this->assertEquals($expected, $actual);
  685. }
  686. public function testFindByShouldDefaultToFindOneBy()
  687. {
  688. $nav = $this->_getFindByNavigation();
  689. $found = $nav->findBy('id', 'page_2_and_3');
  690. $this->assertType('Zend_Navigation_Page', $found);
  691. }
  692. public function testFindOneByMagicMethodNativeProperty()
  693. {
  694. $nav = $this->_getFindByNavigation();
  695. $found = $nav->findOneById('page_2_and_3');
  696. $this->assertType('Zend_Navigation_Page', $found);
  697. $this->assertEquals('Page 2', $found->getLabel());
  698. }
  699. public function testFindOneByMagicMethodCustomProperty()
  700. {
  701. $nav = $this->_getFindByNavigation();
  702. $found = $nav->findOneBypage2('page2');
  703. $this->assertType('Zend_Navigation_Page', $found);
  704. $this->assertEquals('Page 2', $found->getLabel());
  705. }
  706. public function testFindAllByWithMagicMethodNativeProperty()
  707. {
  708. $nav = $this->_getFindByNavigation();
  709. $found = $nav->findAllById('page_2_and_3');
  710. $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
  711. $expected = array('Page 2', 'Page 3');
  712. $actual = array();
  713. foreach ($found as $page) {
  714. $actual[] = $page->getLabel();
  715. }
  716. $this->assertEquals($expected, $actual);
  717. }
  718. public function testFindAllByMagicUcfirstPropDoesNotFindCustomLowercaseProps()
  719. {
  720. $nav = $this->_getFindByNavigation();
  721. $found = $nav->findAllByAction('about');
  722. $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
  723. $expected = array('Page 3');
  724. $actual = array();
  725. foreach ($found as $page) {
  726. $actual[] = $page->getLabel();
  727. }
  728. $this->assertEquals($expected, $actual);
  729. }
  730. public function testFindAllByMagicLowercaseFindsBothNativeAndCustomProps()
  731. {
  732. $nav = $this->_getFindByNavigation();
  733. $found = $nav->findAllByaction('about');
  734. $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
  735. $expected = array('Page 1.3', 'Page 3');
  736. $actual = array();
  737. foreach ($found as $page) {
  738. $actual[] = $page->getLabel();
  739. }
  740. $this->assertEquals($expected, $actual);
  741. }
  742. public function testFindByMagicMethodIsEquivalentToFindOneBy()
  743. {
  744. $nav = $this->_getFindByNavigation();
  745. $found = $nav->findById('page_2_and_3');
  746. $this->assertType('Zend_Navigation_Page', $found);
  747. $this->assertEquals('Page 2', $found->getLabel());
  748. }
  749. public function testInvalidMagicFinderMethodShouldThrowException()
  750. {
  751. $nav = $this->_getFindByNavigation();
  752. try {
  753. $found = $nav->findSomeById('page_2_and_3');
  754. $this->fail('An invalid magic finder method was used, ' .
  755. 'but a Zend_Navigation_Exception was not thrown');
  756. } catch (Zend_Navigation_Exception $e) {
  757. $this->assertContains('Bad method call', $e->getMessage());
  758. }
  759. }
  760. public function testInvalidMagicMethodShouldThrowException()
  761. {
  762. $nav = $this->_getFindByNavigation();
  763. try {
  764. $found = $nav->getPagez();
  765. $this->fail('An invalid magic finder method was used, ' .
  766. 'but a Zend_Navigation_Exception was not thrown');
  767. } catch (Zend_Navigation_Exception $e) {
  768. $this->assertContains('Bad method call', $e->getMessage());
  769. }
  770. }
  771. protected function _getFindByNavigation()
  772. {
  773. // findAllByFoo('bar') // Page 1, Page 1.1
  774. // findById('page_2_and_3') // Page 2
  775. // findOneById('page_2_and_3') // Page 2
  776. // findAllById('page_2_and_3') // Page 2, Page 3
  777. // findAllByAction('about') // Page 1.3, Page 3
  778. return new Zend_Navigation(array(
  779. array(
  780. 'label' => 'Page 1',
  781. 'uri' => 'page-1',
  782. 'foo' => 'bar',
  783. 'pages' => array(
  784. array(
  785. 'label' => 'Page 1.1',
  786. 'uri' => 'page-1.1',
  787. 'foo' => 'bar',
  788. 'title' => 'The given title'
  789. ),
  790. array(
  791. 'label' => 'Page 1.2',
  792. 'uri' => 'page-1.2',
  793. 'title' => 'The given title'
  794. ),
  795. array(
  796. 'type' => 'uri',
  797. 'label' => 'Page 1.3',
  798. 'uri' => 'page-1.3',
  799. 'title' => 'The given title',
  800. 'action' => 'about'
  801. )
  802. )
  803. ),
  804. array(
  805. 'id' => 'page_2_and_3',
  806. 'label' => 'Page 2',
  807. 'module' => 'page2',
  808. 'controller' => 'index',
  809. 'action' => 'page1',
  810. 'page2' => 'page2'
  811. ),
  812. array(
  813. 'id' => 'page_2_and_3',
  814. 'label' => 'Page 3',
  815. 'module' => 'page3',
  816. 'controller' => 'index',
  817. 'action' => 'about'
  818. )
  819. ));
  820. }
  821. public function testCurrent()
  822. {
  823. $container = new Zend_Navigation(array(
  824. array(
  825. 'label' => 'Page 2',
  826. 'type' => 'uri'
  827. ),
  828. array(
  829. 'label' => 'Page 1',
  830. 'type' => 'uri',
  831. 'order' => -1
  832. )
  833. ));
  834. $page = $container->current();
  835. $this->assertEquals('Page 1', $page->getLabel());
  836. }
  837. public function testCurrentShouldThrowExceptionIfIndexIsInvalid()
  838. {
  839. require_once dirname(__FILE__) . '/_files/My/Container.php';
  840. $container = new My_Container(array(
  841. array(
  842. 'label' => 'Page 2',
  843. 'type' => 'uri'
  844. ),
  845. array(
  846. 'label' => 'Page 1',
  847. 'type' => 'uri',
  848. 'order' => -1
  849. )
  850. ));
  851. try {
  852. $page = $container->current();
  853. $this->fail('Container index is invalid, ' .
  854. 'but a Zend_Navigation_Exception was not thrown');
  855. } catch (Zend_Navigation_Exception $e) {
  856. $this->assertContains('Corruption detected', $e->getMessage());
  857. }
  858. }
  859. public function testKeyWhenContainerIsEmpty()
  860. {
  861. $container = new Zend_Navigation();
  862. $this->assertEquals(null, $container->key());
  863. }
  864. public function testKeyShouldReturnCurrentPageHash()
  865. {
  866. $container = new Zend_Navigation();
  867. $page = Zend_Navigation_Page::factory(array(
  868. 'type' => 'uri'
  869. ));
  870. $container->addPage($page);
  871. $this->assertEquals($page->hashCode(), $container->key());
  872. }
  873. public function testGetChildrenShouldReturnTheCurrentPage()
  874. {
  875. $container = new Zend_Navigation();
  876. $page = Zend_Navigation_Page::factory(array(
  877. 'type' => 'uri'
  878. ));
  879. $container->addPage($page);
  880. $this->assertEquals($page, $container->getChildren());
  881. }
  882. public function testGetChildrenShouldReturnNullWhenContainerIsEmpty()
  883. {
  884. $container = new Zend_Navigation();
  885. $this->assertEquals(null, $container->getChildren());
  886. }
  887. }