HelperTest.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace Knp\Menu\Tests\Twig;
  3. use Knp\Menu\Twig\Helper;
  4. class HelperTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function testRenderMenu()
  7. {
  8. $menu = $this->getMock('Knp\Menu\ItemInterface');
  9. $renderer = $this->getMock('Knp\Menu\Renderer\RendererInterface');
  10. $renderer->expects($this->once())
  11. ->method('render')
  12. ->with($menu, array())
  13. ->will($this->returnValue('<p>foobar</p>'))
  14. ;
  15. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  16. $rendererProvider->expects($this->once())
  17. ->method('get')
  18. ->with(null)
  19. ->will($this->returnValue($renderer))
  20. ;
  21. $helper = new Helper($rendererProvider);
  22. $this->assertEquals('<p>foobar</p>', $helper->render($menu));
  23. }
  24. public function testRenderMenuWithOptions()
  25. {
  26. $menu = $this->getMock('Knp\Menu\ItemInterface');
  27. $renderer = $this->getMock('Knp\Menu\Renderer\RendererInterface');
  28. $renderer->expects($this->once())
  29. ->method('render')
  30. ->with($menu, array('firstClass' => 'test'))
  31. ->will($this->returnValue('<p>foobar</p>'))
  32. ;
  33. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  34. $rendererProvider->expects($this->once())
  35. ->method('get')
  36. ->with(null)
  37. ->will($this->returnValue($renderer))
  38. ;
  39. $helper = new Helper($rendererProvider);
  40. $this->assertEquals('<p>foobar</p>', $helper->render($menu, array('firstClass' => 'test')));
  41. }
  42. public function testRenderMenuWithRenderer()
  43. {
  44. $menu = $this->getMock('Knp\Menu\ItemInterface');
  45. $renderer = $this->getMock('Knp\Menu\Renderer\RendererInterface');
  46. $renderer->expects($this->once())
  47. ->method('render')
  48. ->with($menu, array())
  49. ->will($this->returnValue('<p>foobar</p>'))
  50. ;
  51. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  52. $rendererProvider->expects($this->once())
  53. ->method('get')
  54. ->with('custom')
  55. ->will($this->returnValue($renderer))
  56. ;
  57. $helper = new Helper($rendererProvider);
  58. $this->assertEquals('<p>foobar</p>', $helper->render($menu, array(), 'custom'));
  59. }
  60. public function testRenderMenuByName()
  61. {
  62. $menu = $this->getMock('Knp\Menu\ItemInterface');
  63. $menuProvider = $this->getMock('Knp\Menu\Provider\MenuProviderInterface');
  64. $menuProvider->expects($this->once())
  65. ->method('get')
  66. ->with('default')
  67. ->will($this->returnValue($menu))
  68. ;
  69. $renderer = $this->getMock('Knp\Menu\Renderer\RendererInterface');
  70. $renderer->expects($this->once())
  71. ->method('render')
  72. ->with($menu, array())
  73. ->will($this->returnValue('<p>foobar</p>'))
  74. ;
  75. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  76. $rendererProvider->expects($this->once())
  77. ->method('get')
  78. ->with(null)
  79. ->will($this->returnValue($renderer))
  80. ;
  81. $helper = new Helper($rendererProvider, $menuProvider);
  82. $this->assertEquals('<p>foobar</p>', $helper->render('default'));
  83. }
  84. public function testGetMenu()
  85. {
  86. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  87. $menuProvider = $this->getMock('Knp\Menu\Provider\MenuProviderInterface');
  88. $menu = $this->getMock('Knp\Menu\ItemInterface');
  89. $menuProvider->expects($this->once())
  90. ->method('get')
  91. ->with('default')
  92. ->will($this->returnValue($menu))
  93. ;
  94. $helper = new Helper($rendererProvider, $menuProvider);
  95. $this->assertSame($menu, $helper->get('default'));
  96. }
  97. /**
  98. * @expectedException LogicException
  99. */
  100. public function testGetMenuWithBadReturnValue()
  101. {
  102. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  103. $menuProvider = $this->getMock('Knp\Menu\Provider\MenuProviderInterface');
  104. $menuProvider->expects($this->once())
  105. ->method('get')
  106. ->with('default')
  107. ->will($this->returnValue(new \stdClass()))
  108. ;
  109. $helper = new Helper($rendererProvider, $menuProvider);
  110. $helper->get('default');
  111. }
  112. /**
  113. * @expectedException BadMethodCallException
  114. */
  115. public function testGetMenuWithoutProvider()
  116. {
  117. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  118. $helper = new Helper($rendererProvider);
  119. $helper->get('default');
  120. }
  121. public function testGetMenuWithOptions()
  122. {
  123. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  124. $menuProvider = $this->getMock('Knp\Menu\Provider\MenuProviderInterface');
  125. $menu = $this->getMock('Knp\Menu\ItemInterface');
  126. $menuProvider->expects($this->once())
  127. ->method('get')
  128. ->with('default', array('foo' => 'bar'))
  129. ->will($this->returnValue($menu))
  130. ;
  131. $helper = new Helper($rendererProvider, $menuProvider);
  132. $this->assertSame($menu, $helper->get('default', array(), array('foo' => 'bar')));
  133. }
  134. public function testGetMenuByPath()
  135. {
  136. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  137. $menuProvider = $this->getMock('Knp\Menu\Provider\MenuProviderInterface');
  138. $child = $this->getMock('Knp\Menu\ItemInterface');
  139. $menu = $this->getMock('Knp\Menu\ItemInterface');
  140. $menu->expects($this->any())
  141. ->method('getChild')
  142. ->with('child')
  143. ->will($this->returnValue($child))
  144. ;
  145. $menuProvider->expects($this->once())
  146. ->method('get')
  147. ->with('default')
  148. ->will($this->returnValue($menu))
  149. ;
  150. $helper = new Helper($rendererProvider, $menuProvider);
  151. $this->assertSame($child, $helper->get('default', array('child')));
  152. }
  153. /**
  154. * @expectedException InvalidArgumentException
  155. */
  156. public function testGetMenuByInvalidPath()
  157. {
  158. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  159. $menuProvider = $this->getMock('Knp\Menu\Provider\MenuProviderInterface');
  160. $child = $this->getMock('Knp\Menu\ItemInterface');
  161. $child->expects($this->any())
  162. ->method('getChild')
  163. ->will($this->returnValue(null))
  164. ;
  165. $menu = $this->getMock('Knp\Menu\ItemInterface');
  166. $menu->expects($this->any())
  167. ->method('getChild')
  168. ->with('child')
  169. ->will($this->returnValue($child))
  170. ;
  171. $menuProvider->expects($this->once())
  172. ->method('get')
  173. ->with('default')
  174. ->will($this->returnValue($menu))
  175. ;
  176. $helper = new Helper($rendererProvider, $menuProvider);
  177. $this->assertSame($child, $helper->get('default', array('child', 'invalid')));
  178. }
  179. public function testRenderMenuByPath()
  180. {
  181. $child = $this->getMock('Knp\Menu\ItemInterface');
  182. $menu = $this->getMock('Knp\Menu\ItemInterface');
  183. $menu->expects($this->any())
  184. ->method('getChild')
  185. ->with('child')
  186. ->will($this->returnValue($child))
  187. ;
  188. $renderer = $this->getMock('Knp\Menu\Renderer\RendererInterface');
  189. $renderer->expects($this->once())
  190. ->method('render')
  191. ->with($child, array())
  192. ->will($this->returnValue('<p>foobar</p>'))
  193. ;
  194. $rendererProvider = $this->getMock('Knp\Menu\Renderer\RendererProviderInterface');
  195. $rendererProvider->expects($this->once())
  196. ->method('get')
  197. ->with(null)
  198. ->will($this->returnValue($renderer))
  199. ;
  200. $helper = new Helper($rendererProvider);
  201. $this->assertEquals('<p>foobar</p>', $helper->render(array($menu, 'child')));
  202. }
  203. /**
  204. * @expectedException InvalidArgumentException
  205. * @expectedExceptionMessage The array cannot be empty
  206. */
  207. public function testRenderByEmptyPath()
  208. {
  209. $helper = new Helper($this->getMock('Knp\Menu\Renderer\RendererProviderInterface'));
  210. $helper->render(array());
  211. }
  212. }