4 * This file is part of the Symfony CMF package.
6 * (c) 2011-2015 Symfony CMF
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Cmf\Component\Routing\Tests\Routing;
14 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
15 use Symfony\Component\Routing\RequestContext;
16 use Symfony\Component\Routing\Route;
17 use Symfony\Cmf\Component\Routing\ProviderBasedGenerator;
18 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
20 class ProviderBasedGeneratorTest extends CmfUnitTestCase
22 protected $routeDocument;
23 protected $routeCompiled;
26 /** @var ProviderBasedGenerator */
30 public function setUp()
32 $this->routeDocument = $this->buildMock('Symfony\Component\Routing\Route', array('getDefaults', 'compile'));
33 $this->routeCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
34 $this->provider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
35 $this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
37 $this->generator = new TestableProviderBasedGenerator($this->provider);
40 public function testGenerateFromName()
44 $this->provider->expects($this->once())
45 ->method('getRouteByName')
47 ->will($this->returnValue($this->routeDocument))
49 $this->routeDocument->expects($this->once())
51 ->will($this->returnValue($this->routeCompiled))
54 $this->assertEquals('result_url', $this->generator->generate($name));
58 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
60 public function testGenerateNotFound()
64 $this->provider->expects($this->once())
65 ->method('getRouteByName')
67 ->will($this->returnValue(null))
70 $this->generator->generate($name);
73 public function testGenerateFromRoute()
75 $this->provider->expects($this->never())
76 ->method('getRouteByName')
78 $this->routeDocument->expects($this->once())
80 ->will($this->returnValue($this->routeCompiled))
83 $this->assertEquals('result_url', $this->generator->generate($this->routeDocument));
86 public function testSupports()
88 $this->assertTrue($this->generator->supports('foo/bar'));
89 $this->assertTrue($this->generator->supports($this->routeDocument));
90 $this->assertFalse($this->generator->supports($this));
93 public function testGetRouteDebugMessage()
95 $this->assertContains('/some/key', $this->generator->getRouteDebugMessage(new RouteObject()));
96 $this->assertContains('/de/test', $this->generator->getRouteDebugMessage(new Route('/de/test')));
97 $this->assertContains('/some/route', $this->generator->getRouteDebugMessage('/some/route'));
98 $this->assertContains('a:1:{s:10:"route_name";s:7:"example";}', $this->generator->getRouteDebugMessage(array('route_name' => 'example')));
102 * Tests the generate method with passing in a route object into generate().
104 * @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
106 public function testGenerateByRoute()
108 $this->generator = new ProviderBasedGenerator($this->provider);
110 // Setup a route with a numeric parameter, but pass in a string, so it
111 // fails and getRouteDebugMessage should be triggered.
112 $route = new Route('/test');
113 $route->setPath('/test/{number}');
114 $route->setRequirement('number', '\+d');
116 $this->generator->setStrictRequirements(true);
118 $context = new RequestContext();
119 $this->generator->setContext($context);
121 $this->assertSame(null, $this->generator->generate($route, array('number' => 'string')));
126 * Overwrite doGenerate to reduce amount of mocking needed.
128 class TestableProviderBasedGenerator extends ProviderBasedGenerator
130 protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
136 class RouteObject implements RouteObjectInterface
138 public function getRouteKey()
143 public function getContent()