Skip to content

Commit

Permalink
replace class detection over get_class with content-metadata-inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Sep 29, 2021
1 parent d436c36 commit 47f121b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManagerInterface;

class ContentMetadataInspector implements ContentMetadataInspectorInterface
Expand All @@ -29,6 +30,8 @@ public function __construct(EntityManagerInterface $entityManager)

public function getDimensionContentClass(string $contentRichEntityClass): string
{
$contentRichEntityClass = ClassUtils::getRealClass($contentRichEntityClass);

$classMetadata = $this->entityManager->getClassMetadata($contentRichEntityClass);
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');

Expand All @@ -37,6 +40,8 @@ public function getDimensionContentClass(string $contentRichEntityClass): string

public function getDimensionContentPropertyName(string $contentRichEntityClass): string
{
$contentRichEntityClass = ClassUtils::getRealClass($contentRichEntityClass);

$classMetadata = $this->entityManager->getClassMetadata($contentRichEntityClass);
$associationMapping = $classMetadata->getAssociationMapping('dimensionContents');

Expand Down
40 changes: 36 additions & 4 deletions Content/Infrastructure/Doctrine/RouteRemover.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
namespace Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine;

use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;

Expand All @@ -26,14 +26,32 @@
*/
class RouteRemover implements EventSubscriber
{
/**
* @var ContentMetadataInspectorInterface
*/
private $contentMetadataInspector;

/**
* @var RouteRepositoryInterface
*/
private $routeRepository;

public function __construct(RouteRepositoryInterface $routeRepository)
{
/**
* @var array<string, array<mixed>>
*/
private $routeMappings;

/**
* @param array<string, array<mixed>> $routeMappings
*/
public function __construct(
ContentMetadataInspectorInterface $contentMetadataInspector,
RouteRepositoryInterface $routeRepository,
array $routeMappings
) {
$this->routeRepository = $routeRepository;
$this->contentMetadataInspector = $contentMetadataInspector;
$this->routeMappings = $routeMappings;
}

public function getSubscribedEvents()
Expand All @@ -48,7 +66,21 @@ public function preRemove(LifecycleEventArgs $event): void
return; // @codeCoverageIgnore
}

$entityClass = ClassUtils::getRealClass(\get_class($object));
$dimensionContentClass = $this->contentMetadataInspector->getDimensionContentClass(\get_class($object));
$resourceKey = $dimensionContentClass::getResourceKey();

$entityClass = null;
foreach ($this->routeMappings as $key => $mapping) {
if ($resourceKey === $mapping['resource_key']) {
$entityClass = $mapping['entityClass'] ?? $key;
break;
}
}

if (!$entityClass) {
return;
}

foreach ($this->routeRepository->findAllByEntity($entityClass, $object->getId()) as $route) {
$event->getEntityManager()->remove($route);
}
Expand Down
2 changes: 2 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
</service>

<service id="sulu_content.route_remover" class="Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\RouteRemover">
<argument type="service" id="sulu_content.content_metadata_inspector"/>
<argument type="service" id="sulu.repository.route"/>
<argument>%sulu_route.mappings%</argument>
<tag name="doctrine.event_subscriber"/>
</service>

Expand Down
14 changes: 11 additions & 3 deletions Tests/Application/ExampleTestBundle/Entity/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,18 @@ public function getId()
return $this->id;
}

public function createDimensionContent(): DimensionContentInterface
/**
* @var mixed $id
*/
public function setId($id): self
{
$exampleDimensionContent = new ExampleDimensionContent($this);
$this->id = $id;

return $exampleDimensionContent;
return $this;
}

public function createDimensionContent(): DimensionContentInterface
{
return new ExampleDimensionContent($this);
}
}
4 changes: 2 additions & 2 deletions Tests/Application/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
$loader->load(__DIR__ . '/config/config_' . $this->getContext() . '.yml');

$devConfigFile = __DIR__ . '/config/config_' . $this->getContext() . '_' . $this->getEnvironment() . '.yml';
if (is_file($devConfigFile)) {
if (\is_file($devConfigFile)) {
$loader->load($devConfigFile);
}
}
Expand All @@ -66,4 +66,4 @@ protected function getKernelParameters()
}

// Needed for preview WebsiteKernelFactory
class_alias(Kernel::class, 'App\\Kernel');
\class_alias(Kernel::class, 'App\\Kernel');
80 changes: 62 additions & 18 deletions Tests/Unit/Content/Infrastructure/Doctrine/RouteRemoverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,76 @@
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMetadataInspector\ContentMetadataInspectorInterface;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\RouteRemover;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;

class RouteRemoverTest extends TestCase
{
public static function getResourceKey(): string
{
return 'test';
}

/**
* @var ContentMetadataInspectorInterface|ObjectProphecy
*/
private $contentMetadataInspector;

/**
* @var RouteRepositoryInterface|ObjectProphecy
*/
private $routeRepository;

/**
* @var array<string, array<mixed>>
*/
private $routeMappings = [
[
'resource_key' => 'examples',
'entityClass' => ExampleDimensionContent::class,
],
];

/**
* @var RouteRemover
*/
private $routeRemover;

protected function setUp(): void
{
parent::setUp();

$this->contentMetadataInspector = $this->prophesize(ContentMetadataInspectorInterface::class);
$this->routeRepository = $this->prophesize(RouteRepositoryInterface::class);
}

protected function getRouteRemover(): RouteRemover
{
return new RouteRemover($this->routeRepository->reveal());
$this->routeRemover = new RouteRemover(
$this->contentMetadataInspector->reveal(),
$this->routeRepository->reveal(),
$this->routeMappings
);
}

public function testGetSubscribedEvents(): void
{
$routeRemover = $this->getRouteRemover();

$this->assertSame([
Events::preRemove,
], $routeRemover->getSubscribedEvents());
], $this->routeRemover->getSubscribedEvents());
}

public function testPreRemove(): void
{
$object = $this->prophesize(ContentRichEntityInterface::class);
$object->getId()->willReturn('123-123-123');
$object = new Example();
$object->setId('123-123-123');

$this->contentMetadataInspector->getDimensionContentClass(Example::class)
->willReturn(ExampleDimensionContent::class);

$entityManager = $this->prophesize(EntityManagerInterface::class);
$event = new LifecycleEventArgs($object->reveal(), $entityManager->reveal());
$event = new LifecycleEventArgs($object, $entityManager->reveal());

$route1 = $this->prophesize(RouteInterface::class);
$route2 = $this->prophesize(RouteInterface::class);
Expand All @@ -68,20 +98,34 @@ public function testPreRemove(): void
$entityManager->remove($route1->reveal())->shouldBeCalled();
$entityManager->remove($route2->reveal())->shouldBeCalled();

$routeRemover = $this->getRouteRemover();
$routeRemover->preRemove($event);
$this->routeRemover->preRemove($event);
}

public function testPreRemoveNoMappingConfigured(): void
{
$object = new Example();
$object->setId('123-123-123');

$this->contentMetadataInspector->getDimensionContentClass(Example::class)
->willReturn(self::class); // For testing purpose we return the wrong dimension content class

$entityManager = $this->prophesize(EntityManagerInterface::class);
$event = new LifecycleEventArgs($object, $entityManager->reveal());

$this->routeRepository->findAllByEntity(Argument::cetera())->shouldNotBeCalled();

$this->routeRemover->preRemove($event);
}

public function testPreRemoveNoContentRichEntity(): void
{
$object = $this->prophesize(\stdClass::class);
$event = $this->prophesize(LifecycleEventArgs::class);
$object = new \stdClass();

$event->getObject()->willReturn($object->reveal());
$entityManager = $this->prophesize(EntityManagerInterface::class);
$event = new LifecycleEventArgs($object, $entityManager->reveal());

$this->routeRepository->findAllByEntity(Argument::cetera())->shouldNotBeCalled();

$routeRemover = $this->getRouteRemover();
$routeRemover->preRemove($event->reveal());
$this->routeRemover->preRemove($event);
}
}
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parameters:
- %currentWorkingDirectory%/vendor/*
- %currentWorkingDirectory%/Tests/Application/Kernel.php
- %currentWorkingDirectory%/Tests/Application/var/*
- %currentWorkingDirectory%/var/*
symfony:
container_xml_path: %currentWorkingDirectory%/Tests/Application/var/cache/admin/dev/Sulu_Bundle_ContentBundle_Tests_Application_KernelDevDebugContainer.xml
console_application_loader: Tests/phpstan/console-application.php
Expand Down

0 comments on commit 47f121b

Please sign in to comment.