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\RouteReferrersReadInterface;
15 use Symfony\Cmf\Component\Routing\ContentAwareGenerator;
16 use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
18 class ContentAwareGeneratorTest extends CmfUnitTestCase
20 protected $contentDocument;
21 protected $routeDocument;
22 protected $routeCompiled;
26 * @var ContentAwareGenerator
31 public function setUp()
33 $this->contentDocument = $this->buildMock('Symfony\Cmf\Component\Routing\RouteReferrersReadInterface');
34 $this->routeDocument = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
35 $this->routeCompiled = $this->buildMock('Symfony\Component\Routing\CompiledRoute');
36 $this->provider = $this->buildMock('Symfony\Cmf\Component\Routing\RouteProviderInterface');
37 $this->context = $this->buildMock('Symfony\Component\Routing\RequestContext');
39 $this->generator = new TestableContentAwareGenerator($this->provider);
42 public function testGenerateFromContent()
44 $this->provider->expects($this->never())
45 ->method('getRouteByName')
47 $this->contentDocument->expects($this->once())
49 ->will($this->returnValue(array($this->routeDocument)))
51 $this->routeDocument->expects($this->once())
53 ->will($this->returnValue($this->routeCompiled))
56 $this->assertEquals('result_url', $this->generator->generate($this->contentDocument));
59 public function testGenerateFromContentId()
61 $this->provider->expects($this->never())
62 ->method('getRouteByName')
65 $contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
66 $contentRepository->expects($this->once())
69 ->will($this->returnValue($this->contentDocument))
71 $this->generator->setContentRepository($contentRepository);
73 $this->contentDocument->expects($this->once())
75 ->will($this->returnValue(array($this->routeDocument)))
78 $this->routeDocument->expects($this->once())
80 ->will($this->returnValue($this->routeCompiled))
83 $this->assertEquals('result_url', $this->generator->generate('', array('content_id' => '/content/id')));
86 public function testGenerateEmptyRouteString()
88 $this->provider->expects($this->never())
89 ->method('getRouteByName')
92 $this->contentDocument->expects($this->once())
94 ->will($this->returnValue(array($this->routeDocument)))
97 $this->routeDocument->expects($this->once())
99 ->will($this->returnValue($this->routeCompiled))
102 $this->assertEquals('result_url', $this->generator->generate($this->contentDocument));
105 public function testGenerateRouteMultilang()
107 $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
108 $route_en->setLocale('en');
109 $route_de = $this->routeDocument;
110 $route_de->setLocale('de');
112 $this->contentDocument->expects($this->once())
113 ->method('getRoutes')
114 ->will($this->returnValue(array($route_en, $route_de)))
116 $route_en->expects($this->once())
117 ->method('getContent')
118 ->will($this->returnValue($this->contentDocument))
120 $route_en->expects($this->never())
123 $route_de->expects($this->once())
125 ->will($this->returnValue($this->routeCompiled))
128 $this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'de')));
131 public function testGenerateRouteMultilangDefaultLocale()
133 $route = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock');
134 $route->expects($this->any())
136 ->will($this->returnValue($this->routeCompiled))
138 $route->expects($this->any())
139 ->method('getRequirement')
141 ->will($this->returnValue('de|en'))
143 $route->expects($this->any())
144 ->method('getDefault')
146 ->will($this->returnValue('en'))
148 $this->routeCompiled->expects($this->any())
149 ->method('getVariables')
150 ->will($this->returnValue(array()))
153 $this->assertEquals('result_url', $this->generator->generate($route, array('_locale' => 'en')));
156 public function testGenerateRouteMultilangLocaleNomatch()
158 $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
159 $route_en->setLocale('en');
160 $route_de = $this->routeDocument;
161 $route_de->setLocale('de');
163 $this->contentDocument->expects($this->once())
164 ->method('getRoutes')
165 ->will($this->returnValue(array($route_en, $route_de)))
167 $route_en->expects($this->once())
168 ->method('getContent')
169 ->will($this->returnValue($this->contentDocument))
171 $route_en->expects($this->once())
173 ->will($this->returnValue($this->routeCompiled))
175 $route_de->expects($this->never())
179 $this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'fr')));
182 public function testGenerateNoncmfRouteMultilang()
184 $route_en = $this->buildMock('Symfony\Component\Routing\Route', array('getDefaults', 'compile', 'getContent'));
186 $route_en->expects($this->once())
188 ->will($this->returnValue($this->routeCompiled))
191 $this->assertEquals('result_url', $this->generator->generate($route_en, array('_locale' => 'de')));
194 public function testGenerateRoutenameMultilang()
197 $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile', 'getContent'));
198 $route_en->setLocale('en');
199 $route_de = $this->routeDocument;
200 $route_de->setLocale('de');
202 $this->provider->expects($this->once())
203 ->method('getRouteByName')
205 ->will($this->returnValue($route_en))
207 $this->contentDocument->expects($this->once())
208 ->method('getRoutes')
209 ->will($this->returnValue(array($route_en, $route_de)))
211 $route_en->expects($this->once())
212 ->method('getContent')
213 ->will($this->returnValue($this->contentDocument))
215 $route_en->expects($this->never())
218 $route_de->expects($this->once())
220 ->will($this->returnValue($this->routeCompiled))
223 $this->assertEquals('result_url', $this->generator->generate($name, array('_locale' => 'de')));
227 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
229 public function testGenerateRoutenameMultilangNotFound()
233 $this->provider->expects($this->once())
234 ->method('getRouteByName')
236 ->will($this->returnValue(null))
239 $this->generator->generate($name, array('_locale' => 'de'));
242 public function testGenerateDocumentMultilang()
244 $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
245 $route_en->setLocale('en');
246 $route_de = $this->routeDocument;
247 $route_de->setLocale('de');
249 $this->contentDocument->expects($this->once())
250 ->method('getRoutes')
251 ->will($this->returnValue(array($route_en, $route_de)))
253 $route_en->expects($this->never())
256 $route_de->expects($this->once())
258 ->will($this->returnValue($this->routeCompiled))
261 $this->assertEquals('result_url', $this->generator->generate($this->contentDocument, array('_locale' => 'de')));
264 public function testGenerateDocumentMultilangLocaleNomatch()
266 $route_en = $this->buildMock('Symfony\Cmf\Component\Routing\Tests\Routing\RouteMock', array('getDefaults', 'compile'));
267 $route_en->setLocale('en');
268 $route_de = $this->routeDocument;
269 $route_de->setLocale('de');
271 $this->contentDocument->expects($this->once())
272 ->method('getRoutes')
273 ->will($this->returnValue(array($route_en, $route_de)))
275 $route_en->expects($this->once())
277 ->will($this->returnValue($this->routeCompiled))
279 $route_de->expects($this->never())
283 $this->assertEquals('result_url', $this->generator->generate($this->contentDocument, array('_locale' => 'fr')));
287 * Generate without any information.
289 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
291 public function testGenerateNoContent()
293 $this->generator->generate('', array());
297 * Generate with an object that is neither a route nor route aware.
299 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
301 public function testGenerateInvalidContent()
303 $this->generator->generate($this);
307 * Generate with a content_id but there is no content repository.
309 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
311 public function testGenerateNoContentRepository()
313 $this->provider->expects($this->never())
314 ->method('getRouteByName')
317 $this->generator->generate('', array('content_id' => '/content/id'));
321 * Generate with content_id but the content is not found.
323 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
325 public function testGenerateNoContentFoundInRepository()
327 $this->provider->expects($this->never())
328 ->method('getRouteByName')
331 $contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
332 $contentRepository->expects($this->once())
334 ->with('/content/id')
335 ->will($this->returnValue(null))
337 $this->generator->setContentRepository($contentRepository);
339 $this->generator->generate('', array('content_id' => '/content/id'));
343 * Generate with content_id but the object at id is not route aware.
345 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
347 public function testGenerateWrongContentClassInRepository()
349 $this->provider->expects($this->never())
350 ->method('getRouteByName')
353 $contentRepository = $this->buildMock('Symfony\Cmf\Component\Routing\ContentRepositoryInterface', array('findById', 'getContentId'));
354 $contentRepository->expects($this->once())
356 ->with('/content/id')
357 ->will($this->returnValue($this))
359 $this->generator->setContentRepository($contentRepository);
361 $this->generator->generate('', array('content_id' => '/content/id'));
365 * Generate from a content that has no routes associated.
367 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
369 public function testGenerateNoRoutes()
371 $this->contentDocument->expects($this->once())
372 ->method('getRoutes')
373 ->will($this->returnValue(array()));
375 $this->generator->generate($this->contentDocument);
379 * Generate from a content that returns something that is not a route as route.
381 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
383 public function testGenerateInvalidRoute()
385 $this->contentDocument->expects($this->once())
386 ->method('getRoutes')
387 ->will($this->returnValue(array($this)));
389 $this->generator->generate($this->contentDocument);
392 public function testGetLocaleAttribute()
394 $this->generator->setDefaultLocale('en');
396 $attributes = array('_locale' => 'fr');
397 $this->assertEquals('fr', $this->generator->getLocale($attributes));
400 public function testGetLocaleDefault()
402 $this->generator->setDefaultLocale('en');
404 $attributes = array();
405 $this->assertEquals('en', $this->generator->getLocale($attributes));
408 public function testGetLocaleContext()
410 $this->generator->setDefaultLocale('en');
412 $this->generator->getContext()->setParameter('_locale', 'de');
414 $attributes = array();
415 $this->assertEquals('de', $this->generator->getLocale($attributes));
418 public function testSupports()
420 $this->assertTrue($this->generator->supports(''));
421 $this->assertTrue($this->generator->supports(null));
422 $this->assertTrue($this->generator->supports($this->contentDocument));
423 $this->assertFalse($this->generator->supports($this));
426 public function testGetRouteDebugMessage()
428 $this->assertContains('/some/content', $this->generator->getRouteDebugMessage(null, array('content_id' => '/some/content')));
429 $this->assertContains('Route aware content Symfony\Cmf\Component\Routing\Tests\Routing\RouteAware', $this->generator->getRouteDebugMessage(new RouteAware()));
430 $this->assertContains('/some/content', $this->generator->getRouteDebugMessage('/some/content'));
435 * Overwrite doGenerate to reduce amount of mocking needed.
437 class TestableContentAwareGenerator extends ContentAwareGenerator
439 protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array())
446 public function getLocale($parameters)
448 return parent::getLocale($parameters);
452 class RouteAware implements RouteReferrersReadInterface
454 public function getRoutes()