Skip to content

Commit

Permalink
Merge pull request #251 from symfony-cmf/provider-based-generator
Browse files Browse the repository at this point in the history
have provider based generator support new mode too
  • Loading branch information
dbu authored May 4, 2020
2 parents d400fe5 + b54577b commit ab47aab
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

2.3.1
-----

* ProviderBasedGenerator now also supports the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME`
and `RouteObjectInterface::ROUTE_OBJECT`.

2.3.0
-----

Expand Down
7 changes: 5 additions & 2 deletions src/ContentAwareGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public function generate($name, $parameters = [], $absolute = UrlGeneratorInterf

$route = $this->getBestLocaleRoute($name, $parameters);
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name) {
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters) && $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute) {
if (array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute
) {
$route = $this->getBestLocaleRoute($parameters[RouteObjectInterface::ROUTE_OBJECT], $parameters);
} else {
$route = $this->getRouteByContent($name, $parameters);
Expand All @@ -98,8 +100,9 @@ public function generate($name, $parameters = [], $absolute = UrlGeneratorInterf
}

$this->unsetLocaleIfNotNeeded($route, $parameters);
$parameters[RouteObjectInterface::ROUTE_OBJECT] = $route;

return parent::generate($route, $parameters, $absolute);
return parent::generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, $parameters, $absolute);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/ProviderBasedGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,27 @@ public function __construct(RouteProviderInterface $provider, LoggerInterface $l

/**
* {@inheritdoc}
*
* The CMF routing system used to allow to pass route objects as $name to generate the route.
* Since Symfony 5.0, the UrlGeneratorInterface declares $name as string. We widen the contract
* for BC but deprecate passing non-strings.
* Instead, Pass the RouteObjectInterface::OBJECT_BASED_ROUTE_NAME as route name and the object
* in the parameters with key RouteObjectInterface::ROUTE_OBJECT.
*
* @param mixed $name
*/
public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH)
{
if (is_object($name)) {
@trigger_error('Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`', E_USER_DEPRECATED);
}
if ($name instanceof SymfonyRoute) {
$route = $name;
} elseif (RouteObjectInterface::OBJECT_BASED_ROUTE_NAME === $name
&& array_key_exists(RouteObjectInterface::ROUTE_OBJECT, $parameters)
&& $parameters[RouteObjectInterface::ROUTE_OBJECT] instanceof SymfonyRoute
) {
$route = $parameters[RouteObjectInterface::ROUTE_OBJECT];
} elseif (null === $route = $this->provider->getRouteByName($name)) {
throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name));
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Routing/ContentAwareGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ContentAwareGeneratorTest extends TestCase
*/
private $context;

public function setUp()
public function setUp(): void
{
$this->contentDocument = $this->createMock(RouteReferrersReadInterface::class);
$this->routeDocument = $this->getMockBuilder(RouteMock::class)
Expand Down
54 changes: 39 additions & 15 deletions tests/Unit/Routing/ProviderBasedGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ProviderBasedGeneratorTest extends TestCase
*/
private $context;

public function setUp()
public function setUp(): void
{
$this->routeDocument = $this->createMock(Route::class);
$this->routeCompiled = $this->createMock(CompiledRoute::class);
Expand All @@ -59,58 +59,79 @@ public function setUp()
$this->generator = new TestableProviderBasedGenerator($this->provider);
}

public function testGenerateFromName()
public function testGenerateFromName(): void
{
$name = 'foo/bar';

$this->provider->expects($this->once())
->method('getRouteByName')
->with($name)
->will($this->returnValue($this->routeDocument))
->willReturn($this->routeDocument)
;
$this->routeDocument->expects($this->once())
->method('compile')
->will($this->returnValue($this->routeCompiled))
->willReturn($this->routeCompiled)
;

$this->assertEquals('result_url', $this->generator->generate($name));
}

public function testGenerateNotFound()
public function testGenerateNotFound(): void
{
$name = 'foo/bar';

$this->provider->expects($this->once())
->method('getRouteByName')
->with($name)
->will($this->returnValue(null))
->willReturn(null)
;

$this->expectException(RouteNotFoundException::class);
$this->generator->generate($name);
}

public function testGenerateFromRoute()
public function testGenerateFromRoute(): void
{
$this->provider->expects($this->never())
->method('getRouteByName')
;
$this->routeDocument->expects($this->once())
->method('compile')
->will($this->returnValue($this->routeCompiled))
->willReturn($this->routeCompiled)
;

$url = $this->generator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $this->routeDocument,
]);
$this->assertEquals('result_url', $url);
}

/**
* @group legacy
*
* @expectedDeprecation Passing an object as route name is deprecated since version 2.3. Pass the `RouteObjectInterface::OBJECT_BASED_ROUTE_NAME` as route name and the object in the parameters with key `RouteObjectInterface::ROUTE_OBJECT`
*/
public function testGenerateFromRouteLegacy(): void
{
$this->provider->expects($this->never())
->method('getRouteByName')
;
$this->routeDocument->expects($this->once())
->method('compile')
->willReturn($this->routeCompiled)
;

$this->assertEquals('result_url', $this->generator->generate($this->routeDocument));
}

public function testSupports()
public function testSupports(): void
{
$this->assertTrue($this->generator->supports('foo/bar'));
$this->assertTrue($this->generator->supports($this->routeDocument));
$this->assertFalse($this->generator->supports($this));
}

public function testGetRouteDebugMessage()
public function testGetRouteDebugMessage(): void
{
$this->assertContains('/some/key', $this->generator->getRouteDebugMessage(new RouteObject()));
$this->assertContains('/de/test', $this->generator->getRouteDebugMessage(new Route('/de/test')));
Expand All @@ -121,7 +142,7 @@ public function testGetRouteDebugMessage()
/**
* Tests the generate method with passing in a route object into generate().
*/
public function testGenerateByRoute()
public function testGenerateByRoute(): void
{
$this->generator = new ProviderBasedGenerator($this->provider);

Expand All @@ -137,7 +158,10 @@ public function testGenerateByRoute()
$this->generator->setContext($context);

$this->expectException(InvalidParameterException::class);
$this->generator->generate($route, ['number' => 'string']);
$this->generator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $route,
'number' => 'string',
]);
}
}

Expand All @@ -154,13 +178,13 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa

class RouteObject implements RouteObjectInterface
{
public function getRouteKey()
public function getRouteKey(): string
{
return '/some/key';
}

public function getContent()
public function getContent(): ?object
{
return;
return null;
}
}

0 comments on commit ab47aab

Please sign in to comment.