Skip to content

Commit

Permalink
Use default template if no template key is provided (#80)
Browse files Browse the repository at this point in the history
* use default template if no template key is provided

* fix ci
  • Loading branch information
luca-rath committed Feb 21, 2020
1 parent c03e14a commit cb85d08
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,24 @@ class RouteDataMapper implements DataMapperInterface
*/
private $routeManager;

/**
* @var array<string, string>
*/
private $structureDefaultTypes;

/**
* @param array<string, string> $structureDefaultTypes
*/
public function __construct(
StructureMetadataFactoryInterface $factory,
RouteGeneratorInterface $routeGenerator,
RouteManagerInterface $routeManager
RouteManagerInterface $routeManager,
array $structureDefaultTypes
) {
$this->factory = $factory;
$this->routeGenerator = $routeGenerator;
$this->routeManager = $routeManager;
$this->structureDefaultTypes = $structureDefaultTypes;
}

public function map(
Expand All @@ -61,12 +71,18 @@ public function map(
throw new \RuntimeException('LocalizedObject needs to extend the TemplateInterface');
}

if (!isset($data['template'])) {
throw new \RuntimeException('Expected "template" to be set in the data array.');
$type = $localizedObject->getTemplateType();

/** @var string|null $template */
$template = $data['template'] ?? null;

if (null === $template) {
$template = $this->structureDefaultTypes[$type] ?? null;
}

$template = $data['template'];
$type = $localizedObject->getTemplateType();
if (null === $template) {
throw new \RuntimeException('Expected "template" to be set in the data array.');
}

$metadata = $this->factory->getStructureMetadata($type, $template);
if (!$metadata) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,18 @@ class TemplateDataMapper implements DataMapperInterface
*/
private $factory;

public function __construct(StructureMetadataFactoryInterface $factory)
/**
* @var array<string, string>
*/
private $structureDefaultTypes;

/**
* @param array<string, string> $structureDefaultTypes
*/
public function __construct(StructureMetadataFactoryInterface $factory, array $structureDefaultTypes)
{
$this->factory = $factory;
$this->structureDefaultTypes = $structureDefaultTypes;
}

public function map(
Expand All @@ -37,15 +46,22 @@ public function map(
return;
}

if (!isset($data['template'])) {
throw new \RuntimeException('Expected "template" to be set in the data array.');
$type = $unlocalizedObject->getTemplateType();

/** @var string|null $template */
$template = $data['template'] ?? null;

if (null === $template) {
$template = $this->structureDefaultTypes[$type] ?? null;
}

$template = $data['template'];
if (null === $template) {
throw new \RuntimeException('Expected "template" to be set in the data array.');
}

list($unlocalizedData, $localizedData) = $this->getTemplateData(
$data,
$unlocalizedObject->getTemplateType(),
$type,
$template
);

Expand Down
2 changes: 2 additions & 0 deletions Resources/config/mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<!-- DataMapper -->
<service id="sulu_content.template_mapper" class="Sulu\Bundle\ContentBundle\Content\Application\DimensionContentFactory\DataMapper\TemplateDataMapper">
<argument type="service" id="sulu_page.structure.factory"/>
<argument>%sulu.content.structure.default_types%</argument>

<tag name="sulu_content.data_mapper"/>
</service>
Expand All @@ -25,6 +26,7 @@
<argument type="service" id="sulu_page.structure.factory"/>
<argument type="service" id="sulu_route.generator.route_generator"/>
<argument type="service" id="sulu_route.manager.route_manager"/>
<argument>%sulu.content.structure.default_types%</argument>

<tag name="sulu_content.data_mapper"/>
</service>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@

class RouteMapperTest extends TestCase
{
/**
* @param array<string, string> $structureDefaultTypes
*/
protected function createRouteDataMapperInstance(
StructureMetadataFactoryInterface $factory,
RouteGeneratorInterface $routeGenerator,
RouteManagerInterface $routeManager
RouteManagerInterface $routeManager,
array $structureDefaultTypes = []
): RouteDataMapper {
return new RouteDataMapper($factory, $routeGenerator, $routeManager);
return new RouteDataMapper($factory, $routeGenerator, $routeManager, $structureDefaultTypes);
}

public function testMapNoRoutable(): void
Expand Down Expand Up @@ -117,6 +121,7 @@ public function testMapNoTemplate(): void
$localizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$localizedDimensionContent->willImplement(RoutableInterface::class);
$localizedDimensionContent->willImplement(TemplateInterface::class);
$localizedDimensionContent->getTemplateType()->willReturn('example');

$factory = $this->prophesize(StructureMetadataFactoryInterface::class);
$routeGenerator = $this->prophesize(RouteGeneratorInterface::class);
Expand Down Expand Up @@ -515,4 +520,54 @@ public function testMap(): void

$mapper->map($data, $dimensionContent->reveal(), $localizedDimensionContent->reveal());
}

public function testMapNoTemplateWithDefaultTemplate(): void
{
$data = [
'url' => '/test',
];

$dimensionContent = $this->prophesize(DimensionContentInterface::class);
$localizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$localizedDimensionContent->willImplement(RoutableInterface::class);
$localizedDimensionContent->willImplement(TemplateInterface::class);

$factory = $this->prophesize(StructureMetadataFactoryInterface::class);
$routeGenerator = $this->prophesize(RouteGeneratorInterface::class);
$routeManager = $this->prophesize(RouteManagerInterface::class);

$metadata = $this->prophesize(StructureMetadata::class);
$property = $this->prophesize(PropertyMetadata::class);
$property->getType()->willReturn('route');
$property->getName()->willReturn('url');

$metadata->getProperties()->WillReturn([$property->reveal()]);

$localizedDimensionContent->getTemplateType()->willReturn('example');
$localizedDimensionContent->getTemplateData()->willReturn([]);
$factory->getStructureMetadata('example', 'default')->willReturn($metadata->reveal())->shouldBeCalled();

$localizedDimensionContent->getContentId()->willReturn('123-123-123');
$localizedDimensionContent->getContentClass()->willReturn('App\Entity\Example');
$localizedDimensionContent->getLocale()->willReturn('en');

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

$routeManager->createOrUpdateByAttributes(
'App\Entity\Example',
'123-123-123',
'en',
'/test'
)->shouldBeCalled();

$mapper = $this->createRouteDataMapperInstance(
$factory->reveal(),
$routeGenerator->reveal(),
$routeManager->reveal(),
['example' => 'default']
);

$mapper->map($data, $dimensionContent->reveal(), $localizedDimensionContent->reveal());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@

class TemplateMapperTest extends TestCase
{
/**
* @param array<string, string> $structureDefaultTypes
*/
protected function createTemplateDataMapperInstance(
StructureMetadataFactoryInterface $structureMetadataFactory
StructureMetadataFactoryInterface $structureMetadataFactory,
array $structureDefaultTypes = []
): TemplateDataMapper {
return new TemplateDataMapper($structureMetadataFactory);
return new TemplateDataMapper($structureMetadataFactory, $structureDefaultTypes);
}

public function testMapNoTemplateInstance(): void
Expand Down Expand Up @@ -57,6 +61,7 @@ public function testMapLocalizedNoTemplateKey(): void

$dimensionContent = $this->prophesize(DimensionContentInterface::class);
$dimensionContent->willImplement(TemplateInterface::class);
$dimensionContent->getTemplateType()->willReturn('example');

$localizedDimensionContent = $this->prophesize(DimensionContentInterface::class);

Expand Down Expand Up @@ -201,6 +206,51 @@ public function testMapLocalizedTemplate(): void
$templateMapper->map($data, $dimensionContent->reveal(), $localizedDimensionContent->reveal());
}

public function testMapLocalizedNoTemplateKeyWithDefaultTemplate(): void
{
$data = [
'unlocalizedField' => 'Test Unlocalized',
'localizedField' => 'Test Localized',
];

$dimensionContent = $this->prophesize(DimensionContentInterface::class);
$dimensionContent->willImplement(TemplateInterface::class);
$dimensionContent->getTemplateType()->willReturn('example')->shouldBeCalled();
$dimensionContent->getTemplateData()->willReturn([])->shouldBeCalled();
$dimensionContent->setTemplateData(['unlocalizedField' => 'Test Unlocalized'])->shouldBeCalled();

$localizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$localizedDimensionContent->willImplement(TemplateInterface::class);
$localizedDimensionContent->setTemplateKey('template-key')->shouldBeCalled();
$localizedDimensionContent->setTemplateData(['localizedField' => 'Test Localized'])->shouldBeCalled();

$unlocalizedPropertyMetadata = $this->prophesize(PropertyMetadata::class);
$unlocalizedPropertyMetadata->getName()->willReturn('unlocalizedField')->shouldBeCalled();
$unlocalizedPropertyMetadata->isLocalized()->willReturn(false)->shouldBeCalled();
$localizedPropertyMetadata = $this->prophesize(PropertyMetadata::class);
$localizedPropertyMetadata->getName()->willReturn('localizedField')->shouldBeCalled();
$localizedPropertyMetadata->isLocalized()->willReturn(true)->shouldBeCalled();

$structureMetadata = $this->prophesize(StructureMetadata::class);
$structureMetadata->getProperties()->willReturn([
$unlocalizedPropertyMetadata->reveal(),
$localizedPropertyMetadata->reveal(),
])->shouldBeCalled();

$structureMetadataFactory = $this->prophesize(StructureMetadataFactoryInterface::class);
$structureMetadataFactory->getStructureMetadata(
'example',
'template-key'
)->willReturn($structureMetadata->reveal())->shouldBeCalled();

$templateMapper = $this->createTemplateDataMapperInstance(
$structureMetadataFactory->reveal(),
['example' => 'template-key']
);

$templateMapper->map($data, $dimensionContent->reveal(), $localizedDimensionContent->reveal());
}

public function testMapFloatValueTemplate(): void
{
$data = [
Expand Down

0 comments on commit cb85d08

Please sign in to comment.