MenuExtensionTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace Knp\Menu\Tests\Twig;
  3. use Knp\Menu\Twig\MenuExtension;
  4. class MenuExtensionTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function setUp()
  7. {
  8. if (!class_exists('Twig_Environment')) {
  9. $this->markTestSkipped('Twig is not available');
  10. }
  11. }
  12. public function testRenderMenu()
  13. {
  14. $menu = $this->getMock('Knp\Menu\ItemInterface');
  15. $helper = $this->getHelperMock(array('render'));
  16. $helper->expects($this->once())
  17. ->method('render')
  18. ->with($menu, array(), null)
  19. ->will($this->returnValue('<p>foobar</p>'))
  20. ;
  21. $this->assertEquals('<p>foobar</p>', $this->getTemplate('{{ knp_menu_render(menu) }}', $helper)->render(array('menu' => $menu)));
  22. }
  23. public function testRenderMenuWithOptions()
  24. {
  25. $menu = $this->getMock('Knp\Menu\ItemInterface');
  26. $helper = $this->getHelperMock(array('render'));
  27. $helper->expects($this->once())
  28. ->method('render')
  29. ->with($menu, array('firstClass' => 'test'), null)
  30. ->will($this->returnValue('<p>foobar</p>'))
  31. ;
  32. $this->assertEquals('<p>foobar</p>', $this->getTemplate('{{ knp_menu_render(menu, {"firstClass": "test"}) }}', $helper)->render(array('menu' => $menu)));
  33. }
  34. public function testRenderMenuWithRenderer()
  35. {
  36. $menu = $this->getMock('Knp\Menu\ItemInterface');
  37. $helper = $this->getHelperMock(array('render'));
  38. $helper->expects($this->once())
  39. ->method('render')
  40. ->with($menu, array(), 'custom')
  41. ->will($this->returnValue('<p>foobar</p>'))
  42. ;
  43. $this->assertEquals('<p>foobar</p>', $this->getTemplate('{{ knp_menu_render(menu, {}, "custom") }}', $helper)->render(array('menu' => $menu)));
  44. }
  45. public function testRenderMenuByName()
  46. {
  47. $helper = $this->getHelperMock(array('render'));
  48. $helper->expects($this->once())
  49. ->method('render')
  50. ->with('default', array(), null)
  51. ->will($this->returnValue('<p>foobar</p>'))
  52. ;
  53. $this->assertEquals('<p>foobar</p>', $this->getTemplate('{{ knp_menu_render(menu) }}', $helper)->render(array('menu' => 'default')));
  54. }
  55. public function testGetMenu()
  56. {
  57. $menu = $this->getMock('Knp\Menu\ItemInterface');
  58. $helper = $this->getHelperMock(array('get'));
  59. $helper->expects($this->once())
  60. ->method('get')
  61. ->with('default')
  62. ->will($this->returnValue($menu))
  63. ;
  64. $extension = new MenuExtension($helper);
  65. $this->assertSame($menu, $extension->get('default'));
  66. }
  67. public function testGetMenuWithOptions()
  68. {
  69. $menu = $this->getMock('Knp\Menu\ItemInterface');
  70. $helper = $this->getHelperMock(array('get'));
  71. $helper->expects($this->once())
  72. ->method('get')
  73. ->with('default', array(), array('foo' => 'bar'))
  74. ->will($this->returnValue($menu))
  75. ;
  76. $extension = new MenuExtension($helper);
  77. $this->assertSame($menu, $extension->get('default', array(), array('foo' => 'bar')));
  78. }
  79. public function testGetMenuByPath()
  80. {
  81. $menu = $this->getMock('Knp\Menu\ItemInterface');
  82. $helper = $this->getHelperMock(array('get'));
  83. $helper->expects($this->once())
  84. ->method('get')
  85. ->with('default', array('child'))
  86. ->will($this->returnValue($menu))
  87. ;
  88. $extension = new MenuExtension($helper);
  89. $this->assertSame($menu, $extension->get('default', array('child')));
  90. }
  91. public function testRetrieveMenuByName()
  92. {
  93. $menu = $this->getMock('Knp\Menu\ItemInterface');
  94. $helper = $this->getHelperMock(array('get', 'render'));
  95. $helper->expects($this->once())
  96. ->method('render')
  97. ->with($menu, array(), null)
  98. ->will($this->returnValue('<p>foobar</p>'))
  99. ;
  100. $helper->expects($this->once())
  101. ->method('get')
  102. ->with('default')
  103. ->will($this->returnValue($menu))
  104. ;
  105. $this->assertEquals('<p>foobar</p>', $this->getTemplate('{{ knp_menu_render(knp_menu_get("default")) }}', $helper)->render(array()));
  106. }
  107. private function getHelperMock(array $methods)
  108. {
  109. return $this->getMockBuilder('Knp\Menu\Twig\Helper')
  110. ->disableOriginalConstructor()
  111. ->setMethods($methods)
  112. ->getMock()
  113. ;
  114. }
  115. /**
  116. * @param string $template
  117. * @param \Knp\Menu\Twig\Helper $helper
  118. *
  119. * @return \Twig_Template
  120. */
  121. private function getTemplate($template, $helper)
  122. {
  123. $loader = new \Twig_Loader_Array(array('index' => $template));
  124. $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
  125. $twig->addExtension(new MenuExtension($helper));
  126. return $twig->loadTemplate('index');
  127. }
  128. }