ContainerTest.php 36 KB

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