ContainerTest.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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-2012 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-2012 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. /**
  327. * @group ZF-9815
  328. */
  329. public function testAddPagesShouldWorkWithNavigationContainer()
  330. {
  331. $nav = new Zend_Navigation();
  332. $nav->addPages($this->_getFindByNavigation());
  333. $this->assertEquals(3, count($nav),
  334. 'Expected 3 pages, found ' . count($nav));
  335. $this->assertEquals($nav->toArray(), $this->_getFindByNavigation()->toArray());
  336. }
  337. public function testAddPagesShouldThrowExceptionWhenGivenString()
  338. {
  339. $nav = new Zend_Navigation();
  340. try {
  341. $nav->addPages('this is a string');
  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 testAddPagesShouldThrowExceptionWhenGivenAnArbitraryObject()
  349. {
  350. $nav = new Zend_Navigation();
  351. try {
  352. $nav->addPages($pages = new stdClass());
  353. $this->fail('An invalid argument was given to addPages(), ' .
  354. 'but a Zend_Navigation_Exception was not thrown');
  355. } catch (Zend_Navigation_Exception $e) {
  356. $this->assertContains('Invalid argument: $pages must be', $e->getMessage());
  357. }
  358. }
  359. public function testRemovingAllPages()
  360. {
  361. $nav = new Zend_Navigation();
  362. $nav->addPages(array(
  363. array(
  364. 'label' => 'Page 1',
  365. 'uri' => '#'
  366. ),
  367. array(
  368. 'label' => 'Page 2',
  369. 'uri' => '#'
  370. )
  371. ));
  372. $nav->removePages();
  373. $this->assertEquals(0, count($nav),
  374. 'Expected 0 pages, found ' . count($nav));
  375. }
  376. public function testSettingPages()
  377. {
  378. $nav = new Zend_Navigation();
  379. $nav->addPages(array(
  380. array(
  381. 'label' => 'Page 1',
  382. 'uri' => '#'
  383. ),
  384. array(
  385. 'label' => 'Page 2',
  386. 'uri' => '#'
  387. )
  388. ));
  389. $nav->setPages(array(
  390. array(
  391. 'label' => 'Page 3',
  392. 'uri' => '#'
  393. )
  394. ));
  395. $this->assertEquals(1, count($nav),
  396. 'Expected 1 page, found ' . count($nav));
  397. }
  398. public function testGetPagesShouldReturnAnArrayOfPages()
  399. {
  400. $nav = new Zend_Navigation(array(
  401. array(
  402. 'uri' => 'Page 1'
  403. ),
  404. array(
  405. 'uri' => 'Page 2'
  406. )
  407. ));
  408. $pages = $nav->getPages();
  409. $expected = array(
  410. 'type' => 'array',
  411. 'count' => 2
  412. );
  413. $actual = array(
  414. 'type' => gettype($pages),
  415. 'count' => count($pages)
  416. );
  417. $this->assertEquals($expected, $actual);
  418. $this->assertContainsOnly('Zend_Navigation_Page_Uri', $pages, false);
  419. }
  420. public function testGetPagesShouldReturnUnorderedPages()
  421. {
  422. $nav = new Zend_Navigation(array(
  423. array(
  424. 'label' => 'Page 2',
  425. 'uri' => '#',
  426. 'order' => -1
  427. ),
  428. array(
  429. 'label' => 'Page 4',
  430. 'uri' => '#',
  431. 'order' => 100
  432. ),
  433. array(
  434. 'label' => 'Page 1',
  435. 'uri' => '#'
  436. ),
  437. array(
  438. 'label' => 'Page 5',
  439. 'uri' => '#'
  440. ),
  441. array(
  442. 'label' => 'Page 3',
  443. 'uri' => '#'
  444. )
  445. ));
  446. $expected = array('Page 2', 'Page 4', 'Page 1', 'Page 5', 'Page 3');
  447. $actual = array();
  448. foreach ($nav->getPages() as $page) {
  449. $actual[] = $page->getLabel();
  450. }
  451. $this->assertEquals($expected, $actual);
  452. }
  453. public function testRemovingPageByOrder()
  454. {
  455. $nav = new Zend_Navigation(array(
  456. array(
  457. 'label' => 'Page 1',
  458. 'uri' => '#'
  459. ),
  460. array(
  461. 'label' => 'Page 2',
  462. 'uri' => '#',
  463. 'order' => 32
  464. ),
  465. array(
  466. 'label' => 'Page 3',
  467. 'uri' => '#'
  468. ),
  469. array(
  470. 'label' => 'Page 4',
  471. 'uri' => '#'
  472. )
  473. ));
  474. $expected = array(
  475. 'remove0' => true,
  476. 'remove32' => true,
  477. 'remove0again' => true,
  478. 'remove1000' => false,
  479. 'count' => 1,
  480. 'current' => 'Page 4'
  481. );
  482. $actual = array(
  483. 'remove0' => $nav->removePage(0),
  484. 'remove32' => $nav->removePage(32),
  485. 'remove0again' => $nav->removePage(0),
  486. 'remove1000' => $nav->removePage(1000),
  487. 'count' => $nav->count(),
  488. 'current' => $nav->current()->getLabel()
  489. );
  490. $this->assertEquals($expected, $actual);
  491. }
  492. public function testRemovingPageByInstance()
  493. {
  494. $nav = new Zend_Navigation(array(
  495. array(
  496. 'label' => 'Page 1',
  497. 'uri' => '#'
  498. ),
  499. array(
  500. 'label' => 'Page 2',
  501. 'uri' => '#'
  502. )
  503. ));
  504. $page3 = Zend_Navigation_Page::factory(array(
  505. 'label' => 'Page 3',
  506. 'uri' => '#'
  507. ));
  508. $nav->addPage($page3);
  509. $this->assertEquals(true, $nav->removePage($page3));
  510. }
  511. public function testRemovingPageByInstanceShouldReturnFalseIfPageIsNotInContainer()
  512. {
  513. $nav = new Zend_Navigation(array(
  514. array(
  515. 'label' => 'Page 1',
  516. 'uri' => '#'
  517. ),
  518. array(
  519. 'label' => 'Page 2',
  520. 'uri' => '#'
  521. )
  522. ));
  523. $page = Zend_Navigation_Page::factory(array(
  524. 'label' => 'Page lol',
  525. 'uri' => '#'
  526. ));
  527. $this->assertEquals(false, $nav->removePage($page));
  528. }
  529. public function testHasPage()
  530. {
  531. $page0 = Zend_Navigation_Page::factory(array(
  532. 'label' => 'Page 0',
  533. 'uri' => '#'
  534. ));
  535. $page1 = Zend_Navigation_Page::factory(array(
  536. 'label' => 'Page 1',
  537. 'uri' => '#'
  538. ));
  539. $page1_1 = Zend_Navigation_Page::factory(array(
  540. 'label' => 'Page 1.1',
  541. 'uri' => '#'
  542. ));
  543. $page1_2 = Zend_Navigation_Page::factory(array(
  544. 'label' => 'Page 1.2',
  545. 'uri' => '#'
  546. ));
  547. $page1_2_1 = Zend_Navigation_Page::factory(array(
  548. 'label' => 'Page 1.2.1',
  549. 'uri' => '#'
  550. ));
  551. $page1_3 = Zend_Navigation_Page::factory(array(
  552. 'label' => 'Page 1.3',
  553. 'uri' => '#'
  554. ));
  555. $page2 = Zend_Navigation_Page::factory(array(
  556. 'label' => 'Page 2',
  557. 'uri' => '#'
  558. ));
  559. $page3 = Zend_Navigation_Page::factory(array(
  560. 'label' => 'Page 3',
  561. 'uri' => '#'
  562. ));
  563. $nav = new Zend_Navigation(array($page1, $page2, $page3));
  564. $page1->addPage($page1_1);
  565. $page1->addPage($page1_2);
  566. $page1_2->addPage($page1_2_1);
  567. $page1->addPage($page1_3);
  568. $expected = array(
  569. 'haspage0' => false,
  570. 'haspage2' => true,
  571. 'haspage1_1' => false,
  572. 'haspage1_1recursive' => true
  573. );
  574. $actual = array(
  575. 'haspage0' => $nav->hasPage($page0),
  576. 'haspage2' => $nav->hasPage($page2),
  577. 'haspage1_1' => $nav->hasPage($page1_1),
  578. 'haspage1_1recursive' => $nav->hasPage($page1_1, true)
  579. );
  580. $this->assertEquals($expected, $actual);
  581. }
  582. public function testHasPages()
  583. {
  584. $nav1 = new Zend_Navigation();
  585. $nav2 = new Zend_Navigation();
  586. $nav2->addPage(array(
  587. 'label' => 'Page 1',
  588. 'uri' => '#'
  589. ));
  590. $expected = array(
  591. 'empty' => false,
  592. 'notempty' => true
  593. );
  594. $actual = array(
  595. 'empty' => $nav1->hasPages(),
  596. 'notempty' => $nav2->hasPages()
  597. );
  598. $this->assertEquals($expected, $actual);
  599. }
  600. public function testSetParentShouldWorkWithPage()
  601. {
  602. $page1 = Zend_Navigation_Page::factory(array(
  603. 'label' => 'Page 1',
  604. 'uri' => '#'
  605. ));
  606. $page2 = Zend_Navigation_Page::factory(array(
  607. 'label' => 'Page 2',
  608. 'uri' => '#'
  609. ));
  610. $page2->setParent($page1);
  611. $expected = array(
  612. 'parent' => 'Page 1',
  613. 'hasPages' => true
  614. );
  615. $actual = array(
  616. 'parent' => $page2->getParent()->getLabel(),
  617. 'hasPages' => $page1->hasPages()
  618. );
  619. $this->assertEquals($expected, $actual);
  620. }
  621. public function testSetParentShouldWorkWithNull()
  622. {
  623. $page1 = Zend_Navigation_Page::factory(array(
  624. 'label' => 'Page 1',
  625. 'uri' => '#'
  626. ));
  627. $page2 = Zend_Navigation_Page::factory(array(
  628. 'label' => 'Page 2',
  629. 'uri' => '#'
  630. ));
  631. $page2->setParent($page1);
  632. $page2->setParent(null);
  633. $this->assertEquals(null, $page2->getParent());
  634. }
  635. public function testSetParentShouldRemoveFromOldParentPage()
  636. {
  637. $page1 = Zend_Navigation_Page::factory(array(
  638. 'label' => 'Page 1',
  639. 'uri' => '#'
  640. ));
  641. $page2 = Zend_Navigation_Page::factory(array(
  642. 'label' => 'Page 2',
  643. 'uri' => '#'
  644. ));
  645. $page2->setParent($page1);
  646. $page2->setParent(null);
  647. $expected = array(
  648. 'parent' => null,
  649. 'haspages' => false
  650. );
  651. $actual = array(
  652. 'parent' => $page2->getParent(),
  653. 'haspages' => $page2->hasPages()
  654. );
  655. $this->assertEquals($expected, $actual);
  656. }
  657. public function testFinderMethodsShouldWorkWithCustomProperties()
  658. {
  659. $nav = $this->_getFindByNavigation();
  660. $found = $nav->findOneBy('page2', 'page2');
  661. $this->assertType('Zend_Navigation_Page', $found);
  662. $this->assertEquals('Page 2', $found->getLabel());
  663. }
  664. public function testFindOneByShouldReturnOnlyOnePage()
  665. {
  666. $nav = $this->_getFindByNavigation();
  667. $found = $nav->findOneBy('id', 'page_2_and_3');
  668. $this->assertType('Zend_Navigation_Page', $found);
  669. $this->assertEquals('Page 2', $found->getLabel());
  670. }
  671. public function testFindOneByShouldReturnNullIfNotFound()
  672. {
  673. $nav = $this->_getFindByNavigation();
  674. $found = $nav->findOneBy('id', 'non-existant');
  675. $this->assertNull($found);
  676. }
  677. public function testFindAllByShouldReturnAllMatchingPages()
  678. {
  679. $nav = $this->_getFindByNavigation();
  680. $found = $nav->findAllBy('id', 'page_2_and_3');
  681. $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
  682. $expected = array('Page 2', 'Page 3');
  683. $actual = array();
  684. foreach ($found as $page) {
  685. $actual[] = $page->getLabel();
  686. }
  687. $this->assertEquals($expected, $actual);
  688. }
  689. public function testFindAllByShouldReturnEmptyArrayifNotFound()
  690. {
  691. $nav = $this->_getFindByNavigation();
  692. $found = $nav->findAllBy('id', 'non-existant');
  693. $expected = array('type' => 'array', 'count' => 0);
  694. $actual = array('type' => gettype($found), 'count' => count($found));
  695. $this->assertEquals($expected, $actual);
  696. }
  697. public function testFindByShouldDefaultToFindOneBy()
  698. {
  699. $nav = $this->_getFindByNavigation();
  700. $found = $nav->findBy('id', 'page_2_and_3');
  701. $this->assertType('Zend_Navigation_Page', $found);
  702. }
  703. public function testFindOneByMagicMethodNativeProperty()
  704. {
  705. $nav = $this->_getFindByNavigation();
  706. $found = $nav->findOneById('page_2_and_3');
  707. $this->assertType('Zend_Navigation_Page', $found);
  708. $this->assertEquals('Page 2', $found->getLabel());
  709. }
  710. public function testFindOneByMagicMethodCustomProperty()
  711. {
  712. $nav = $this->_getFindByNavigation();
  713. $found = $nav->findOneBypage2('page2');
  714. $this->assertType('Zend_Navigation_Page', $found);
  715. $this->assertEquals('Page 2', $found->getLabel());
  716. }
  717. public function testFindAllByWithMagicMethodNativeProperty()
  718. {
  719. $nav = $this->_getFindByNavigation();
  720. $found = $nav->findAllById('page_2_and_3');
  721. $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
  722. $expected = array('Page 2', 'Page 3');
  723. $actual = array();
  724. foreach ($found as $page) {
  725. $actual[] = $page->getLabel();
  726. }
  727. $this->assertEquals($expected, $actual);
  728. }
  729. public function testFindAllByMagicUcfirstPropDoesNotFindCustomLowercaseProps()
  730. {
  731. $nav = $this->_getFindByNavigation();
  732. $found = $nav->findAllByAction('about');
  733. $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
  734. $expected = array('Page 3');
  735. $actual = array();
  736. foreach ($found as $page) {
  737. $actual[] = $page->getLabel();
  738. }
  739. $this->assertEquals($expected, $actual);
  740. }
  741. public function testFindAllByMagicLowercaseFindsBothNativeAndCustomProps()
  742. {
  743. $nav = $this->_getFindByNavigation();
  744. $found = $nav->findAllByaction('about');
  745. $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
  746. $expected = array('Page 1.3', 'Page 3');
  747. $actual = array();
  748. foreach ($found as $page) {
  749. $actual[] = $page->getLabel();
  750. }
  751. $this->assertEquals($expected, $actual);
  752. }
  753. public function testFindByMagicMethodIsEquivalentToFindOneBy()
  754. {
  755. $nav = $this->_getFindByNavigation();
  756. $found = $nav->findById('page_2_and_3');
  757. $this->assertType('Zend_Navigation_Page', $found);
  758. $this->assertEquals('Page 2', $found->getLabel());
  759. }
  760. public function testInvalidMagicFinderMethodShouldThrowException()
  761. {
  762. $nav = $this->_getFindByNavigation();
  763. try {
  764. $found = $nav->findSomeById('page_2_and_3');
  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. public function testInvalidMagicMethodShouldThrowException()
  772. {
  773. $nav = $this->_getFindByNavigation();
  774. try {
  775. $found = $nav->getPagez();
  776. $this->fail('An invalid magic finder method was used, ' .
  777. 'but a Zend_Navigation_Exception was not thrown');
  778. } catch (Zend_Navigation_Exception $e) {
  779. $this->assertContains('Bad method call', $e->getMessage());
  780. }
  781. }
  782. protected function _getFindByNavigation()
  783. {
  784. // findAllByFoo('bar') // Page 1, Page 1.1
  785. // findById('page_2_and_3') // Page 2
  786. // findOneById('page_2_and_3') // Page 2
  787. // findAllById('page_2_and_3') // Page 2, Page 3
  788. // findAllByAction('about') // Page 1.3, Page 3
  789. return new Zend_Navigation(array(
  790. array(
  791. 'label' => 'Page 1',
  792. 'uri' => 'page-1',
  793. 'foo' => 'bar',
  794. 'pages' => array(
  795. array(
  796. 'label' => 'Page 1.1',
  797. 'uri' => 'page-1.1',
  798. 'foo' => 'bar',
  799. 'title' => 'The given title'
  800. ),
  801. array(
  802. 'label' => 'Page 1.2',
  803. 'uri' => 'page-1.2',
  804. 'title' => 'The given title'
  805. ),
  806. array(
  807. 'type' => 'uri',
  808. 'label' => 'Page 1.3',
  809. 'uri' => 'page-1.3',
  810. 'title' => 'The given title',
  811. 'action' => 'about'
  812. )
  813. )
  814. ),
  815. array(
  816. 'id' => 'page_2_and_3',
  817. 'label' => 'Page 2',
  818. 'module' => 'page2',
  819. 'controller' => 'index',
  820. 'action' => 'page1',
  821. 'page2' => 'page2'
  822. ),
  823. array(
  824. 'id' => 'page_2_and_3',
  825. 'label' => 'Page 3',
  826. 'module' => 'page3',
  827. 'controller' => 'index',
  828. 'action' => 'about'
  829. )
  830. ));
  831. }
  832. public function testCurrent()
  833. {
  834. $container = new Zend_Navigation(array(
  835. array(
  836. 'label' => 'Page 2',
  837. 'type' => 'uri'
  838. ),
  839. array(
  840. 'label' => 'Page 1',
  841. 'type' => 'uri',
  842. 'order' => -1
  843. )
  844. ));
  845. $page = $container->current();
  846. $this->assertEquals('Page 1', $page->getLabel());
  847. }
  848. public function testCurrentShouldThrowExceptionIfIndexIsInvalid()
  849. {
  850. require_once dirname(__FILE__) . '/_files/My/Container.php';
  851. $container = new My_Container(array(
  852. array(
  853. 'label' => 'Page 2',
  854. 'type' => 'uri'
  855. ),
  856. array(
  857. 'label' => 'Page 1',
  858. 'type' => 'uri',
  859. 'order' => -1
  860. )
  861. ));
  862. try {
  863. $page = $container->current();
  864. $this->fail('Container index is invalid, ' .
  865. 'but a Zend_Navigation_Exception was not thrown');
  866. } catch (Zend_Navigation_Exception $e) {
  867. $this->assertContains('Corruption detected', $e->getMessage());
  868. }
  869. }
  870. public function testKeyWhenContainerIsEmpty()
  871. {
  872. $container = new Zend_Navigation();
  873. $this->assertEquals(null, $container->key());
  874. }
  875. public function testKeyShouldReturnCurrentPageHash()
  876. {
  877. $container = new Zend_Navigation();
  878. $page = Zend_Navigation_Page::factory(array(
  879. 'type' => 'uri'
  880. ));
  881. $container->addPage($page);
  882. $this->assertEquals($page->hashCode(), $container->key());
  883. }
  884. public function testGetChildrenShouldReturnTheCurrentPage()
  885. {
  886. $container = new Zend_Navigation();
  887. $page = Zend_Navigation_Page::factory(array(
  888. 'type' => 'uri'
  889. ));
  890. $container->addPage($page);
  891. $this->assertEquals($page, $container->getChildren());
  892. }
  893. public function testGetChildrenShouldReturnNullWhenContainerIsEmpty()
  894. {
  895. $container = new Zend_Navigation();
  896. $this->assertEquals(null, $container->getChildren());
  897. }
  898. }