RoutingExtensionTest.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Knp\Menu\Tests\Silex;
  3. use Knp\Menu\Silex\RoutingExtension;
  4. class RoutingExtensionTest extends \PHPUnit_Framework_TestCase
  5. {
  6. public function setUp()
  7. {
  8. if (!interface_exists('Symfony\Component\Routing\Generator\UrlGeneratorInterface')) {
  9. $this->markTestSkipped('The Symfony2 Routing component is not available');
  10. }
  11. }
  12. public function testCreateItemWithRoute()
  13. {
  14. $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
  15. $generator->expects($this->once())
  16. ->method('generate')
  17. ->with('homepage', array(), false)
  18. ->will($this->returnValue('/foobar'))
  19. ;
  20. $extension = new RoutingExtension($generator);
  21. $processedOptions = $extension->buildOptions(array('uri' => '/hello', 'route' => 'homepage', 'label' => 'foo'));
  22. $this->assertEquals('/foobar', $processedOptions['uri']);
  23. $this->assertEquals('foo', $processedOptions['label']);
  24. }
  25. public function testCreateItemWithRouteAndParameters()
  26. {
  27. $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
  28. $generator->expects($this->once())
  29. ->method('generate')
  30. ->with('homepage', array('id' => 12), false)
  31. ->will($this->returnValue('/foobar'))
  32. ;
  33. $extension = new RoutingExtension($generator);
  34. $processedOptions = $extension->buildOptions(array('route' => 'homepage', 'routeParameters' => array('id' => 12)));
  35. $this->assertEquals('/foobar', $processedOptions['uri']);
  36. }
  37. public function testCreateItemWithAbsoluteRoute()
  38. {
  39. $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
  40. $generator->expects($this->once())
  41. ->method('generate')
  42. ->with('homepage', array(), true)
  43. ->will($this->returnValue('http://php.net'))
  44. ;
  45. $extension = new RoutingExtension($generator);
  46. $processedOptions = $extension->buildOptions(array('route' => 'homepage', 'routeAbsolute' => true));
  47. $this->assertEquals('http://php.net', $processedOptions['uri']);
  48. }
  49. public function testCreateItemAppendsRouteUnderExtras()
  50. {
  51. $generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
  52. $extension = new RoutingExtension($generator);
  53. $processedOptions = $extension->buildOptions( array('route' => 'homepage'));
  54. $this->assertEquals(array(array('route' => 'homepage', 'parameters' => array())), $processedOptions['extras']['routes']);
  55. $processedOptions = $extension->buildOptions( array('route' => 'homepage', 'routeParameters' => array('bar' => 'baz')));
  56. $this->assertEquals(array(array('route' => 'homepage', 'parameters' => array('bar' => 'baz'))), $processedOptions['extras']['routes']);
  57. $processedOptions = $extension->buildOptions( array('route' => 'homepage', 'extras' => array('routes' => array('other_page'))));
  58. $this->assertContains(array('route' => 'homepage', 'parameters' => array()), $processedOptions['extras']['routes']);
  59. $this->assertContains('other_page', $processedOptions['extras']['routes']);
  60. }
  61. }