MenuItemGetterSetterTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 testToArrayWithChildren()
  172. {
  173. $menu = $this->createMenu();
  174. $menu->addChild('jack', array('uri' => 'http://php.net', 'linkAttributes' => array('title' => 'php'), 'display' => false))
  175. ->addChild('john')
  176. ;
  177. $menu->addChild('joe', array('attributes' => array('class' => 'leaf'), 'label' => 'test', 'labelAttributes' => array('class' => 'center'), 'displayChildren' => false));
  178. $this->assertEquals(
  179. array(
  180. 'name' => 'test_menu',
  181. 'label' => null,
  182. 'uri' => 'homepage',
  183. 'attributes' => array(),
  184. 'labelAttributes' => array(),
  185. 'linkAttributes' => array(),
  186. 'childrenAttributes' => array(),
  187. 'extras' => array(),
  188. 'display' => true,
  189. 'displayChildren' => true,
  190. 'children' => array(
  191. 'jack' => array(
  192. 'name' => 'jack',
  193. 'label' => null,
  194. 'uri' => 'http://php.net',
  195. 'attributes' => array(),
  196. 'labelAttributes' => array(),
  197. 'linkAttributes' => array('title' => 'php'),
  198. 'childrenAttributes' => array(),
  199. 'extras' => array(),
  200. 'display' => false,
  201. 'displayChildren' => true,
  202. 'children' => array(
  203. 'john' => array(
  204. 'name' => 'john',
  205. 'label' => null,
  206. 'uri' => null,
  207. 'attributes' => array(),
  208. 'labelAttributes' => array(),
  209. 'linkAttributes' => array(),
  210. 'childrenAttributes' => array(),
  211. 'extras' => array(),
  212. 'display' => true,
  213. 'displayChildren' => true,
  214. 'children' => array(),
  215. ),
  216. ),
  217. ),
  218. 'joe' => array(
  219. 'name' => 'joe',
  220. 'label' => 'test',
  221. 'uri' => null,
  222. 'attributes' => array('class' => 'leaf'),
  223. 'labelAttributes' => array('class' => 'center'),
  224. 'linkAttributes' => array(),
  225. 'childrenAttributes' => array(),
  226. 'extras' => array(),
  227. 'display' => true,
  228. 'displayChildren' => false,
  229. 'children' => array(),
  230. ),
  231. ),
  232. ),
  233. $menu->toArray()
  234. );
  235. }
  236. public function testToArrayWithLimitedChildren()
  237. {
  238. $menu = $this->createMenu();
  239. $menu->addChild('jack', array('uri' => 'http://php.net', 'linkAttributes' => array('title' => 'php'), 'display' => false))
  240. ->addChild('john')
  241. ;
  242. $menu->addChild('joe', array('attributes' => array('class' => 'leaf'), 'label' => 'test', 'labelAttributes' => array('class' => 'center'), 'displayChildren' => false));
  243. $this->assertEquals(
  244. array(
  245. 'name' => 'test_menu',
  246. 'label' => null,
  247. 'uri' => 'homepage',
  248. 'attributes' => array(),
  249. 'labelAttributes' => array(),
  250. 'linkAttributes' => array(),
  251. 'childrenAttributes' => array(),
  252. 'extras' => array(),
  253. 'display' => true,
  254. 'displayChildren' => true,
  255. 'children' => array(
  256. 'jack' => array(
  257. 'name' => 'jack',
  258. 'label' => null,
  259. 'uri' => 'http://php.net',
  260. 'attributes' => array(),
  261. 'labelAttributes' => array(),
  262. 'linkAttributes' => array('title' => 'php'),
  263. 'childrenAttributes' => array(),
  264. 'extras' => array(),
  265. 'display' => false,
  266. 'displayChildren' => true,
  267. ),
  268. 'joe' => array(
  269. 'name' => 'joe',
  270. 'label' => 'test',
  271. 'uri' => null,
  272. 'attributes' => array('class' => 'leaf'),
  273. 'labelAttributes' => array('class' => 'center'),
  274. 'linkAttributes' => array(),
  275. 'childrenAttributes' => array(),
  276. 'extras' => array(),
  277. 'display' => true,
  278. 'displayChildren' => false,
  279. ),
  280. ),
  281. ),
  282. $menu->toArray(1)
  283. );
  284. }
  285. public function testToArrayWithoutChildren()
  286. {
  287. $menu = $this->createMenu();
  288. $menu->addChild('jack', array('uri' => 'http://php.net', 'linkAttributes' => array('title' => 'php'), 'display' => false));
  289. $menu->addChild('joe', array('attributes' => array('class' => 'leaf'), 'label' => 'test', 'labelAttributes' => array('class' => 'center'), 'displayChildren' => false));
  290. $this->assertEquals(
  291. array(
  292. 'name' => 'test_menu',
  293. 'label' => null,
  294. 'uri' => 'homepage',
  295. 'attributes' => array(),
  296. 'labelAttributes' => array(),
  297. 'linkAttributes' => array(),
  298. 'childrenAttributes' => array(),
  299. 'extras' => array(),
  300. 'display' => true,
  301. 'displayChildren' => true,
  302. ),
  303. $menu->toArray(0)
  304. );
  305. }
  306. public function testCallRecursively()
  307. {
  308. $menu = $this->createMenu();
  309. $child1 = $this->getMock('Knp\Menu\ItemInterface');
  310. $child1->expects($this->any())
  311. ->method('getName')
  312. ->will($this->returnValue('Child 1'))
  313. ;
  314. $child1->expects($this->once())
  315. ->method('callRecursively')
  316. ->with('setDisplay', array(false))
  317. ;
  318. $menu->addChild($child1);
  319. $child2 = $this->getMock('Knp\Menu\ItemInterface');
  320. $child2->expects($this->any())
  321. ->method('getName')
  322. ->will($this->returnValue('Child 2'))
  323. ;
  324. $child2->expects($this->once())
  325. ->method('callRecursively')
  326. ->with('setDisplay', array(false))
  327. ;
  328. $menu->addChild($child2);
  329. $menu->callRecursively('setDisplay', array(false));
  330. $this->assertFalse($menu->isDisplayed());
  331. }
  332. public function testFactory()
  333. {
  334. $child1 = $this->getMock('Knp\Menu\ItemInterface');
  335. $factory = $this->getMock('Knp\Menu\FactoryInterface');
  336. $factory->expects($this->once())
  337. ->method('createItem')
  338. ->will($this->returnValue($child1));
  339. $menu = $this->createMenu();
  340. $menu->setFactory($factory);
  341. $menu->addChild('child1');
  342. }
  343. /**
  344. * Create a new MenuItem
  345. *
  346. * @param string $name
  347. * @param string $uri
  348. * @param array $attributes
  349. * @return \Knp\Menu\MenuItem
  350. */
  351. protected function createMenu($name = 'test_menu', $uri = 'homepage', array $attributes = array())
  352. {
  353. $factory = new MenuFactory();
  354. return $factory->createItem($name, array('attributes' => $attributes, 'uri' => $uri));
  355. }
  356. }