123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- <?php
-
- namespace Knp\Menu\Tests;
-
- use Knp\Menu\MenuItem;
- use Knp\Menu\MenuFactory;
-
- class MenuItemGetterSetterTest extends \PHPUnit_Framework_TestCase
- {
- public function testCreateMenuItemWithEmptyParameter()
- {
- $menu = $this->createMenu();
- $this->assertTrue($menu instanceof MenuItem);
- }
-
- public function testCreateMenuWithNameAndUri()
- {
- $menu = $this->createMenu('test1', 'other_uri');
- $this->assertEquals('test1', $menu->getName());
- $this->assertEquals('other_uri', $menu->getUri());
- }
-
- public function testCreateMenuWithTitle()
- {
- $title = 'This is a test item title';
- $menu = $this->createMenu(null, null, array('title' => $title));
- $this->assertEquals($title, $menu->getAttribute('title'));
- }
-
- public function testName()
- {
- $menu = $this->createMenu();
- $menu->setName('menu name');
- $this->assertEquals('menu name', $menu->getName());
- }
-
- public function testLabel()
- {
- $menu = $this->createMenu();
- $menu->setLabel('menu label');
- $this->assertEquals('menu label', $menu->getLabel());
- }
-
- public function testNameIsUsedAsDefaultLabel()
- {
- $menu = $this->createMenu('My Label');
- $this->assertEquals('My Label', $menu->getLabel());
- $menu->setLabel('Other Label');
- $this->assertEquals('Other Label', $menu->getLabel());
- }
-
- public function testUri()
- {
- $menu = $this->createMenu();
- $menu->setUri('menu_uri');
- $this->assertEquals('menu_uri', $menu->getUri());
- }
-
- public function testAttributes()
- {
- $attributes = array('class' => 'test_class', 'title' => 'Test title');
- $menu = $this->createMenu();
- $menu->setAttributes($attributes);
- $this->assertEquals($attributes, $menu->getAttributes());
- }
-
- public function testDefaultAttribute()
- {
- $menu = $this->createMenu(null, null, array('id' => 'test_id'));
- $this->assertEquals('test_id', $menu->getAttribute('id'));
- $this->assertEquals('default_value', $menu->getAttribute('unknown_attribute', 'default_value'));
- }
-
- public function testLinkAttributes()
- {
- $attributes = array('class' => 'test_class', 'title' => 'Test title');
- $menu = $this->createMenu();
- $menu->setLinkAttributes($attributes);
- $this->assertEquals($attributes, $menu->getLinkAttributes());
- }
-
- public function testDefaultLinkAttribute()
- {
- $menu = $this->createMenu();
- $menu->setLinkAttribute('class', 'test_class');
- $this->assertEquals('test_class', $menu->getLinkAttribute('class'));
- $this->assertNull($menu->getLinkAttribute('title'));
- $this->assertEquals('foobar', $menu->getLinkAttribute('title', 'foobar'));
- }
-
- public function testChildrenAttributes()
- {
- $attributes = array('class' => 'test_class', 'title' => 'Test title');
- $menu = $this->createMenu();
- $menu->setChildrenAttributes($attributes);
- $this->assertEquals($attributes, $menu->getChildrenAttributes());
- }
-
- public function testDefaultChildrenAttribute()
- {
- $menu = $this->createMenu();
- $menu->setChildrenAttribute('class', 'test_class');
- $this->assertEquals('test_class', $menu->getChildrenAttribute('class'));
- $this->assertNull($menu->getChildrenAttribute('title'));
- $this->assertEquals('foobar', $menu->getChildrenAttribute('title', 'foobar'));
- }
-
- public function testLabelAttributes()
- {
- $attributes = array('class' => 'test_class', 'title' => 'Test title');
- $menu = $this->createMenu();
- $menu->setLabelAttributes($attributes);
- $this->assertEquals($attributes, $menu->getLabelAttributes());
- }
-
- public function testDefaultLabelAttribute()
- {
- $menu = $this->createMenu();
- $menu->setLabelAttribute('class', 'test_class');
- $this->assertEquals('test_class', $menu->getLabelAttribute('class'));
- $this->assertNull($menu->getLabelAttribute('title'));
- $this->assertEquals('foobar', $menu->getLabelAttribute('title', 'foobar'));
- }
-
- public function testExtras()
- {
- $extras = array('class' => 'test_class', 'title' => 'Test title');
- $menu = $this->createMenu();
- $menu->setExtras($extras);
- $this->assertEquals($extras, $menu->getExtras());
- }
-
- public function testDefaultExtras()
- {
- $menu = $this->createMenu();
- $menu->setExtra('class', 'test_class');
- $this->assertEquals('test_class', $menu->getExtra('class'));
- $this->assertNull($menu->getExtra('title'));
- $this->assertEquals('foobar', $menu->getExtra('title', 'foobar'));
- }
-
- public function testDisplay()
- {
- $menu = $this->createMenu();
- $this->assertEquals(true, $menu->isDisplayed());
- $menu->setDisplay(false);
- $this->assertEquals(false, $menu->isDisplayed());
- }
-
- public function testShowChildren()
- {
- $menu = $this->createMenu();
- $this->assertEquals(true, $menu->getDisplayChildren());
- $menu->setDisplayChildren(false);
- $this->assertEquals(false, $menu->getDisplayChildren());
- }
-
- public function testParent()
- {
- $menu = $this->createMenu();
- $child = $this->createMenu('child_menu');
- $this->assertNull($child->getParent());
- $child->setParent($menu);
- $this->assertEquals($menu, $child->getParent());
- }
-
- public function testChildren()
- {
- $menu = $this->createMenu();
- $child = $this->createMenu('child_menu');
- $menu->setChildren(array($child));
- $this->assertEquals(array($child), $menu->getChildren());
- }
-
- /**
- * @expectedException InvalidArgumentException
- */
- public function testSetExistingNameThrowsAnException()
- {
- $menu = $this->createMenu();
- $menu->addChild('jack');
- $menu->addChild('joe');
- $menu->getChild('joe')->setName('jack');
- }
-
- public function testSetSameName()
- {
- $parent = $this->getMock('Knp\Menu\ItemInterface');
- $parent->expects($this->never())
- ->method('offsetExists');
-
- $menu = $this->createMenu('my_name');
- $menu->setParent($parent);
- $menu->setName('my_name');
- $this->assertEquals('my_name', $menu->getName());
- }
-
- public function testToArrayWithChildren()
- {
- $menu = $this->createMenu();
- $menu->addChild('jack', array('uri' => 'http://php.net', 'linkAttributes' => array('title' => 'php'), 'display' => false))
- ->addChild('john')
- ;
- $menu->addChild('joe', array('attributes' => array('class' => 'leaf'), 'label' => 'test', 'labelAttributes' => array('class' => 'center'), 'displayChildren' => false));
-
- $this->assertEquals(
- array(
- 'name' => 'test_menu',
- 'label' => null,
- 'uri' => 'homepage',
- 'attributes' => array(),
- 'labelAttributes' => array(),
- 'linkAttributes' => array(),
- 'childrenAttributes' => array(),
- 'extras' => array(),
- 'display' => true,
- 'displayChildren' => true,
- 'children' => array(
- 'jack' => array(
- 'name' => 'jack',
- 'label' => null,
- 'uri' => 'http://php.net',
- 'attributes' => array(),
- 'labelAttributes' => array(),
- 'linkAttributes' => array('title' => 'php'),
- 'childrenAttributes' => array(),
- 'extras' => array(),
- 'display' => false,
- 'displayChildren' => true,
- 'children' => array(
- 'john' => array(
- 'name' => 'john',
- 'label' => null,
- 'uri' => null,
- 'attributes' => array(),
- 'labelAttributes' => array(),
- 'linkAttributes' => array(),
- 'childrenAttributes' => array(),
- 'extras' => array(),
- 'display' => true,
- 'displayChildren' => true,
- 'children' => array(),
- ),
- ),
- ),
- 'joe' => array(
- 'name' => 'joe',
- 'label' => 'test',
- 'uri' => null,
- 'attributes' => array('class' => 'leaf'),
- 'labelAttributes' => array('class' => 'center'),
- 'linkAttributes' => array(),
- 'childrenAttributes' => array(),
- 'extras' => array(),
- 'display' => true,
- 'displayChildren' => false,
- 'children' => array(),
- ),
- ),
- ),
- $menu->toArray()
- );
- }
-
- public function testToArrayWithLimitedChildren()
- {
- $menu = $this->createMenu();
- $menu->addChild('jack', array('uri' => 'http://php.net', 'linkAttributes' => array('title' => 'php'), 'display' => false))
- ->addChild('john')
- ;
- $menu->addChild('joe', array('attributes' => array('class' => 'leaf'), 'label' => 'test', 'labelAttributes' => array('class' => 'center'), 'displayChildren' => false));
-
- $this->assertEquals(
- array(
- 'name' => 'test_menu',
- 'label' => null,
- 'uri' => 'homepage',
- 'attributes' => array(),
- 'labelAttributes' => array(),
- 'linkAttributes' => array(),
- 'childrenAttributes' => array(),
- 'extras' => array(),
- 'display' => true,
- 'displayChildren' => true,
- 'children' => array(
- 'jack' => array(
- 'name' => 'jack',
- 'label' => null,
- 'uri' => 'http://php.net',
- 'attributes' => array(),
- 'labelAttributes' => array(),
- 'linkAttributes' => array('title' => 'php'),
- 'childrenAttributes' => array(),
- 'extras' => array(),
- 'display' => false,
- 'displayChildren' => true,
- ),
- 'joe' => array(
- 'name' => 'joe',
- 'label' => 'test',
- 'uri' => null,
- 'attributes' => array('class' => 'leaf'),
- 'labelAttributes' => array('class' => 'center'),
- 'linkAttributes' => array(),
- 'childrenAttributes' => array(),
- 'extras' => array(),
- 'display' => true,
- 'displayChildren' => false,
- ),
- ),
- ),
- $menu->toArray(1)
- );
- }
-
- public function testToArrayWithoutChildren()
- {
- $menu = $this->createMenu();
- $menu->addChild('jack', array('uri' => 'http://php.net', 'linkAttributes' => array('title' => 'php'), 'display' => false));
- $menu->addChild('joe', array('attributes' => array('class' => 'leaf'), 'label' => 'test', 'labelAttributes' => array('class' => 'center'), 'displayChildren' => false));
-
- $this->assertEquals(
- array(
- 'name' => 'test_menu',
- 'label' => null,
- 'uri' => 'homepage',
- 'attributes' => array(),
- 'labelAttributes' => array(),
- 'linkAttributes' => array(),
- 'childrenAttributes' => array(),
- 'extras' => array(),
- 'display' => true,
- 'displayChildren' => true,
- ),
- $menu->toArray(0)
- );
- }
-
- public function testCallRecursively()
- {
- $menu = $this->createMenu();
- $child1 = $this->getMock('Knp\Menu\ItemInterface');
- $child1->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('Child 1'))
- ;
- $child1->expects($this->once())
- ->method('callRecursively')
- ->with('setDisplay', array(false))
- ;
- $menu->addChild($child1);
- $child2 = $this->getMock('Knp\Menu\ItemInterface');
- $child2->expects($this->any())
- ->method('getName')
- ->will($this->returnValue('Child 2'))
- ;
- $child2->expects($this->once())
- ->method('callRecursively')
- ->with('setDisplay', array(false))
- ;
- $menu->addChild($child2);
-
- $menu->callRecursively('setDisplay', array(false));
- $this->assertFalse($menu->isDisplayed());
- }
-
- public function testFactory()
- {
- $child1 = $this->getMock('Knp\Menu\ItemInterface');
- $factory = $this->getMock('Knp\Menu\FactoryInterface');
- $factory->expects($this->once())
- ->method('createItem')
- ->will($this->returnValue($child1));
-
- $menu = $this->createMenu();
- $menu->setFactory($factory);
-
- $menu->addChild('child1');
- }
-
- /**
- * Create a new MenuItem
- *
- * @param string $name
- * @param string $uri
- * @param array $attributes
- * @return \Knp\Menu\MenuItem
- */
- protected function createMenu($name = 'test_menu', $uri = 'homepage', array $attributes = array())
- {
- $factory = new MenuFactory();
-
- return $factory->createItem($name, array('attributes' => $attributes, 'uri' => $uri));
- }
- }
|