MenuItemTreeTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Knp\Menu\Tests;
  3. use Knp\Menu\MenuItem;
  4. class TestMenuItem extends MenuItem {}
  5. class MenuItemTreeTest extends TestCase
  6. {
  7. public function testSampleTreeIntegrity()
  8. {
  9. $this->assertCount(2, $this->menu);
  10. $this->assertCount(3, $this->menu['Parent 1']);
  11. $this->assertCount(1, $this->menu['Parent 2']);
  12. $this->assertCount(1, $this->menu['Parent 2']['Child 4']);
  13. $this->assertEquals('Grandchild 1', $this->menu['Parent 2']['Child 4']['Grandchild 1']->getName());
  14. }
  15. public function testGetLevel()
  16. {
  17. $this->assertEquals(0, $this->menu->getLevel());
  18. $this->assertEquals(1, $this->pt1->getLevel());
  19. $this->assertEquals(1, $this->pt2->getLevel());
  20. $this->assertEquals(2, $this->ch4->getLevel());
  21. $this->assertEquals(3, $this->gc1->getLevel());
  22. }
  23. public function testGetRoot()
  24. {
  25. $this->assertSame($this->menu, $this->menu->getRoot());
  26. $this->assertSame($this->menu, $this->pt1->getRoot());
  27. $this->assertSame($this->menu, $this->gc1->getRoot());
  28. }
  29. public function testIsRoot()
  30. {
  31. $this->assertTrue($this->menu->isRoot());
  32. $this->assertFalse($this->pt1->isRoot());
  33. $this->assertFalse($this->ch3->isRoot());
  34. }
  35. public function testGetParent()
  36. {
  37. $this->assertNull($this->menu->getParent());
  38. $this->assertSame($this->menu, $this->pt1->getParent());
  39. $this->assertSame($this->ch4, $this->gc1->getParent());
  40. }
  41. public function testMoveSampleMenuToNewRoot()
  42. {
  43. $newRoot = new TestMenuItem("newRoot", $this->getMock('Knp\Menu\FactoryInterface'));
  44. $newRoot->addChild($this->menu);
  45. $this->assertEquals(1, $this->menu->getLevel());
  46. $this->assertEquals(2, $this->pt1->getLevel());
  47. $this->assertSame($newRoot, $this->menu->getRoot());
  48. $this->assertSame($newRoot, $this->pt1->getRoot());
  49. $this->assertFalse($this->menu->isRoot());
  50. $this->assertTrue($newRoot->isRoot());
  51. $this->assertSame($newRoot, $this->menu->getParent());
  52. }
  53. public function testIsFirst()
  54. {
  55. $this->assertFalse($this->menu->isFirst(), 'The root item is not considered as first');
  56. $this->assertTrue($this->pt1->isFirst());
  57. $this->assertFalse($this->pt2->isFirst());
  58. $this->assertTrue($this->ch4->isFirst());
  59. }
  60. public function testActsLikeFirst()
  61. {
  62. $this->ch1->setDisplay(false);
  63. $this->assertFalse($this->menu->actsLikeFirst(), 'The root item is not considered as first');
  64. $this->assertFalse($this->ch1->actsLikeFirst(), 'A hidden item does not acts like first');
  65. $this->assertTrue($this->ch2->actsLikeFirst());
  66. $this->assertFalse($this->ch3->actsLikeFirst());
  67. $this->assertTrue($this->ch4->actsLikeFirst());
  68. }
  69. public function testActsLikeFirstWithNoDisplayedItem()
  70. {
  71. $this->pt1->setDisplay(false);
  72. $this->pt2->setDisplay(false);
  73. $this->assertFalse($this->pt1->actsLikeFirst());
  74. $this->assertFalse($this->pt2->actsLikeFirst());
  75. }
  76. public function testIsLast()
  77. {
  78. $this->assertFalse($this->menu->isLast(), 'The root item is not considered as last');
  79. $this->assertFalse($this->pt1->isLast());
  80. $this->assertTrue($this->pt2->isLast());
  81. $this->assertTrue($this->ch4->isLast());
  82. }
  83. public function testActsLikeLast()
  84. {
  85. $this->ch3->setDisplay(false);
  86. $this->assertFalse($this->menu->actsLikeLast(), 'The root item is not considered as last');
  87. $this->assertFalse($this->ch1->actsLikeLast());
  88. $this->assertTrue($this->ch2->actsLikeLast());
  89. $this->assertFalse($this->ch3->actsLikeLast(), 'A hidden item does not acts like last');
  90. $this->assertTrue($this->ch4->actsLikeLast());
  91. }
  92. public function testActsLikeLastWithNoDisplayedItem()
  93. {
  94. $this->pt1->setDisplay(false);
  95. $this->pt2->setDisplay(false);
  96. $this->assertFalse($this->pt1->actsLikeLast());
  97. $this->assertFalse($this->pt2->actsLikeLast());
  98. }
  99. public function testArrayAccess()
  100. {
  101. $this->menu->addChild('Child Menu');
  102. $this->assertEquals('Child Menu', $this->menu['Child Menu']->getName());
  103. $this->assertNull($this->menu['Fake']);
  104. $this->menu['New Child'] = 'New Label';
  105. $this->assertEquals('Knp\Menu\MenuItem', get_class($this->menu['New Child']));
  106. $this->assertEquals('New Child', $this->menu['New Child']->getName());
  107. $this->assertEquals('New Label', $this->menu['New Child']->getLabel());
  108. unset($this->menu['New Child']);
  109. $this->assertNull($this->menu['New Child']);
  110. }
  111. public function testCountable()
  112. {
  113. $this->assertCount(2, $this->menu);
  114. $this->menu->addChild('New Child');
  115. $this->assertCount(3, $this->menu);
  116. unset($this->menu['New Child']);
  117. $this->assertCount(2, $this->menu);
  118. }
  119. public function testGetChildren()
  120. {
  121. $children = $this->ch4->getChildren();
  122. $this->assertCount(1, $children);
  123. $this->assertEquals($this->gc1->getName(), $children['Grandchild 1']->getName());
  124. }
  125. public function testGetFirstChild()
  126. {
  127. $this->assertSame($this->pt1, $this->menu->getFirstChild());
  128. // test for bug in getFirstChild implementation (when internal array pointer is changed getFirstChild returns wrong child)
  129. foreach ($this->menu->getChildren() as $c);
  130. $this->assertSame($this->pt1, $this->menu->getFirstChild());
  131. }
  132. public function testGetLastChild()
  133. {
  134. $this->assertSame($this->pt2, $this->menu->getLastChild());
  135. // test for bug in getFirstChild implementation (when internal array pointer is changed getLastChild returns wrong child)
  136. foreach ($this->menu->getChildren() as $c);
  137. $this->assertSame($this->pt2, $this->menu->getLastChild());
  138. }
  139. public function testAddChildDoesNotUSeTheFactoryIfItem()
  140. {
  141. $factory = $this->getMock('Knp\Menu\FactoryInterface');
  142. $factory->expects($this->never())
  143. ->method('createItem');
  144. $menu = new MenuItem('Root li', $factory);
  145. $menu->addChild(new MenuItem('Child 3', $factory));
  146. }
  147. /**
  148. * @expectedException \LogicException
  149. */
  150. public function testAddChildFailsIfInAnotherMenu()
  151. {
  152. $factory = $this->getMock('Knp\Menu\FactoryInterface');
  153. $menu = new MenuItem('Root li', $factory);
  154. $child = new MenuItem('Child 3', $factory);
  155. $menu->addChild($child);
  156. $menu2 = new MenuItem('Second menu', $factory);
  157. $menu2->addChild($child);
  158. }
  159. public function testGetChild()
  160. {
  161. $this->assertSame($this->gc1, $this->ch4->getChild('Grandchild 1'));
  162. $this->assertNull($this->ch4->getChild('nonexistentchild'));
  163. }
  164. public function testRemoveChild()
  165. {
  166. $gc2 = $this->ch4->addChild('gc2');
  167. $gc3 = $this->ch4->addChild('gc3');
  168. $gc4 = $this->ch4->addChild('gc4');
  169. $this->assertCount(4, $this->ch4);
  170. $this->ch4->removeChild('gc4');
  171. $this->assertCount(3, $this->ch4);
  172. $this->assertTrue($this->ch4->getChild('Grandchild 1')->isFirst());
  173. $this->assertTrue($this->ch4->getChild('gc3')->isLast());
  174. }
  175. public function testRemoveFakeChild()
  176. {
  177. $this->menu->removeChild('fake');
  178. $this->assertCount(2, $this->menu);
  179. }
  180. public function testReAddRemovedChild()
  181. {
  182. $gc2 = $this->ch4->addChild('gc2');
  183. $this->ch4->removeChild('gc2');
  184. $this->menu->addChild($gc2);
  185. $this->assertCount(3, $this->menu);
  186. $this->assertTrue($gc2->isLast());
  187. $this->assertFalse($this->pt2->isLast());
  188. }
  189. public function testUpdateChildAfterRename()
  190. {
  191. $this->pt1->setName('Temp name');
  192. $this->assertSame($this->pt1, $this->menu->getChild('Temp name'));
  193. $this->assertEquals(array('Temp name', 'Parent 2'), array_keys($this->menu->getChildren()));
  194. $this->assertNull($this->menu->getChild('Parent 1'));
  195. }
  196. /**
  197. * @expectedException \InvalidArgumentException
  198. */
  199. public function testRenameToExistingSiblingNameThrowAnException()
  200. {
  201. $this->pt1->setName('Parent 2');
  202. }
  203. public function testGetUri()
  204. {
  205. $this->addChildWithExternalUrl();
  206. $this->assertNull($this->pt1->getUri());
  207. $this->assertEquals('http://www.symfony-reloaded.org', $this->menu['child']->getUri());
  208. }
  209. protected function addChildWithExternalUrl()
  210. {
  211. $this->menu->addChild('child', array('uri' => 'http://www.symfony-reloaded.org'));
  212. }
  213. }