ContainerTest.php 29 KB

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