Skip to content

Commit

Permalink
Enhance route generation (#156)
Browse files Browse the repository at this point in the history
* Enhance route generation

* fix ci

* fix review
  • Loading branch information
luca-rath committed Jun 5, 2020
1 parent 667b4a7 commit aa13ae0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,17 @@ public function map(

$routePath = $data[$name] ?? null;
if (!$routePath) {
/** @var mixed $routeGenerationData */
$routeGenerationData = array_merge(
$data,
[
'_unlocalizedObject' => $unlocalizedObject,
'_localizedObject' => $localizedObject,
]
);

$routePath = $this->routeGenerator->generate(
$localizedObject,
$routeGenerationData,
$routeSchema
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function createRouteDataMapperInstance(
'testKey' => [
'generator' => 'schema',
'options' => [
'route_schema' => '/{object.getTitle()}',
'route_schema' => '/{object["title"]}',
],
'resource_key' => 'testKey',
'entityClass' => 'mock-content-class',
Expand Down Expand Up @@ -319,8 +319,13 @@ public function testMapNoRoutePropertyDataAndNoOldRoute(): void

$factory->getStructureMetadata('mock-template-type', 'default')->willReturn($metadata->reveal())->shouldBeCalled();

$routeGenerator->generate($localizedDimensionContentMock, ['route_schema' => '/{object.getTitle()}'])
->willReturn('/test');
$routeGenerator->generate(
array_merge($data, [
'_unlocalizedObject' => $dimensionContent->reveal(),
'_localizedObject' => $localizedDimensionContentMock,
]),
['route_schema' => '/{object["title"]}']
)->willReturn('/test');

$routeManager->createOrUpdateByAttributes(
'mock-content-class',
Expand Down Expand Up @@ -373,8 +378,13 @@ public function testMapNoRoutePropertyDataAndNoOldRouteIgnoreSlash(): void

$factory->getStructureMetadata('mock-template-type', 'default')->willReturn($metadata->reveal())->shouldBeCalled();

$routeGenerator->generate($localizedDimensionContentMock, ['route_schema' => '/{object.getTitle()}'])
->willReturn('/');
$routeGenerator->generate(
array_merge($data, [
'_unlocalizedObject' => $dimensionContent->reveal(),
'_localizedObject' => $localizedDimensionContentMock,
]),
['route_schema' => '/{object["title"]}']
)->willReturn('/');

$routeManager->createOrUpdateByAttributes(Argument::cetera())->shouldNotBeCalled();

Expand Down Expand Up @@ -505,8 +515,13 @@ public function testMapNoRoutePath(): void

$localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent);

$routeGenerator->generate($localizedDimensionContentMock, ['route_schema' => '/{object.getTitle()}'])
->willReturn('/test');
$routeGenerator->generate(
array_merge($data, [
'_unlocalizedObject' => $dimensionContent->reveal(),
'_localizedObject' => $localizedDimensionContentMock,
]),
['route_schema' => '/{object["title"]}']
)->willReturn('/test');

$routeManager->createOrUpdateByAttributes(
'mock-content-class',
Expand Down Expand Up @@ -559,8 +574,7 @@ public function testMap(): void
$localizedDimensionContent->getResourceKey()->willReturn('testKey');
$localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent);

$routeGenerator->generate($localizedDimensionContentMock, ['schema' => '/{object.getTitle()}'])
->shouldNotBeCalled();
$routeGenerator->generate(Argument::any())->shouldNotBeCalled();

$routeManager->createOrUpdateByAttributes(
'mock-content-class',
Expand Down Expand Up @@ -612,8 +626,7 @@ public function testMapNoTemplateWithDefaultTemplate(): void
$localizedDimensionContent->getResourceKey()->willReturn('testKey');
$localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent);

$routeGenerator->generate($localizedDimensionContentMock, ['schema' => '/{object.getTitle()}'])
->shouldNotBeCalled();
$routeGenerator->generate(Argument::any())->shouldNotBeCalled();

$routeManager->createOrUpdateByAttributes(
'mock-content-class',
Expand Down Expand Up @@ -668,8 +681,13 @@ public function testMapCustomRoute(): void
$localizedDimensionContent->getResourceKey()->willReturn('testKey');
$localizedDimensionContentMock = $this->wrapRoutableMock($localizedDimensionContent);

$routeGenerator->generate($localizedDimensionContentMock, ['route_schema' => 'custom/{object.getName()}-{object.getId()}'])
->willReturn('/custom/testEntity-123');
$routeGenerator->generate(
array_merge($data, [
'_unlocalizedObject' => $dimensionContent->reveal(),
'_localizedObject' => $localizedDimensionContentMock,
]),
['route_schema' => 'custom/{object["_localizedObject"].getName()}-{object["_unlocalizedObject"].getRoutableId()}']
)->willReturn('/custom/testEntity-123');

$routeManager->createOrUpdateByAttributes(
'Sulu/Test/TestEntity',
Expand All @@ -687,7 +705,7 @@ public function testMapCustomRoute(): void
'testKey' => [
'generator' => 'schema',
'options' => [
'route_schema' => 'custom/{object.getName()}-{object.getId()}',
'route_schema' => 'custom/{object["_localizedObject"].getName()}-{object["_unlocalizedObject"].getRoutableId()}',
],
'resource_key' => 'testKey',
'entityClass' => 'Sulu/Test/TestEntity',
Expand Down

0 comments on commit aa13ae0

Please sign in to comment.