ContainerTest.php 28 KB

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