/** * The page factory should not work with page types that don't exist * */ public function testShouldFailForNonExistantType() { $pageConfig = array('type' => 'My_NonExistant_Page', 'label' => 'My non-existant Page'); try { $page = Zym_Navigation_Page::factory($pageConfig); } catch (Zend_Exception $e) { return; } $msg = 'A Zend_Exception has not been thrown for non-existant class'; $this->fail($msg); }
/** * When setting new parent for page that already has a parent, it should * be removed from the old parent * */ public function testSetParentShouldRemoveFromOldParentPage() { $page1 = Zym_Navigation_Page::factory(array('label' => 'Page 1', 'uri' => '#')); $page2 = Zym_Navigation_Page::factory(array('label' => 'Page 2', 'uri' => '#')); $page2->setParent($page1); $page2->setParent(null); $this->assertEquals(null, $page2->getParent()); $this->assertEquals(false, $page1->hasPages()); }
/** * Tests the toArray() method * */ public function testToArrayMethod() { $options = array( 'label' => 'foo', 'uri' => '#', 'id' => 'my-id', 'class' => 'my-class', 'title' => 'my-title', 'target' => 'my-target', 'position' => 100, 'active' => true, 'visible' => false, 'resource' => 'joker', 'privilege' => null, 'foo' => 'bar', 'meaning' => 42, 'pages' => array( array( 'label' => 'foo.bar', 'uri' => '#' ), array( 'label' => 'foo.baz', 'uri' => '#' ) ) ); $page = Zym_Navigation_Page::factory($options); $toArray = $page->toArray(); // tweak options to what we expect toArray() to contain $options['type'] = 'Zym_Navigation_Page_Uri'; // calculate diff between toArray() and $options $diff = array_diff_assoc($toArray, $options); // should be no diff $this->assertEquals(array(), $diff); // $toArray should have 2 sub pages $this->assertEquals(2, count($toArray['pages'])); // tweak options to what we expect sub page 1 to be $options['label'] = 'foo.bar'; $options['position'] = null; $options['id'] = null; $options['class'] = null; $options['title'] = null; $options['target'] = null; $options['resource'] = null; $options['active'] = false; $options['visible'] = true; unset($options['foo']); unset($options['meaning']); // assert that there is no diff from what we expect $subPageOneDiff = array_diff_assoc($toArray['pages'][0], $options); $this->assertEquals(array(), $subPageOneDiff); // tweak options to what we expect sub page 2 to be $options['label'] = 'foo.baz'; // assert that there is no diff from what we expect $subPageTwoDiff = array_diff_assoc($toArray['pages'][1], $options); $this->assertEquals(array(), $subPageTwoDiff); }
/** * Adds a page to the container * * @param Zym_Navigation_Page|array|Zend_Config $page page to add * @return Zym_Navigation_Container * @throws InvalidArgumentException if invalid page is given */ public function addPage($page) { if (is_array($page) || $page instanceof Zend_Config) { require_once 'Zym/Navigation/Page.php'; $page = Zym_Navigation_Page::factory($page); } elseif (!$page instanceof Zym_Navigation_Page) { $msg = '$page must be Zym_Navigation_Page|array|Zend_Config'; throw new InvalidArgumentException($msg); } $id = spl_object_hash($page); if (array_key_exists($id, $this->_order)) { return $this; } $this->_pages[$id] = $page; $this->_order[$id] = $page->getPosition(); $this->_orderUpdated = true; $page->setParent($this); return $this; }
<?php require_once 'Zym/Navigation/Page.php'; class My_Navigation_Page extends Zym_Navigation_Page { protected $_foo; protected $_fooBar; public function setFoo($foo) { $this->_foo = $foo; } public function setFooBar($fooBar) { $this->_fooBar = $fooBar; } } // can now construct using $page = new My_Navigation_Page(array('label' => 'Property names are translated', 'foo' => 'bar', 'foo_bar' => 'baz')); // ...or $page = Zym_Navigation_Page::factory(array('type' => 'My_Navigation_Page', 'label' => 'Property names are translated', 'foo' => 'bar', 'foo_bar' => 'baz'));
<?php class My_Navigation_Page extends Zym_Navigation_Page { protected $_fooBar = 'ok'; public function setFooBar($fooBar) { $this->_fooBar = $fooBar; } } $page = Zym_Navigation_Page::factory(array('type' => 'My_Navigation_Page', 'label' => 'My custom page', 'foo_bar' => 'foo bar'));