Skip to content

Commit

Permalink
Update some datamapper and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Oct 7, 2021
1 parent fbe3c7c commit d5f1c27
Show file tree
Hide file tree
Showing 22 changed files with 610 additions and 985 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function map(
$unlocalizedDimensionContent = $dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes);
$localizedDimensionContent = $dimensionContentCollection->getDimensionContent($localizedDimensionAttributes);

if (!$unlocalizedDimensionContent ||!$localizedDimensionContent) {
if (!$unlocalizedDimensionContent || !$localizedDimensionContent) {
// TODO see https://github.com/sulu/SuluContentBundle/pull/204
throw new \RuntimeException('Create unlocalized and localized dimension content.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

class AuthorDataMapper implements DataMapperInterface
Expand All @@ -39,7 +38,6 @@ public function map(
return;
}


$this->setAuthorData($localizedDimensionContent, $data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Sulu\Bundle\ContentBundle\Content\Domain\Factory\CategoryFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\TagFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ExcerptInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\RoutableInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Bundle\RouteBundle\Generator\RouteGeneratorInterface;
use Sulu\Bundle\RouteBundle\Manager\ConflictResolverInterface;
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
use Sulu\Component\Content\Metadata\PropertyMetadata;
Expand All @@ -40,11 +39,6 @@ class RoutableDataMapper implements DataMapperInterface
*/
private $routeManager;

/**
* @var ConflictResolverInterface
*/
private $conflictResolver;

/**
* @var array<string, string>
*/
Expand All @@ -63,14 +57,12 @@ public function __construct(
StructureMetadataFactoryInterface $factory,
RouteGeneratorInterface $routeGenerator,
RouteManagerInterface $routeManager,
ConflictResolverInterface $conflictResolver,
array $structureDefaultTypes,
array $routeMappings
) {
$this->factory = $factory;
$this->routeGenerator = $routeGenerator;
$this->routeManager = $routeManager;
$this->conflictResolver = $conflictResolver;
$this->structureDefaultTypes = $structureDefaultTypes;
$this->routeMappings = $routeMappings;
}
Expand All @@ -85,7 +77,7 @@ public function map(
}

if (!$localizedDimensionContent instanceof TemplateInterface) {
throw new \RuntimeException('LocalizedDimensionContent needs to extend the TemplateInterface');
throw new \RuntimeException('LocalizedDimensionContent needs to extend the TemplateInterface.');
}

$type = $localizedDimensionContent::getTemplateType();
Expand All @@ -107,19 +99,19 @@ public function map(
}

$property = $this->getRouteProperty($metadata);
if (!$property) {
return;
}

if (!$localizedDimensionContent->getResourceId()) {
// FIXME the code only works if the entity is flushed once and has a valid id

if (!$property) {
return;
}

$locale = $localizedDimensionContent->getLocale();
if (!$locale) {
return;
throw new \RuntimeException('Expected a LocalizedDimensionContent with a locale.');
}

if (!$localizedDimensionContent->getResourceId()) {
// TODO route bundle should work to update the entity later with a resourceId over UPDATE SQL statement
throw new \RuntimeException('Expected a LocalizedDimensionContent with a resourceId.');
}

/** @var string $name */
Expand All @@ -133,6 +125,7 @@ public function map(
$entityClass = null;
$routeSchema = null;
$resourceKey = $localizedDimensionContent::getResourceKey();

foreach ($this->routeMappings as $key => $mapping) {
if ($resourceKey === $mapping['resource_key']) {
$entityClass = $mapping['entityClass'] ?? $key;
Expand All @@ -142,11 +135,11 @@ public function map(
}

if (null === $entityClass || null === $routeSchema) {
// TODO FIXME add test case for this
return; // @codeCoverageIgnore
return;
}

$routePath = $data[$name] ?? null;

if (!$routePath) {
/** @var mixed $routeGenerationData */
$routeGenerationData = \array_merge(
Expand All @@ -161,21 +154,20 @@ public function map(
$routeGenerationData,
$routeSchema
);
}

if ('/' === $routePath) {
return;
}
if ('/' === $routePath) {
throw new \RuntimeException('Not allowed url "/" given or generated.');
}

$route = $this->routeManager->createOrUpdateByAttributes(
$entityClass,
(string) $localizedDimensionContent->getResourceId(),
$locale,
$routePath
$routePath,
true
);

$this->conflictResolver->resolve($route);

if (($data[$name] ?? null) !== $route->getPath()) {
$localizedDimensionContent->setTemplateData(
\array_merge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\SeoInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper;

use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\TemplateInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactoryInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function map(
}

/**
* @param WorkflowInterface&DimensionContentInterface $object
* @param mixed[] $data
*/
private function setWorkflowData(WorkflowInterface $object, array $data): void
Expand All @@ -40,6 +41,7 @@ private function setWorkflowData(WorkflowInterface $object, array $data): void
}

/**
* @param WorkflowInterface&DimensionContentInterface $object
* @param mixed[] $data
*/
private function setInitialPlaceToDraftDimension(WorkflowInterface $object, array $data): void
Expand All @@ -48,8 +50,7 @@ private function setInitialPlaceToDraftDimension(WorkflowInterface $object, arra
// after the place was set by this mapper initially, the place should only be changed by the ContentWorkflow
// see: https://github.com/sulu/SuluContentBundle/issues/92

if (!$object instanceof DimensionContentInterface
|| DimensionContentInterface::STAGE_DRAFT !== $object->getStage()) {
if (DimensionContentInterface::STAGE_DRAFT !== $object->getStage()) {
return;
}

Expand All @@ -60,15 +61,15 @@ private function setInitialPlaceToDraftDimension(WorkflowInterface $object, arra
}

/**
* @param WorkflowInterface&DimensionContentInterface $object
* @param mixed[] $data
*/
private function setPublishedToLiveDimension(WorkflowInterface $object, array $data): void
{
// the published property of the draft dimension should only be changed by a ContentWorkflow subscriber
// therefore we only want to copy the published property from the draft to the live dimension

if (!$object instanceof DimensionContentInterface
|| DimensionContentInterface::STAGE_LIVE !== $object->getStage()) {
if (DimensionContentInterface::STAGE_LIVE !== $object->getStage()) {
return;
}

Expand Down
1 change: 0 additions & 1 deletion Resources/config/data-mapper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
<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 type="service" id="sulu_route.manager.conflict_resolver.auto_increment"/>
<argument>%sulu.content.structure.default_types%</argument>
<argument>%sulu_route.mappings%</argument>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ public function postAction(Request $request): Response
{
$example = new Example();

$this->entityManager->persist($example);
$this->entityManager->flush($example);

$data = $this->getData($request);
$dimensionAttributes = $this->getDimensionAttributes($request); // ["locale" => "en", "stage" => "draft"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,11 @@ public function setTitle(?string $title): void
public function getTemplateData(): array
{
$data = $this->parentGetTemplateData();
$data['title'] = $this->getTitle();
$title = $this->getTitle();

if ($title) {
$data['title'] = $title;
}

return $data;
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Integration/ExampleControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function testPost(): int
$this->assertResponseSnapshot('example_post.json', $response, 201);

$routeRepository = $this->getContainer()->get('sulu.repository.route');
$this->assertCount(0, $routeRepository->findAll());
$this->assertCount(1, $routeRepository->findAll());

$id = \json_decode((string) $response->getContent(), true)['id'] ?? null;

Expand Down
12 changes: 10 additions & 2 deletions Tests/Traits/CreateExampleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ protected static function createExample(array $dataSet = [], array $options = []
['stage' => DimensionContentInterface::STAGE_DRAFT, 'locale' => $locale],
ExampleDimensionContent::class
);
$contentDataMapper->map($fillWithdefaultData($draftData), $draftDimensionContentCollection);
$contentDataMapper->map(
$draftDimensionContentCollection,
$draftDimensionContentCollection->getDimensionAttributes(),
$fillWithdefaultData($draftData)
);
$draftLocalizedDimension->setWorkflowPlace(WorkflowInterface::WORKFLOW_PLACE_DRAFT);

if ($liveData) {
Expand Down Expand Up @@ -134,7 +138,11 @@ protected static function createExample(array $dataSet = [], array $options = []
ExampleDimensionContent::class
);
$liveData['published'] = \date('Y-m-d H:i:s');
$contentDataMapper->map($fillWithdefaultData($liveData), $liveDimensionContentCollection);
$contentDataMapper->map(
$liveDimensionContentCollection,
$liveDimensionContentCollection->getDimensionAttributes(),
$fillWithdefaultData($liveData)
);

if ($options['create_route'] ?? false) {
$route = new Route();
Expand Down
41 changes: 41 additions & 0 deletions Tests/Traits/SetGetPrivatePropertyTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Tests\Traits;

trait SetGetPrivatePropertyTrait
{
/**
* @return mixed
*/
protected static function getPrivateProperty(object $object, string $propertyName)
{
$reflection = new \ReflectionClass($object);
$propertyReflection = $reflection->getProperty($propertyName);
$propertyReflection->setAccessible(true);

return $propertyReflection->getValue($object);
}

/**
* @param mixed $value
*/
protected static function setPrivateProperty(object $object, string $propertyName, $value): void
{
$reflection = new \ReflectionClass($object);
$propertyReflection = $reflection->getProperty($propertyName);
$propertyReflection->setAccessible(true);

$propertyReflection->setValue($object, $value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\ContentDataMapperInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\DataMapperInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

class ContentDataMapperTest extends TestCase
{
Expand All @@ -40,12 +41,46 @@ public function testMap(): void
$dataMapper2->reveal(),
]);

$unlocalizedDimensionAttributes = ['stage' => 'draft', 'locale' => null];
$localizedDimensionAttributes = ['stage' => 'draft', 'locale' => 'en'];

$data = ['test-key' => 'test-value'];
$dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class);
$unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$localizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes)
->willReturn($unlocalizedDimensionContent->reveal())
->shouldBeCalled();
$dimensionContentCollection->getDimensionContent($localizedDimensionAttributes)
->willReturn($localizedDimensionContent->reveal())
->shouldBeCalled();

$dataMapper1->map($unlocalizedDimensionContent->reveal(), $localizedDimensionContent->reveal(), $data)
->shouldBeCalled();
$dataMapper2->map($unlocalizedDimensionContent->reveal(), $localizedDimensionContent->reveal(), $data)
->shouldBeCalled();

$dataMapper1->map($data, $dimensionContentCollection->reveal())->shouldBeCalled();
$dataMapper2->map($data, $dimensionContentCollection->reveal())->shouldBeCalled();
$contentDataMapper->map($dimensionContentCollection->reveal(), $localizedDimensionAttributes, $data);
}

public function testMapNoLocalized(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Create unlocalized and localized dimension content.');

$unlocalizedDimensionAttributes = ['stage' => 'draft', 'locale' => null];
$localizedDimensionAttributes = ['stage' => 'draft', 'locale' => 'en'];

$data = ['test-key' => 'test-value'];
$dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class);
$dimensionContentCollection->getDimensionContent($unlocalizedDimensionAttributes)
->willReturn(null)
->shouldBeCalled();
$dimensionContentCollection->getDimensionContent($localizedDimensionAttributes)
->willReturn(null)
->shouldBeCalled();

$contentDataMapper->map($data, $dimensionContentCollection->reveal());
$contentDataMapper = $this->createContentDataMapperInstance([]);
$contentDataMapper->map($dimensionContentCollection->reveal(), $localizedDimensionAttributes, $data);
}
}
Loading

0 comments on commit d5f1c27

Please sign in to comment.