-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
211 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?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\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\Domain\Model\ContentRichEntityInterface; | ||
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface; | ||
|
||
class RouteRemover implements EventSubscriber | ||
{ | ||
/** | ||
* @var RouteRepositoryInterface | ||
*/ | ||
private $routeRepository; | ||
|
||
public function __construct(RouteRepositoryInterface $routeRepository) | ||
{ | ||
$this->routeRepository = $routeRepository; | ||
} | ||
|
||
public function getSubscribedEvents() | ||
{ | ||
return [Events::preRemove]; | ||
} | ||
|
||
public function preRemove(LifecycleEventArgs $event): void | ||
{ | ||
$object = $event->getObject(); | ||
if (!$object instanceof ContentRichEntityInterface) { | ||
return; // @codeCoverageIgnore | ||
} | ||
|
||
$entityClass = ClassUtils::getRealClass(\get_class($object)); | ||
foreach ($this->routeRepository->findAllByEntity($entityClass, $object->getId()) as $route) { | ||
$event->getEntityManager()->remove($route); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* eslint-disable flowtype/require-valid-file-annotation */ | ||
/* eslint-disable import/no-nodejs-modules */ | ||
const path = require('path'); | ||
|
||
// eslint-disable-next-line no-undef | ||
module.exports = { | ||
plugins: { | ||
'postcss-import': { | ||
path: path.resolve(process.cwd(), 'node_modules'), | ||
}, | ||
'postcss-nested': {}, | ||
'postcss-simple-vars': {}, | ||
'postcss-calc': {}, | ||
'postcss-hexrgba': {}, | ||
'autoprefixer': {}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
Tests/Unit/Content/Infrastructure/Doctrine/RouteRemoverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?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\Unit\Content\Infrastructure\Doctrine; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Doctrine\ORM\Event\LifecycleEventArgs; | ||
use Doctrine\ORM\Events; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\ObjectProphecy; | ||
use Sulu\Bundle\ContentBundle\Content\Domain\Model\ContentRichEntityInterface; | ||
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\RouteRemover; | ||
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface; | ||
use Sulu\Bundle\RouteBundle\Model\RouteInterface; | ||
|
||
class RouteRemoverTest extends TestCase | ||
{ | ||
/** | ||
* @var RouteRepositoryInterface|ObjectProphecy | ||
*/ | ||
private $routeRepository; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->routeRepository = $this->prophesize(RouteRepositoryInterface::class); | ||
} | ||
|
||
protected function getRouteRemover(): RouteRemover | ||
{ | ||
return new RouteRemover($this->routeRepository->reveal()); | ||
} | ||
|
||
public function testGetSubscribedEvents(): void | ||
{ | ||
$routeRemover = $this->getRouteRemover(); | ||
|
||
$this->assertSame([ | ||
Events::preRemove, | ||
], $routeRemover->getSubscribedEvents()); | ||
} | ||
|
||
public function testPreRemove(): void | ||
{ | ||
$object = $this->prophesize(ContentRichEntityInterface::class); | ||
$object->getId()->willReturn('123-123-123'); | ||
|
||
$entityManager = $this->prophesize(EntityManagerInterface::class); | ||
|
||
$event = $this->prophesize(LifecycleEventArgs::class); | ||
$event->getObject()->willReturn($object->reveal()); | ||
$event->getEntityManager()->willReturn($entityManager->reveal()); | ||
|
||
$route1 = $this->prophesize(RouteInterface::class); | ||
$route2 = $this->prophesize(RouteInterface::class); | ||
$this->routeRepository->findAllByEntity(Argument::any(), '123-123-123') | ||
->willReturn([$route1->reveal(), $route2->reveal()]); | ||
|
||
$entityManager->remove($route1->reveal())->shouldBeCalled(); | ||
$entityManager->remove($route2->reveal())->shouldBeCalled(); | ||
|
||
$routeRemover = $this->getRouteRemover(); | ||
$routeRemover->preRemove($event->reveal()); | ||
} | ||
|
||
public function testPreRemoveNoContentRichEntity(): void | ||
{ | ||
$object = $this->prophesize(\stdClass::class); | ||
$event = $this->prophesize(LifecycleEventArgs::class); | ||
|
||
$event->getObject()->willReturn($object->reveal()); | ||
|
||
$this->routeRepository->findAllByEntity(Argument::cetera())->shouldNotBeCalled(); | ||
|
||
$routeRemover = $this->getRouteRemover(); | ||
$routeRemover->preRemove($event->reveal()); | ||
} | ||
} |