diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ac22c365b..2e85d78e19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,12 +18,17 @@ a release. --- ## [Unreleased] +### Added +- SoftDeleteable: `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs` and `Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes. + +### Deprecated +- Do not add type-hinted parameters `Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs` and `Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs` classes to `preSoftDelete` and `postSoftDelete` events. +- `Gedmo\Mapping\Event\AdapterInterface::createLifecycleEventArgsInstance()` method. ## [3.14.0] ### Added - Support for Symfony 7 - Tree: Added `@template` and `@template-extends` annotations to the Tree repositories -- SoftDeleteable: `Gedmo\SoftDeleteable\Mapping\Event::createPreSoftDeleteEventArgs()` and `Gedmo\SoftDeleteable\Mapping\Event::createPostSoftDeleteEventArgs()` methods. ### Changed - Dropped support for PHP < 7.4 @@ -33,7 +38,6 @@ a release. ### Deprecated - Calling `Gedmo\Mapping\Event\Adapter\ORM::getObjectManager()` and `getObject()` on EventArgs that do not implement `getObjectManager()` and `getObject()` (such as old EventArgs implementing `getEntityManager()` and `getEntity()`) - Calling `Gedmo\Uploadable\Event\UploadableBaseEventArgs::getEntityManager()` and `getEntity()`. Call `getObjectManager()` and `getObject()` instead. -- `Gedmo\Mapping\Event\AdapterInterface::createLifecycleEventArgsInstance()` method. ## [3.13.0] - 2023-09-06 ### Fixed diff --git a/rector.php b/rector.php index aad132431a..2803e9f705 100644 --- a/rector.php +++ b/rector.php @@ -10,7 +10,6 @@ */ use Rector\Config\RectorConfig; -use Rector\Php71\Rector\FuncCall\CountOnNullRector; use Rector\Set\ValueObject\LevelSetList; use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromAssignsRector; @@ -33,7 +32,4 @@ $rectorConfig->importNames(); $rectorConfig->importShortClasses(false); - $rectorConfig->skip([ - CountOnNullRector::class, - ]); }; diff --git a/src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php b/src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php deleted file mode 100644 index 7eb7940692..0000000000 --- a/src/SoftDeleteable/Event/ORM/PostSoftDeleteEventArgs.php +++ /dev/null @@ -1,27 +0,0 @@ - http://www.gediminasm.org - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Gedmo\SoftDeleteable\Event\ORM; - -use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Event\LifecycleEventArgs; -use Doctrine\Persistence\Event\LifecycleEventArgs as PersistenceLifecycleEventArgs; - -if (!class_exists(LifecycleEventArgs::class)) { - /** @template-extends PersistenceLifecycleEventArgs */ - final class PostSoftDeleteEventArgs extends PersistenceLifecycleEventArgs - { - } -} else { - final class PostSoftDeleteEventArgs extends LifecycleEventArgs - { - } -} diff --git a/src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php b/src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php deleted file mode 100644 index be74f9aafc..0000000000 --- a/src/SoftDeleteable/Event/ORM/PreSoftDeleteEventArgs.php +++ /dev/null @@ -1,27 +0,0 @@ - http://www.gediminasm.org - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Gedmo\SoftDeleteable\Event\ORM; - -use Doctrine\ORM\EntityManagerInterface; -use Doctrine\ORM\Event\LifecycleEventArgs; -use Doctrine\Persistence\Event\LifecycleEventArgs as PersistenceLifecycleEventArgs; - -if (!class_exists(LifecycleEventArgs::class)) { - /** @template-extends PersistenceLifecycleEventArgs */ - final class PreSoftDeleteEventArgs extends PersistenceLifecycleEventArgs - { - } -} else { - final class PreSoftDeleteEventArgs extends LifecycleEventArgs - { - } -} diff --git a/src/SoftDeleteable/Event/ODM/PostSoftDeleteEventArgs.php b/src/SoftDeleteable/Event/PostSoftDeleteEventArgs.php similarity index 62% rename from src/SoftDeleteable/Event/ODM/PostSoftDeleteEventArgs.php rename to src/SoftDeleteable/Event/PostSoftDeleteEventArgs.php index d40745648f..f8a41338b2 100644 --- a/src/SoftDeleteable/Event/ODM/PostSoftDeleteEventArgs.php +++ b/src/SoftDeleteable/Event/PostSoftDeleteEventArgs.php @@ -9,10 +9,16 @@ * file that was distributed with this source code. */ -namespace Gedmo\SoftDeleteable\Event\ODM; +namespace Gedmo\SoftDeleteable\Event; -use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; +use Doctrine\Persistence\Event\LifecycleEventArgs; +use Doctrine\Persistence\ObjectManager; +/** + * @template TObjectManager of ObjectManager + * + * @template-extends LifecycleEventArgs + */ final class PostSoftDeleteEventArgs extends LifecycleEventArgs { } diff --git a/src/SoftDeleteable/Event/ODM/PreSoftDeleteEventArgs.php b/src/SoftDeleteable/Event/PreSoftDeleteEventArgs.php similarity index 61% rename from src/SoftDeleteable/Event/ODM/PreSoftDeleteEventArgs.php rename to src/SoftDeleteable/Event/PreSoftDeleteEventArgs.php index e35216c8cb..e01cd73d86 100644 --- a/src/SoftDeleteable/Event/ODM/PreSoftDeleteEventArgs.php +++ b/src/SoftDeleteable/Event/PreSoftDeleteEventArgs.php @@ -9,10 +9,16 @@ * file that was distributed with this source code. */ -namespace Gedmo\SoftDeleteable\Event\ODM; +namespace Gedmo\SoftDeleteable\Event; -use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; +use Doctrine\Persistence\Event\LifecycleEventArgs; +use Doctrine\Persistence\ObjectManager; +/** + * @template TObjectManager of ObjectManager + * + * @template-extends LifecycleEventArgs + */ final class PreSoftDeleteEventArgs extends LifecycleEventArgs { } diff --git a/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php b/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php index 07bcc32315..bffbbfd8d2 100644 --- a/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php +++ b/src/SoftDeleteable/Mapping/Event/Adapter/ODM.php @@ -9,12 +9,8 @@ namespace Gedmo\SoftDeleteable\Mapping\Event\Adapter; -use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; -use Doctrine\Persistence\ObjectManager; use Gedmo\Mapping\Event\Adapter\ODM as BaseAdapterODM; -use Gedmo\SoftDeleteable\Event\ODM\PostSoftDeleteEventArgs; -use Gedmo\SoftDeleteable\Event\ODM\PreSoftDeleteEventArgs; use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter; /** @@ -44,20 +40,4 @@ public function getDateValue($meta, $field) return $datetime; } - - /** - * @param DocumentManager $manager - */ - public function createPreSoftDeleteEventArgs(object $object, ObjectManager $manager): PreSoftDeleteEventArgs - { - return new PreSoftDeleteEventArgs($object, $manager); - } - - /** - * @param DocumentManager $manager - */ - public function createPostSoftDeleteEventArgs(object $object, ObjectManager $manager): PostSoftDeleteEventArgs - { - return new PostSoftDeleteEventArgs($object, $manager); - } } diff --git a/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php b/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php index bab3b43f76..6c28005890 100644 --- a/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php +++ b/src/SoftDeleteable/Mapping/Event/Adapter/ORM.php @@ -11,12 +11,8 @@ use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; -use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; -use Doctrine\Persistence\ObjectManager; use Gedmo\Mapping\Event\Adapter\ORM as BaseAdapterORM; -use Gedmo\SoftDeleteable\Event\ORM\PostSoftDeleteEventArgs; -use Gedmo\SoftDeleteable\Event\ORM\PreSoftDeleteEventArgs; use Gedmo\SoftDeleteable\Mapping\Event\SoftDeleteableAdapter; /** @@ -39,22 +35,6 @@ public function getDateValue($meta, $field) return $converter->convertToPHPValue($this->getRawDateValue($mapping), $platform); } - /** - * @param EntityManagerInterface $manager - */ - public function createPreSoftDeleteEventArgs(object $object, ObjectManager $manager): PreSoftDeleteEventArgs - { - return new PreSoftDeleteEventArgs($object, $manager); - } - - /** - * @param EntityManagerInterface $manager - */ - public function createPostSoftDeleteEventArgs(object $object, ObjectManager $manager): PostSoftDeleteEventArgs - { - return new PostSoftDeleteEventArgs($object, $manager); - } - /** * Generates current timestamp for the specified mapping * diff --git a/src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php b/src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php index ae433e42ae..a855c58c79 100644 --- a/src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php +++ b/src/SoftDeleteable/Mapping/Event/SoftDeleteableAdapter.php @@ -9,18 +9,13 @@ namespace Gedmo\SoftDeleteable\Mapping\Event; -use Doctrine\Persistence\Event\LifecycleEventArgs; use Doctrine\Persistence\Mapping\ClassMetadata; -use Doctrine\Persistence\ObjectManager; use Gedmo\Mapping\Event\AdapterInterface; /** * Doctrine event adapter for the SoftDeleteable extension. * * @author Gediminas Morkevicius - * - * @method LifecycleEventArgs createPreSoftDeleteEventArgs(object $object, ObjectManager $manager) - * @method LifecycleEventArgs createPostSoftDeleteEventArgs(object $object, ObjectManager $manager) */ interface SoftDeleteableAdapter extends AdapterInterface { diff --git a/src/SoftDeleteable/SoftDeleteableListener.php b/src/SoftDeleteable/SoftDeleteableListener.php index 39f2a38213..eae0260eb9 100644 --- a/src/SoftDeleteable/SoftDeleteableListener.php +++ b/src/SoftDeleteable/SoftDeleteableListener.php @@ -10,6 +10,7 @@ namespace Gedmo\SoftDeleteable; use Doctrine\Common\EventArgs; +use Doctrine\Common\EventManager; use Doctrine\ODM\MongoDB\DocumentManager; use Doctrine\ODM\MongoDB\UnitOfWork as MongoDBUnitOfWork; use Doctrine\ORM\EntityManagerInterface; @@ -17,6 +18,8 @@ use Doctrine\Persistence\Mapping\ClassMetadata; use Doctrine\Persistence\ObjectManager; use Gedmo\Mapping\MappedEventSubscriber; +use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs; +use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs; /** * SoftDeleteable listener @@ -81,15 +84,17 @@ public function onFlush(EventArgs $args) continue; // want to hard delete } - // @todo: in the next major remove check and call createPreSoftDeleteEventArgs - $preSoftDeleteEventArgs = method_exists($ea, 'createPreSoftDeleteEventArgs') - ? $ea->createPreSoftDeleteEventArgs($object, $om) - : $ea->createLifecycleEventArgsInstance($object, $om); + if ($evm->hasListeners(self::PRE_SOFT_DELETE)) { + // @todo: in the next major remove check and only instantiate the event + $preSoftDeleteEventArgs = $this->hasToDispatchNewEvent($evm, self::PRE_SOFT_DELETE, PreSoftDeleteEventArgs::class) + ? new PreSoftDeleteEventArgs($object, $om) + : $ea->createLifecycleEventArgsInstance($object, $om); - $evm->dispatchEvent( - self::PRE_SOFT_DELETE, - $preSoftDeleteEventArgs - ); + $evm->dispatchEvent( + self::PRE_SOFT_DELETE, + $preSoftDeleteEventArgs + ); + } $reflProp->setValue($object, $date); @@ -103,15 +108,17 @@ public function onFlush(EventArgs $args) ]); } - // @todo: in the next major remove check and call createPostSoftDeleteEventArgs - $postSoftDeleteEventArgs = method_exists($ea, 'createPostSoftDeleteEventArgs') - ? $ea->createPostSoftDeleteEventArgs($object, $om) - : $ea->createLifecycleEventArgsInstance($object, $om); + if ($evm->hasListeners(self::POST_SOFT_DELETE)) { + // @todo: in the next major remove check and only instantiate the event + $postSoftDeleteEventArgs = $this->hasToDispatchNewEvent($evm, self::POST_SOFT_DELETE, PostSoftDeleteEventArgs::class) + ? new PostSoftDeleteEventArgs($object, $om) + : $ea->createLifecycleEventArgsInstance($object, $om); - $evm->dispatchEvent( - self::POST_SOFT_DELETE, - $postSoftDeleteEventArgs - ); + $evm->dispatchEvent( + self::POST_SOFT_DELETE, + $postSoftDeleteEventArgs + ); + } } } } @@ -134,4 +141,32 @@ protected function getNamespace() { return __NAMESPACE__; } + + /** @param class-string $eventClass */ + private function hasToDispatchNewEvent(EventManager $eventManager, string $eventName, string $eventClass): bool + { + foreach ($eventManager->getListeners($eventName) as $listener) { + $reflMethod = new \ReflectionMethod($listener, $eventName); + + $parameters = $reflMethod->getParameters(); + + if ( + 1 !== count($parameters) + || !$parameters[0]->hasType() + || !$parameters[0]->getType() instanceof \ReflectionNamedType + || $eventClass !== $parameters[0]->getType()->getName() + ) { + @trigger_error(sprintf( + 'Type-hinting to something different than "%s" in "%s::%s()" is deprecated.', + $eventClass, + get_class($listener), + $reflMethod->getName() + ), E_USER_DEPRECATED); + + return false; + } + } + + return true; + } } diff --git a/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithLifecycleEventArgsFromODMTypeListener.php b/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithLifecycleEventArgsFromODMTypeListener.php new file mode 100644 index 0000000000..fdc592d810 --- /dev/null +++ b/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithLifecycleEventArgsFromODMTypeListener.php @@ -0,0 +1,35 @@ + http://www.gediminasm.org + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gedmo\Tests\SoftDeleteable\Fixture\Listener; + +use Doctrine\Common\EventSubscriber; +use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; +use Gedmo\SoftDeleteable\SoftDeleteableListener; + +final class WithLifecycleEventArgsFromODMTypeListener implements EventSubscriber +{ + public function preSoftDelete(LifecycleEventArgs $args): void + { + } + + public function postSoftDelete(LifecycleEventArgs $args): void + { + } + + public function getSubscribedEvents(): array + { + return [ + SoftDeleteableListener::PRE_SOFT_DELETE, + SoftDeleteableListener::POST_SOFT_DELETE, + ]; + } +} diff --git a/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithLifecycleEventArgsFromORMTypeListener.php b/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithLifecycleEventArgsFromORMTypeListener.php new file mode 100644 index 0000000000..5d936b7d5f --- /dev/null +++ b/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithLifecycleEventArgsFromORMTypeListener.php @@ -0,0 +1,35 @@ + http://www.gediminasm.org + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gedmo\Tests\SoftDeleteable\Fixture\Listener; + +use Doctrine\Common\EventSubscriber; +use Doctrine\ORM\Event\LifecycleEventArgs; +use Gedmo\SoftDeleteable\SoftDeleteableListener; + +final class WithLifecycleEventArgsFromORMTypeListener implements EventSubscriber +{ + public function preSoftDelete(LifecycleEventArgs $args): void + { + } + + public function postSoftDelete(LifecycleEventArgs $args): void + { + } + + public function getSubscribedEvents(): array + { + return [ + SoftDeleteableListener::PRE_SOFT_DELETE, + SoftDeleteableListener::POST_SOFT_DELETE, + ]; + } +} diff --git a/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithPreAndPostSoftDeleteEventArgsTypeListener.php b/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithPreAndPostSoftDeleteEventArgsTypeListener.php new file mode 100644 index 0000000000..b24c51f6d7 --- /dev/null +++ b/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithPreAndPostSoftDeleteEventArgsTypeListener.php @@ -0,0 +1,39 @@ + http://www.gediminasm.org + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gedmo\Tests\SoftDeleteable\Fixture\Listener; + +use Doctrine\Common\EventSubscriber; +use Doctrine\Persistence\ObjectManager; +use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs; +use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs; +use Gedmo\SoftDeleteable\SoftDeleteableListener; + +final class WithPreAndPostSoftDeleteEventArgsTypeListener implements EventSubscriber +{ + /** @param PreSoftDeleteEventArgs $args */ + public function preSoftDelete(PreSoftDeleteEventArgs $args): void + { + } + + /** @param PostSoftDeleteEventArgs $args */ + public function postSoftDelete(PostSoftDeleteEventArgs $args): void + { + } + + public function getSubscribedEvents(): array + { + return [ + SoftDeleteableListener::PRE_SOFT_DELETE, + SoftDeleteableListener::POST_SOFT_DELETE, + ]; + } +} diff --git a/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithoutTypeListener.php b/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithoutTypeListener.php new file mode 100644 index 0000000000..9ab5cf3a8e --- /dev/null +++ b/tests/Gedmo/SoftDeleteable/Fixture/Listener/WithoutTypeListener.php @@ -0,0 +1,39 @@ + http://www.gediminasm.org + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Gedmo\Tests\SoftDeleteable\Fixture\Listener; + +use Doctrine\Common\EventSubscriber; +use Doctrine\Persistence\ObjectManager; +use Gedmo\SoftDeleteable\Event\PostSoftDeleteEventArgs; +use Gedmo\SoftDeleteable\Event\PreSoftDeleteEventArgs; +use Gedmo\SoftDeleteable\SoftDeleteableListener; + +final class WithoutTypeListener implements EventSubscriber +{ + /** @param PreSoftDeleteEventArgs $args */ + public function preSoftDelete($args): void + { + } + + /** @param PostSoftDeleteEventArgs $args */ + public function postSoftDelete($args): void + { + } + + public function getSubscribedEvents(): array + { + return [ + SoftDeleteableListener::PRE_SOFT_DELETE, + SoftDeleteableListener::POST_SOFT_DELETE, + ]; + } +} diff --git a/tests/Gedmo/SoftDeleteable/SoftDeleteableDocumentTest.php b/tests/Gedmo/SoftDeleteable/SoftDeleteableDocumentTest.php index 7681f69f56..fcf8261f96 100644 --- a/tests/Gedmo/SoftDeleteable/SoftDeleteableDocumentTest.php +++ b/tests/Gedmo/SoftDeleteable/SoftDeleteableDocumentTest.php @@ -12,13 +12,13 @@ namespace Gedmo\Tests\SoftDeleteable; use Doctrine\Common\EventManager; -use Doctrine\Common\EventSubscriber; -use Gedmo\SoftDeleteable\Event\ODM\PostSoftDeleteEventArgs; -use Gedmo\SoftDeleteable\Event\ODM\PreSoftDeleteEventArgs; use Gedmo\SoftDeleteable\Filter\ODM\SoftDeleteableFilter; use Gedmo\SoftDeleteable\SoftDeleteableListener; use Gedmo\Tests\SoftDeleteable\Fixture\Document\User; use Gedmo\Tests\SoftDeleteable\Fixture\Document\UserTimeAware; +use Gedmo\Tests\SoftDeleteable\Fixture\Listener\WithLifecycleEventArgsFromODMTypeListener; +use Gedmo\Tests\SoftDeleteable\Fixture\Listener\WithoutTypeListener; +use Gedmo\Tests\SoftDeleteable\Fixture\Listener\WithPreAndPostSoftDeleteEventArgsTypeListener; use Gedmo\Tests\Tool\BaseTestCaseMongoODM; /** @@ -152,28 +152,22 @@ public function shouldSupportSoftDeleteableFilterTimeAware(): void public function testPostSoftDeleteEventIsDispatched(): void { - $subscriber = $this->getMockBuilder(EventSubscriber::class) - ->setMethods([ - 'getSubscribedEvents', - 'preSoftDelete', - 'postSoftDelete', - ]) - ->getMock(); + $this->dm->getEventManager()->addEventSubscriber(new WithPreAndPostSoftDeleteEventArgsTypeListener()); - $subscriber->expects(static::once()) - ->method('getSubscribedEvents') - ->willReturn([SoftDeleteableListener::PRE_SOFT_DELETE, SoftDeleteableListener::POST_SOFT_DELETE]); - - $subscriber->expects(static::once()) - ->method('preSoftDelete') - ->with(static::isInstanceOf(PreSoftDeleteEventArgs::class)); + $this->doTestPostSoftDeleteEventIsDispatched(); + } - $subscriber->expects(static::once()) - ->method('postSoftDelete') - ->with(static::isInstanceOf(PostSoftDeleteEventArgs::class)); + /** @group legacy */ + public function testPostSoftDeleteEventIsDispatchedWithDeprecatedListeners(): void + { + $this->dm->getEventManager()->addEventSubscriber(new WithoutTypeListener()); + $this->dm->getEventManager()->addEventSubscriber(new WithLifecycleEventArgsFromODMTypeListener()); - $this->dm->getEventManager()->addEventSubscriber($subscriber); + $this->doTestPostSoftDeleteEventIsDispatched(); + } + private function doTestPostSoftDeleteEventIsDispatched(): void + { $repo = $this->dm->getRepository(self::USER_CLASS); $newUser = new User(); diff --git a/tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php b/tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php index ed01012c5f..fcff3c745b 100644 --- a/tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php +++ b/tests/Gedmo/SoftDeleteable/SoftDeleteableEntityTest.php @@ -12,10 +12,8 @@ namespace Gedmo\Tests\SoftDeleteable; use Doctrine\Common\EventManager; -use Doctrine\Common\EventSubscriber; +use Doctrine\ORM\Event\LifecycleEventArgs; use Doctrine\ORM\Query; -use Gedmo\SoftDeleteable\Event\ORM\PostSoftDeleteEventArgs; -use Gedmo\SoftDeleteable\Event\ORM\PreSoftDeleteEventArgs; use Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter; use Gedmo\SoftDeleteable\Query\TreeWalker\SoftDeleteableWalker; use Gedmo\SoftDeleteable\SoftDeleteableListener; @@ -29,6 +27,9 @@ use Gedmo\Tests\SoftDeleteable\Fixture\Entity\Page; use Gedmo\Tests\SoftDeleteable\Fixture\Entity\User; use Gedmo\Tests\SoftDeleteable\Fixture\Entity\UserNoHardDelete; +use Gedmo\Tests\SoftDeleteable\Fixture\Listener\WithLifecycleEventArgsFromORMTypeListener; +use Gedmo\Tests\SoftDeleteable\Fixture\Listener\WithoutTypeListener; +use Gedmo\Tests\SoftDeleteable\Fixture\Listener\WithPreAndPostSoftDeleteEventArgsTypeListener; use Gedmo\Tests\Tool\BaseTestCaseORM; use Symfony\Component\Cache\Adapter\ArrayAdapter; @@ -531,52 +532,21 @@ public function testShouldFilterBeQueryCachedCorrectlyWhenToggledForEntity(): vo public function testPostSoftDeleteEventIsDispatched(): void { - $subscriber = $this->getMockBuilder(EventSubscriber::class) - ->setMethods([ - 'getSubscribedEvents', - 'preSoftDelete', - 'postSoftDelete', - ]) - ->getMock(); - - $subscriber->expects(static::once()) - ->method('getSubscribedEvents') - ->willReturn([ - SoftDeleteableListener::PRE_SOFT_DELETE, - SoftDeleteableListener::POST_SOFT_DELETE, - ]); - - $subscriber->expects(static::exactly(2)) - ->method('preSoftDelete') - ->with(static::isInstanceOf(PreSoftDeleteEventArgs::class)); - - $subscriber->expects(static::exactly(2)) - ->method('postSoftDelete') - ->with(static::isInstanceOf(PostSoftDeleteEventArgs::class)); - - $this->em->getEventManager()->addEventSubscriber($subscriber); + $this->em->getEventManager()->addEventSubscriber(new WithPreAndPostSoftDeleteEventArgsTypeListener()); - $repo = $this->em->getRepository(self::ARTICLE_CLASS); - - $comment = new Comment(); - $commentValue = 'Comment 1'; - $comment->setComment($commentValue); - $art0 = new Article(); - $field = 'title'; - $value = 'Title 1'; - $art0->setTitle($value); - $art0->addComment($comment); - - $this->em->persist($art0); - $this->em->flush(); + $this->doTestPostSoftDeleteEventIsDispatched(); + } - $art = $repo->findOneBy([$field => $value]); + /** @group legacy */ + public function testPostSoftDeleteEventIsDispatchedWithDeprecatedListeners(): void + { + $this->em->getEventManager()->addEventSubscriber(new WithoutTypeListener()); - static::assertNull($art->getDeletedAt()); - static::assertNull($comment->getDeletedAt()); + if (class_exists(LifecycleEventArgs::class)) { + $this->em->getEventManager()->addEventSubscriber(new WithLifecycleEventArgsFromORMTypeListener()); + } - $this->em->remove($art); - $this->em->flush(); + $this->doTestPostSoftDeleteEventIsDispatched(); } public function testShouldNotDeleteIfColumnNameDifferFromPropertyName(): void @@ -627,4 +597,29 @@ protected function getUsedEntityFixtures(): array self::USER_NO_HARD_DELETE_CLASS, ]; } + + private function doTestPostSoftDeleteEventIsDispatched(): void + { + $repo = $this->em->getRepository(self::ARTICLE_CLASS); + + $comment = new Comment(); + $commentValue = 'Comment 1'; + $comment->setComment($commentValue); + $art0 = new Article(); + $field = 'title'; + $value = 'Title 1'; + $art0->setTitle($value); + $art0->addComment($comment); + + $this->em->persist($art0); + $this->em->flush(); + + $art = $repo->findOneBy([$field => $value]); + + static::assertNull($art->getDeletedAt()); + static::assertNull($comment->getDeletedAt()); + + $this->em->remove($art); + $this->em->flush(); + } }