| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287 |
- <?php
- /**
- * Zend Framework
- *
- * LICENSE
- *
- * This source file is subject to the new BSD license that is bundled
- * with this package in the file LICENSE.txt.
- * It is also available through the world-wide-web at this URL:
- * http://framework.zend.com/license/new-bsd
- * If you did not receive a copy of the license and are unable to
- * obtain it through the world-wide-web, please send an email
- * to license@zend.com so we can send you a copy immediately.
- *
- * @category Zend
- * @package Zend_Navigation
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @version $Id$
- */
- require_once 'Zend/Navigation.php';
- require_once 'Zend/Config.php';
- require_once 'Zend/Controller/Front.php';
- require_once 'Zend/Controller/Request/Http.php';
- /**
- * Tests the class Zend_Navigation_Container
- *
- * @category Zend
- * @package Zend_Navigation
- * @subpackage UnitTests
- * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- * @group Zend_Navigation
- */
- class Zend_Navigation_ContainerTest extends PHPUnit_Framework_TestCase
- {
- /**
- * Prepares the environment before running a test.
- *
- */
- protected function setUp()
- {
- Zend_Controller_Front::getInstance()->setRequest(
- new Zend_Controller_Request_Http()
- );
- }
- /**
- * Tear down the environment after running a test
- *
- */
- protected function tearDown()
- {
- }
- public function testConstructWithArray()
- {
- $argument = array(
- array(
- 'label' => 'Page 1',
- 'uri' => 'page1.html'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => 'page2.html'
- ),
- array(
- 'label' => 'Page 3',
- 'uri' => 'page3.html'
- )
- );
- $container = new Zend_Navigation($argument);
- $this->assertEquals(3, $container->count());
- }
- public function testConstructWithConfig()
- {
- $argument = new Zend_Config(array(
- array(
- 'label' => 'Page 1',
- 'uri' => 'page1.html'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => 'page2.html'
- ),
- array(
- 'label' => 'Page 3',
- 'uri' => 'page3.html'
- )
- ));
- $container = new Zend_Navigation($argument);
- $this->assertEquals(3, $container->count());
- }
- public function testConstructorShouldThrowExceptionOnInvalidArgument()
- {
- try {
- $nav = new Zend_Navigation('ok');
- $this->fail('An invalid argument was given to the constructor, ' .
- 'but a Zend_Navigation_Exception was not thrown');
- } catch (Zend_Navigation_Exception $e) {
- $this->assertContains('Invalid argument: $pages', $e->getMessage());
- }
- try {
- $nav = new Zend_Navigation(1337);
- $this->fail('An invalid argument was given to the constructor, ' .
- 'but a Zend_Navigation_Exception was not thrown');
- } catch (Zend_Navigation_Exception $e) {
- $this->assertContains('Invalid argument: $pages', $e->getMessage());
- }
- try {
- $nav = new Zend_Navigation(new stdClass());
- $this->fail('An invalid argument was given to the constructor, ' .
- 'but a Zend_Navigation_Exception was not thrown');
- } catch (Zend_Navigation_Exception $e) {
- $this->assertContains('Invalid argument: $pages', $e->getMessage());
- }
- }
- public function testIterationShouldBeOrderAware()
- {
- $nav = new Zend_Navigation(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => '#',
- 'order' => -1
- ),
- array(
- 'label' => 'Page 3',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 4',
- 'uri' => '#',
- 'order' => 100
- ),
- array(
- 'label' => 'Page 5',
- 'uri' => '#'
- )
- ));
- $expected = array('Page 2', 'Page 1', 'Page 3', 'Page 5', 'Page 4');
- $actual = array();
- foreach ($nav as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testRecursiveIteration()
- {
- $nav = new Zend_Navigation(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#',
- 'pages' => array(
- array(
- 'label' => 'Page 1.1',
- 'uri' => '#',
- 'pages' => array(
- array(
- 'label' => 'Page 1.1.1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 1.1.2',
- 'uri' => '#'
- )
- )
- ),
- array(
- 'label' => 'Page 1.2',
- 'uri' => '#'
- )
- )
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => '#',
- 'pages' => array(
- array(
- 'label' => 'Page 2.1',
- 'uri' => '#'
- )
- )
- ),
- array(
- 'label' => 'Page 3',
- 'uri' => '#'
- )
- ));
- $actual = array();
- $expected = array(
- 'Page 1',
- 'Page 1.1',
- 'Page 1.1.1',
- 'Page 1.1.2',
- 'Page 1.2',
- 'Page 2',
- 'Page 2.1',
- 'Page 3'
- );
- $iterator = new RecursiveIteratorIterator($nav,
- RecursiveIteratorIterator::SELF_FIRST);
- foreach ($iterator as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testSettingPageOrderShouldUpdateContainerOrder()
- {
- $nav = new Zend_Navigation(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => '#'
- )
- ));
- $page3 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 3',
- 'uri' => '#'
- ));
- $nav->addPage($page3);
- $expected = array(
- 'before' => array('Page 1', 'Page 2', 'Page 3'),
- 'after' => array('Page 3', 'Page 1', 'Page 2')
- );
- $actual = array(
- 'before' => array(),
- 'after' => array()
- );
- foreach ($nav as $page) {
- $actual['before'][] = $page->getLabel();
- }
- $page3->setOrder(-1);
- foreach ($nav as $page) {
- $actual['after'][] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testAddPageShouldWorkWithArray()
- {
- $pageOptions = array(
- 'label' => 'From array',
- 'uri' => '#array'
- );
- $nav = new Zend_Navigation();
- $nav->addPage($pageOptions);
- $this->assertEquals(1, count($nav));
- }
- public function testAddPageShouldWorkWithConfig()
- {
- $pageOptions = array(
- 'label' => 'From config',
- 'uri' => '#config'
- );
- $pageOptions = new Zend_Config($pageOptions);
- $nav = new Zend_Navigation();
- $nav->addPage($pageOptions);
- $this->assertEquals(1, count($nav));
- }
- public function testAddPageShouldWorkWithPageInstance()
- {
- $pageOptions = array(
- 'label' => 'From array 1',
- 'uri' => '#array'
- );
- $nav = new Zend_Navigation(array($pageOptions));
- $page = Zend_Navigation_Page::factory($pageOptions);
- $nav->addPage($page);
- $this->assertEquals(2, count($nav));
- }
- public function testAddPagesShouldWorkWithArray()
- {
- $nav = new Zend_Navigation();
- $nav->addPages(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'action' => 'index',
- 'controller' => 'index'
- )
- ));
- $this->assertEquals(2, count($nav),
- 'Expected 2 pages, found ' . count($nav));
- }
- public function testAddPagesShouldWorkWithConfig()
- {
- $nav = new Zend_Navigation();
- $nav->addPages(new Zend_Config(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'action' => 'index',
- 'controller' => 'index'
- )
- )));
- $this->assertEquals(2, count($nav),
- 'Expected 2 pages, found ' . count($nav));
- }
- public function testAddPagesShouldWorkWithMixedArray()
- {
- $nav = new Zend_Navigation();
- $nav->addPages(new Zend_Config(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- new Zend_Config(array(
- 'label' => 'Page 2',
- 'action' => 'index',
- 'controller' => 'index'
- )),
- Zend_Navigation_Page::factory(array(
- 'label' => 'Page 3',
- 'uri' => '#'
- ))
- )));
- $this->assertEquals(3, count($nav),
- 'Expected 3 pages, found ' . count($nav));
- }
- /**
- * @group ZF-9815
- */
- public function testAddPagesShouldWorkWithNavigationContainer()
- {
- $nav = new Zend_Navigation();
- $nav->addPages($this->_getFindByNavigation());
-
- $this->assertEquals(3, count($nav),
- 'Expected 3 pages, found ' . count($nav));
-
- $this->assertEquals($nav->toArray(), $this->_getFindByNavigation()->toArray());
- }
- public function testAddPagesShouldThrowExceptionWhenGivenString()
- {
- $nav = new Zend_Navigation();
- try {
- $nav->addPages('this is a string');
- $this->fail('An invalid argument was given to addPages(), ' .
- 'but a Zend_Navigation_Exception was not thrown');
- } catch (Zend_Navigation_Exception $e) {
- $this->assertContains('Invalid argument: $pages must be', $e->getMessage());
- }
- }
- public function testAddPagesShouldThrowExceptionWhenGivenAnArbitraryObject()
- {
- $nav = new Zend_Navigation();
- try {
- $nav->addPages($pages = new stdClass());
- $this->fail('An invalid argument was given to addPages(), ' .
- 'but a Zend_Navigation_Exception was not thrown');
- } catch (Zend_Navigation_Exception $e) {
- $this->assertContains('Invalid argument: $pages must be', $e->getMessage());
- }
- }
- public function testRemovingAllPages()
- {
- $nav = new Zend_Navigation();
- $nav->addPages(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => '#'
- )
- ));
- $nav->removePages();
- $this->assertEquals(0, count($nav),
- 'Expected 0 pages, found ' . count($nav));
- }
- public function testSettingPages()
- {
- $nav = new Zend_Navigation();
- $nav->addPages(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => '#'
- )
- ));
- $nav->setPages(array(
- array(
- 'label' => 'Page 3',
- 'uri' => '#'
- )
- ));
- $this->assertEquals(1, count($nav),
- 'Expected 1 page, found ' . count($nav));
- }
- public function testGetPagesShouldReturnAnArrayOfPages()
- {
- $nav = new Zend_Navigation(array(
- array(
- 'uri' => 'Page 1'
- ),
- array(
- 'uri' => 'Page 2'
- )
- ));
- $pages = $nav->getPages();
- $expected = array(
- 'type' => 'array',
- 'count' => 2
- );
- $actual = array(
- 'type' => gettype($pages),
- 'count' => count($pages)
- );
- $this->assertEquals($expected, $actual);
- $this->assertContainsOnly('Zend_Navigation_Page_Uri', $pages, false);
- }
- public function testGetPagesShouldReturnUnorderedPages()
- {
- $nav = new Zend_Navigation(array(
- array(
- 'label' => 'Page 2',
- 'uri' => '#',
- 'order' => -1
- ),
- array(
- 'label' => 'Page 4',
- 'uri' => '#',
- 'order' => 100
- ),
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 5',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 3',
- 'uri' => '#'
- )
- ));
- $expected = array('Page 2', 'Page 4', 'Page 1', 'Page 5', 'Page 3');
- $actual = array();
- foreach ($nav->getPages() as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testRemovingPageByOrder()
- {
- $nav = new Zend_Navigation(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => '#',
- 'order' => 32
- ),
- array(
- 'label' => 'Page 3',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 4',
- 'uri' => '#'
- )
- ));
- $expected = array(
- 'remove0' => true,
- 'remove32' => true,
- 'remove0again' => true,
- 'remove1000' => false,
- 'count' => 1,
- 'current' => 'Page 4'
- );
- $actual = array(
- 'remove0' => $nav->removePage(0),
- 'remove32' => $nav->removePage(32),
- 'remove0again' => $nav->removePage(0),
- 'remove1000' => $nav->removePage(1000),
- 'count' => $nav->count(),
- 'current' => $nav->current()->getLabel()
- );
- $this->assertEquals($expected, $actual);
- }
- public function testRemovingPageByInstance()
- {
- $nav = new Zend_Navigation(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => '#'
- )
- ));
- $page3 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 3',
- 'uri' => '#'
- ));
- $nav->addPage($page3);
- $this->assertEquals(true, $nav->removePage($page3));
- }
- public function testRemovingPageByInstanceShouldReturnFalseIfPageIsNotInContainer()
- {
- $nav = new Zend_Navigation(array(
- array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ),
- array(
- 'label' => 'Page 2',
- 'uri' => '#'
- )
- ));
- $page = Zend_Navigation_Page::factory(array(
- 'label' => 'Page lol',
- 'uri' => '#'
- ));
- $this->assertEquals(false, $nav->removePage($page));
- }
- public function testHasPage()
- {
- $page0 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 0',
- 'uri' => '#'
- ));
- $page1 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ));
- $page1_1 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 1.1',
- 'uri' => '#'
- ));
- $page1_2 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 1.2',
- 'uri' => '#'
- ));
- $page1_2_1 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 1.2.1',
- 'uri' => '#'
- ));
- $page1_3 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 1.3',
- 'uri' => '#'
- ));
- $page2 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 2',
- 'uri' => '#'
- ));
- $page3 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 3',
- 'uri' => '#'
- ));
- $nav = new Zend_Navigation(array($page1, $page2, $page3));
- $page1->addPage($page1_1);
- $page1->addPage($page1_2);
- $page1_2->addPage($page1_2_1);
- $page1->addPage($page1_3);
- $expected = array(
- 'haspage0' => false,
- 'haspage2' => true,
- 'haspage1_1' => false,
- 'haspage1_1recursive' => true
- );
- $actual = array(
- 'haspage0' => $nav->hasPage($page0),
- 'haspage2' => $nav->hasPage($page2),
- 'haspage1_1' => $nav->hasPage($page1_1),
- 'haspage1_1recursive' => $nav->hasPage($page1_1, true)
- );
- $this->assertEquals($expected, $actual);
- }
- public function testHasPages()
- {
- $nav1 = new Zend_Navigation();
- $nav2 = new Zend_Navigation();
- $nav2->addPage(array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ));
- $expected = array(
- 'empty' => false,
- 'notempty' => true
- );
- $actual = array(
- 'empty' => $nav1->hasPages(),
- 'notempty' => $nav2->hasPages()
- );
- $this->assertEquals($expected, $actual);
- }
- public function testSetParentShouldWorkWithPage()
- {
- $page1 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ));
- $page2 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 2',
- 'uri' => '#'
- ));
- $page2->setParent($page1);
- $expected = array(
- 'parent' => 'Page 1',
- 'hasPages' => true
- );
- $actual = array(
- 'parent' => $page2->getParent()->getLabel(),
- 'hasPages' => $page1->hasPages()
- );
- $this->assertEquals($expected, $actual);
- }
- public function testSetParentShouldWorkWithNull()
- {
- $page1 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ));
- $page2 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 2',
- 'uri' => '#'
- ));
- $page2->setParent($page1);
- $page2->setParent(null);
- $this->assertEquals(null, $page2->getParent());
- }
- public function testSetParentShouldRemoveFromOldParentPage()
- {
- $page1 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 1',
- 'uri' => '#'
- ));
- $page2 = Zend_Navigation_Page::factory(array(
- 'label' => 'Page 2',
- 'uri' => '#'
- ));
- $page2->setParent($page1);
- $page2->setParent(null);
- $expected = array(
- 'parent' => null,
- 'haspages' => false
- );
- $actual = array(
- 'parent' => $page2->getParent(),
- 'haspages' => $page2->hasPages()
- );
- $this->assertEquals($expected, $actual);
- }
- public function testFinderMethodsShouldWorkWithCustomProperties()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findOneBy('page2', 'page2');
- $this->assertTrue($found instanceof Zend_Navigation_Page);
- $this->assertEquals('Page 2', $found->getLabel());
- }
- public function testFindOneByShouldReturnOnlyOnePage()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findOneBy('id', 'page_2_and_3');
- $this->assertTrue($found instanceof Zend_Navigation_Page);
- $this->assertEquals('Page 2', $found->getLabel());
- }
- public function testFindOneByShouldReturnNullIfNotFound()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findOneBy('id', 'non-existant');
- $this->assertNull($found);
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindOneByWithRegex()
- {
- $nav = $this->_getFindByNavigation();
-
- $found = $nav->findOneBy('id', '/page_2/', true);
- $this->assertEquals('Page 2', $found->getLabel());
-
- $found = $nav->findOneBy('class', '/([o]{2})/', true);
- $this->assertEquals('Page 1.3', $found->getLabel());
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindOneWithinArrayProperties()
- {
- $nav = $this->_getFindByNavigation();
-
- $found = $nav->findOneBy('rel', 'Example.org');
- $this->assertEquals('Page 1.2', $found->getLabel());
-
- $found = $nav->findOneBy('rel', '/Example/', true);
- $this->assertEquals('Page 1.2', $found->getLabel());
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindOneWithRegexWithinArrayProperties()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findOneBy('rel', '#Example(.*)#', true);
- $this->assertEquals('Page 1.2', $found->getLabel());
-
- $found = $nav->findOneBy('rel', '?Ex([a-z]*)?', true);
- $this->assertEquals('Page 1.2', $found->getLabel());
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindByWithRegex()
- {
- $nav = $this->_getFindByNavigation();
-
- // Find one
- $found = $nav->findBy('id', '/page_2/', false, true);
- $this->assertEquals('Page 2', $found->getLabel());
-
- $found = $nav->findBy('class', '/([fo]{2})/', false, true);
- $this->assertEquals('Page 1.3', $found->getLabel());
-
- // Find all
- $found = $nav->findBy('title', '/(The){1}.*(given){1}/', true, true);
-
- $expected = array('Page 1.1', 'Page 1.2', 'Page 1.3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testFindAllByShouldReturnAllMatchingPages()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findAllBy('id', 'page_2_and_3');
- $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
- $expected = array('Page 2', 'Page 3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testFindAllByShouldReturnEmptyArrayifNotFound()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findAllBy('id', 'non-existant');
- $expected = array('type' => 'array', 'count' => 0);
- $actual = array('type' => gettype($found), 'count' => count($found));
- $this->assertEquals($expected, $actual);
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindAllByWithRegex()
- {
- $nav = $this->_getFindByNavigation();
-
- $found = $nav->findAllBy('title', '/(The){1}.*(given){1}/', true);
-
- $expected = array('Page 1.1', 'Page 1.2', 'Page 1.3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindAllWithinArrayProperties()
- {
- $nav = $this->_getFindByNavigation();
-
- // Strict is true
- $found = $nav->findAllBy('rel', 'Example.org');
-
- $expected = array('Page 1.2', 'Page 1.3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindAllWithRegexWithinArrayProperties()
- {
- $nav = $this->_getFindByNavigation();
-
- // Test 1
- $found = $nav->findAllBy('rel', '#Example(.*)#', true);
-
- $expected = array('Page 1.2', 'Page 1.3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
-
- // Test 2
- $found = $nav->findAllBy('rel', '?Ex([a-z]*)?', true);
-
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testFindByShouldDefaultToFindOneBy()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findBy('id', 'page_2_and_3');
- $this->assertTrue($found instanceof Zend_Navigation_Page);
- }
- public function testFindOneByMagicMethodNativeProperty()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findOneById('page_2_and_3');
- $this->assertTrue($found instanceof Zend_Navigation_Page);
- $this->assertEquals('Page 2', $found->getLabel());
- }
- public function testFindOneByMagicMethodCustomProperty()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findOneBypage2('page2');
- $this->assertTrue($found instanceof Zend_Navigation_Page);
- $this->assertEquals('Page 2', $found->getLabel());
- }
- public function testFindAllByWithMagicMethodNativeProperty()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findAllById('page_2_and_3');
- $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
- $expected = array('Page 2', 'Page 3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testFindAllByMagicUcfirstPropDoesNotFindCustomLowercaseProps()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findAllByAction('about');
- $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
- $expected = array('Page 3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testFindAllByMagicLowercaseFindsBothNativeAndCustomProps()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findAllByaction('about');
- $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
- $expected = array('Page 1.3', 'Page 3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
- public function testFindByMagicMethodIsEquivalentToFindOneBy()
- {
- $nav = $this->_getFindByNavigation();
- $found = $nav->findById('page_2_and_3');
- $this->assertTrue($found instanceof Zend_Navigation_Page);
- $this->assertEquals('Page 2', $found->getLabel());
- }
- public function testInvalidMagicFinderMethodShouldThrowException()
- {
- $nav = $this->_getFindByNavigation();
- try {
- $found = $nav->findSomeById('page_2_and_3');
- $this->fail('An invalid magic finder method was used, ' .
- 'but a Zend_Navigation_Exception was not thrown');
- } catch (Zend_Navigation_Exception $e) {
- $this->assertContains('Bad method call', $e->getMessage());
- }
- }
- public function testInvalidMagicMethodShouldThrowException()
- {
- $nav = $this->_getFindByNavigation();
- try {
- $found = $nav->getPagez();
- $this->fail('An invalid magic finder method was used, ' .
- 'but a Zend_Navigation_Exception was not thrown');
- } catch (Zend_Navigation_Exception $e) {
- $this->assertContains('Bad method call', $e->getMessage());
- }
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindByMagicMethodWithRegex()
- {
- $nav = $this->_getFindByNavigation();
-
- // Find one
- $found = $nav->findOneByClass('/bar/', true);
- $this->assertSame('page-1.3', $found->getUri());
-
- // Find all
- $found = $nav->findAllById('/page_2_/', true);
- $this->assertContainsOnly('Zend_Navigation_Page', $found, false);
- $expected = array('Page 2', 'Page 3');
- $actual = array();
- foreach ($found as $page) {
- $actual[] = $page->getLabel();
- }
- $this->assertEquals($expected, $actual);
- }
-
- /**
- * @group ZF-9994
- */
- public function testFindByMagicMethodActivateRegex()
- {
- $nav = $this->_getFindByNavigation();
-
- $found = $nav->findOneByClass('/bar/', true);
- $this->assertSame('page-1.3', $found->getUri());
-
- $found = $nav->findOneByClass('/bar/', 1);
- $this->assertSame('page-1.3', $found->getUri());
-
- $found = $nav->findOneByClass('/bar/', 42);
- $this->assertSame('page-1.3', $found->getUri());
-
- $found = $nav->findOneByClass('/bar/', 'true');
- $this->assertSame('page-1.3', $found->getUri());
-
- $found = $nav->findOneByClass('/bar/', (object) null);
- $this->assertSame('page-1.3', $found->getUri());
-
- $found = $nav->findOneByClass('/bar/', '0');
- $this->assertNull($found);
-
- $found = $nav->findOneByClass('/bar/', array());
- $this->assertNull($found);
- }
- protected function _getFindByNavigation()
- {
- // findAllByFoo('bar') // Page 1, Page 1.1
- // findById('page_2_and_3') // Page 2
- // findOneById('page_2_and_3') // Page 2
- // findAllById('page_2_and_3') // Page 2, Page 3
- // findAllByAction('about') // Page 1.3, Page 3
- return new Zend_Navigation(array(
- array(
- 'label' => 'Page 1',
- 'uri' => 'page-1',
- 'foo' => 'bar',
- 'pages' => array(
- array(
- 'label' => 'Page 1.1',
- 'uri' => 'page-1.1',
- 'foo' => 'bar',
- 'title' => 'The given title',
- ),
- array(
- 'label' => 'Page 1.2',
- 'uri' => 'page-1.2',
- 'title' => 'The given title',
- 'rel' => array(
- 'alternate' => array(
- 'label' => 'Example.org',
- 'uri' => 'http://www.example.org/',
- ),
- ),
- ),
- array(
- 'type' => 'uri',
- 'label' => 'Page 1.3',
- 'uri' => 'page-1.3',
- 'title' => 'The given title',
- 'action' => 'about',
- 'class' => 'foo bar',
- 'rel' => array(
- 'alternate' => array(
- 'label' => 'Example.org',
- 'uri' => 'http://www.example.org/',
- ),
- ),
- )
- )
- ),
- array(
- 'id' => 'page_2_and_3',
- 'label' => 'Page 2',
- 'module' => 'page2',
- 'controller' => 'index',
- 'action' => 'page1',
- 'page2' => 'page2',
- ),
- array(
- 'id' => 'page_2_and_3',
- 'label' => 'Page 3',
- 'module' => 'page3',
- 'controller' => 'index',
- 'action' => 'about',
- )
- ));
- }
- public function testCurrent()
- {
- $container = new Zend_Navigation(array(
- array(
- 'label' => 'Page 2',
- 'type' => 'uri'
- ),
- array(
- 'label' => 'Page 1',
- 'type' => 'uri',
- 'order' => -1
- )
- ));
- $page = $container->current();
- $this->assertEquals('Page 1', $page->getLabel());
- }
- public function testCurrentShouldThrowExceptionIfIndexIsInvalid()
- {
- require_once dirname(__FILE__) . '/_files/My/Container.php';
- $container = new My_Container(array(
- array(
- 'label' => 'Page 2',
- 'type' => 'uri'
- ),
- array(
- 'label' => 'Page 1',
- 'type' => 'uri',
- 'order' => -1
- )
- ));
- try {
- $page = $container->current();
- $this->fail('Container index is invalid, ' .
- 'but a Zend_Navigation_Exception was not thrown');
- } catch (Zend_Navigation_Exception $e) {
- $this->assertContains('Corruption detected', $e->getMessage());
- }
- }
- public function testKeyWhenContainerIsEmpty()
- {
- $container = new Zend_Navigation();
- $this->assertEquals(null, $container->key());
- }
- public function testKeyShouldReturnCurrentPageHash()
- {
- $container = new Zend_Navigation();
- $page = Zend_Navigation_Page::factory(array(
- 'type' => 'uri'
- ));
- $container->addPage($page);
- $this->assertEquals($page->hashCode(), $container->key());
- }
- public function testGetChildrenShouldReturnTheCurrentPage()
- {
- $container = new Zend_Navigation();
- $page = Zend_Navigation_Page::factory(array(
- 'type' => 'uri'
- ));
- $container->addPage($page);
- $this->assertEquals($page, $container->getChildren());
- }
- public function testGetChildrenShouldReturnNullWhenContainerIsEmpty()
- {
- $container = new Zend_Navigation();
- $this->assertEquals(null, $container->getChildren());
- }
- public function testRemovePageRecursively()
- {
- $container = new Zend_Navigation(array(
- array(
- 'route' => 'foo',
- 'pages' => array(
- array(
- 'route' => 'bar',
- 'pages' => array(
- array(
- 'route' => 'baz',
- ),
- ),
- )
- )
- ),
- ));
- $container->removePage($container->findOneBy('route', 'baz'), true);
- $this->assertNull($container->findOneBy('route', 'baz'));
- $container->removePage($container->findOneBy('route', 'bar'), true);
- $this->assertNull($container->findOneBy('route', 'bar'));
- }
- }
|