MenuItemGetterSetterTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace Knp\Menu\Tests;
  3. use Knp\Menu\MenuItem;
  4. use Knp\Menu\MenuFactory;
  5. class MenuItemGetterSetterTest extends \PHPUnit_Framework_TestCase
  6. {
  7. public function testCreateMenuItemWithEmptyParameter()
  8. {
  9. $menu = $this->createMenu();
  10. $this->assertTrue($menu instanceof MenuItem);
  11. }
  12. public function testCreateMenuWithNameAndUri()
  13. {
  14. $menu = $this->createMenu('test1', 'other_uri');
  15. $this->assertEquals('test1', $menu->getName());
  16. $this->assertEquals('other_uri', $menu->getUri());
  17. }
  18. public function testCreateMenuWithTitle()
  19. {
  20. $title = 'This is a test item title';
  21. $menu = $this->createMenu(null, null, array('title' => $title));
  22. $this->assertEquals($title, $menu->getAttribute('title'));
  23. }
  24. public function testName()
  25. {
  26. $menu = $this->createMenu();
  27. $menu->setName('menu name');
  28. $this->assertEquals('menu name', $menu->getName());
  29. }
  30. public function testLabel()
  31. {
  32. $menu = $this->createMenu();
  33. $menu->setLabel('menu label');
  34. $this->assertEquals('menu label', $menu->getLabel());
  35. }
  36. public function testNameIsUsedAsDefaultLabel()
  37. {
  38. $menu = $this->createMenu('My Label');
  39. $this->assertEquals('My Label', $menu->getLabel());
  40. $menu->setLabel('Other Label');
  41. $this->assertEquals('Other Label', $menu->getLabel());
  42. }
  43. public function testUri()
  44. {
  45. $menu = $this->createMenu();
  46. $menu->setUri('menu_uri');
  47. $this->assertEquals('menu_uri', $menu->getUri());
  48. }
  49. public function testAttributes()
  50. {
  51. $attributes = array('class' => 'test_class', 'title' => 'Test title');
  52. $menu = $this->createMenu();
  53. $menu->setAttributes($attributes);
  54. $this->assertEquals($attributes, $menu->getAttributes());
  55. }
  56. public function testDefaultAttribute()
  57. {
  58. $menu = $this->createMenu(null, null, array('id' => 'test_id'));
  59. $this->assertEquals('test_id', $menu->getAttribute('id'));
  60. $this->assertEquals('default_value', $menu->getAttribute('unknown_attribute', 'default_value'));
  61. }
  62. public function testLinkAttributes()
  63. {
  64. $attributes = array('class' => 'test_class', 'title' => 'Test title');
  65. $menu = $this->createMenu();
  66. $menu->setLinkAttributes($attributes);
  67. $this->assertEquals($attributes, $menu->getLinkAttributes());
  68. }
  69. public function testDefaultLinkAttribute()
  70. {
  71. $menu = $this->createMenu();
  72. $menu->setLinkAttribute('class', 'test_class');
  73. $this->assertEquals('test_class', $menu->getLinkAttribute('class'));
  74. $this->assertNull($menu->getLinkAttribute('title'));
  75. $this->assertEquals('foobar', $menu->getLinkAttribute('title', 'foobar'));
  76. }
  77. public function testChildrenAttributes()
  78. {
  79. $attributes = array('class' => 'test_class', 'title' => 'Test title');
  80. $menu = $this->createMenu();
  81. $menu->setChildrenAttributes($attributes);
  82. $this->assertEquals($attributes, $menu->getChildrenAttributes());
  83. }
  84. public function testDefaultChildrenAttribute()
  85. {
  86. $menu = $this->createMenu();
  87. $menu->setChildrenAttribute('class', 'test_class');
  88. $this->assertEquals('test_class', $menu->getChildrenAttribute('class'));
  89. $this->assertNull($menu->getChildrenAttribute('title'));
  90. $this->assertEquals('foobar', $menu->getChildrenAttribute('title', 'foobar'));
  91. }
  92. public function testLabelAttributes()
  93. {
  94. $attributes = array('class' => 'test_class', 'title' => 'Test title');
  95. $menu = $this->createMenu();
  96. $menu->setLabelAttributes($attributes);
  97. $this->assertEquals($attributes, $menu->getLabelAttributes());
  98. }
  99. public function testDefaultLabelAttribute()
  100. {
  101. $menu = $this->createMenu();
  102. $menu->setLabelAttribute('class', 'test_class');
  103. $this->assertEquals('test_class', $menu->getLabelAttribute('class'));
  104. $this->assertNull($menu->getLabelAttribute('title'));
  105. $this->assertEquals('foobar', $menu->getLabelAttribute('title', 'foobar'));
  106. }
  107. public function testExtras()
  108. {
  109. $extras = array('class' => 'test_class', 'title' => 'Test title');
  110. $menu = $this->createMenu();
  111. $menu->setExtras($extras);
  112. $this->assertEquals($extras, $menu->getExtras());
  113. }
  114. public function testDefaultExtras()
  115. {
  116. $menu = $this->createMenu();
  117. $menu->setExtra('class', 'test_class');
  118. $this->assertEquals('test_class', $menu->getExtra('class'));
  119. $this->assertNull($menu->getExtra('title'));
  120. $this->assertEquals('foobar', $menu->getExtra('title', 'foobar'));
  121. }
  122. public function testDisplay()
  123. {
  124. $menu = $this->createMenu();
  125. $this->assertEquals(true, $menu->isDisplayed());
  126. $menu->setDisplay(false);
  127. $this->assertEquals(false, $menu->isDisplayed());
  128. }
  129. public function testShowChildren()
  130. {
  131. $menu = $this->createMenu();
  132. $this->assertEquals(true, $menu->getDisplayChildren());
  133. $menu->setDisplayChildren(false);
  134. $this->assertEquals(false, $menu->getDisplayChildren());
  135. }
  136. public function testParent()
  137. {
  138. $menu = $this->createMenu();
  139. $child = $this->createMenu('child_menu');
  140. $this->assertNull($child->getParent());
  141. $child->setParent($menu);
  142. $this->assertEquals($menu, $child->getParent());
  143. }
  144. public function testChildren()
  145. {
  146. $menu = $this->createMenu();
  147. $child = $this->createMenu('child_menu');
  148. $menu->setChildren(array($child));
  149. $this->assertEquals(array($child), $menu->getChildren());
  150. }
  151. /**
  152. * @expectedException \InvalidArgumentException
  153. */
  154. public function testSetExistingNameThrowsAnException()
  155. {
  156. $menu = $this->createMenu();
  157. $menu->addChild('jack');
  158. $menu->addChild('joe');
  159. $menu->getChild('joe')->setName('jack');
  160. }
  161. public function testSetSameName()
  162. {
  163. $parent = $this->getMock('Knp\Menu\ItemInterface');
  164. $parent->expects($this->never())
  165. ->method('offsetExists');
  166. $menu = $this->createMenu('my_name');
  167. $menu->setParent($parent);
  168. $menu->setName('my_name');
  169. $this->assertEquals('my_name', $menu->getName());
  170. }
  171. public function testFactory()
  172. {
  173. $child1 = $this->getMock('Knp\Menu\ItemInterface');
  174. $factory = $this->getMock('Knp\Menu\FactoryInterface');
  175. $factory->expects($this->once())
  176. ->method('createItem')
  177. ->will($this->returnValue($child1));
  178. $menu = $this->createMenu();
  179. $menu->setFactory($factory);
  180. $menu->addChild('child1');
  181. }
  182. /**
  183. * Create a new MenuItem
  184. *
  185. * @param string $name
  186. * @param string $uri
  187. * @param array $attributes
  188. *
  189. * @return \Knp\Menu\MenuItem
  190. */
  191. protected function createMenu($name = 'test_menu', $uri = 'homepage', array $attributes = array())
  192. {
  193. $factory = new MenuFactory();
  194. return $factory->createItem($name, array('attributes' => $attributes, 'uri' => $uri));
  195. }
  196. }