From e5cf82066a30bdea371e8899f15da85c9c848075 Mon Sep 17 00:00:00 2001 From: saifulislamferoz Date: Wed, 19 Nov 2025 18:34:30 +0600 Subject: [PATCH] fix: configured event not working For any configured event it always fired all event --- Attribute/SubscribeDoctrineEvents.php | 16 +--- .../Attribute/SubscribeDoctrineEventsTest.php | 28 +++---- Tests/Fixtures/ODM/UnitOfWork.php | 8 +- Tests/Fixtures/ORM/Movie.php | 9 +-- Tests/Fixtures/ORM/UnitOfWork.php | 8 +- Tests/Subscriber/DoctrineSubscriberTest.php | 76 ++++++++----------- 6 files changed, 55 insertions(+), 90 deletions(-) diff --git a/Attribute/SubscribeDoctrineEvents.php b/Attribute/SubscribeDoctrineEvents.php index 81e2760..135da00 100644 --- a/Attribute/SubscribeDoctrineEvents.php +++ b/Attribute/SubscribeDoctrineEvents.php @@ -16,22 +16,14 @@ * * @author Roni Saha */ - #[\Attribute(\Attribute::TARGET_CLASS)] -/* @final */ class SubscribeDoctrineEvents +class SubscribeDoctrineEvents { - public $events = array(); + public array $events = []; - public function __construct(array $values) + public function __construct(array|string $values) { - if (isset($values['value'])) { - $values['events'] = $values['value']; - } - if (!isset($values['events'])) { - return; - } - - $this->events = is_array($values['events']) ? $values['events'] : array_map('trim', explode(',', $values['events'])); + $this->events = is_array($values) ? $values : array_map('trim', explode(',', $values)); $this->events = array_filter($this->events); } diff --git a/Tests/Attribute/SubscribeDoctrineEventsTest.php b/Tests/Attribute/SubscribeDoctrineEventsTest.php index 1be9671..43f2c35 100644 --- a/Tests/Attribute/SubscribeDoctrineEventsTest.php +++ b/Tests/Attribute/SubscribeDoctrineEventsTest.php @@ -18,46 +18,36 @@ class SubscribeDoctrineEventsTest extends TestCase { public function testConstructWithoutData() { - $annotation = new SubscribeDoctrineEvents(array()); + $annotation = new SubscribeDoctrineEvents([]); $this->assertTrue(is_array($annotation->events)); $this->assertEmpty($annotation->events); - } - - public function testConstructWithInvalidData() - { - $data = array( - 'unknown' => 'foo', - 'array' => array('bar' => 'bar'), - ); - - $annotation = new SubscribeDoctrineEvents($data); - - $this->assertTrue(is_array($annotation->events)); + $annotation = new SubscribeDoctrineEvents(""); $this->assertEmpty($annotation->events); } - public function testConstructWithValue() + + public function testConstructWithStringValue() { - $data = array('value' => 'updated,created'); + $data = 'created,updated'; $annotation = new SubscribeDoctrineEvents($data); $this->assertTrue(is_array($annotation->events)); $this->assertNotEmpty($annotation->events); - $this->assertEquals(explode(',', $data['value']), $annotation->events); + $this->assertEquals(explode(',', $data), $annotation->events); } - public function testConstructWithEvent() + public function testConstructWithArrayValue() { - $data = array('events' => 'updated,created'); + $data = ['created', 'updated']; $annotation = new SubscribeDoctrineEvents($data); $this->assertTrue(is_array($annotation->events)); $this->assertNotEmpty($annotation->events); - $this->assertEquals(explode(',', $data['events']), $annotation->events); + $this->assertEquals($data, $annotation->events); } } diff --git a/Tests/Fixtures/ODM/UnitOfWork.php b/Tests/Fixtures/ODM/UnitOfWork.php index 4d78e60..10c4d79 100644 --- a/Tests/Fixtures/ODM/UnitOfWork.php +++ b/Tests/Fixtures/ODM/UnitOfWork.php @@ -10,12 +10,12 @@ class UnitOfWork implements PropertyChangedListener /** * Collect information about a property change. * - * @param object $sender the object on which the property changed + * @param object $sender the object on which the property changed * @param string $propertyName the name of the property that changed - * @param mixed $oldValue the old value of the property that changed - * @param mixed $newValue the new value of the property that changed + * @param mixed $oldValue the old value of the property that changed + * @param mixed $newValue the new value of the property that changed */ - public function propertyChanged($sender, $propertyName, $oldValue, $newValue) + public function propertyChanged($sender, $propertyName, $oldValue, $newValue): void { } diff --git a/Tests/Fixtures/ORM/Movie.php b/Tests/Fixtures/ORM/Movie.php index 12e57bd..e70571e 100644 --- a/Tests/Fixtures/ORM/Movie.php +++ b/Tests/Fixtures/ORM/Movie.php @@ -21,19 +21,12 @@ #[ORM\Entity] class Movie { - /** - * @ORM\Id - * @ORM\Column(type="integer") - * @ORM\GeneratedValue(strategy="AUTO") - */ + #[ORM\Id] #[ORM\Column(type: 'integer')] #[ORM\GeneratedValue(strategy: "AUTO")] protected $id; - /** - * @ORM\Column(type="string") - */ #[ORM\Column(type: 'string')] protected $name; diff --git a/Tests/Fixtures/ORM/UnitOfWork.php b/Tests/Fixtures/ORM/UnitOfWork.php index a093e6c..d8e2056 100644 --- a/Tests/Fixtures/ORM/UnitOfWork.php +++ b/Tests/Fixtures/ORM/UnitOfWork.php @@ -10,12 +10,12 @@ class UnitOfWork implements PropertyChangedListener /** * Collect information about a property change. * - * @param object $sender the object on which the property changed + * @param object $sender the object on which the property changed * @param string $propertyName the name of the property that changed - * @param mixed $oldValue the old value of the property that changed - * @param mixed $newValue the new value of the property that changed + * @param mixed $oldValue the old value of the property that changed + * @param mixed $newValue the new value of the property that changed */ - public function propertyChanged($sender, $propertyName, $oldValue, $newValue) + public function propertyChanged($sender, $propertyName, $oldValue, $newValue): void { } diff --git a/Tests/Subscriber/DoctrineSubscriberTest.php b/Tests/Subscriber/DoctrineSubscriberTest.php index 47f072e..198cead 100644 --- a/Tests/Subscriber/DoctrineSubscriberTest.php +++ b/Tests/Subscriber/DoctrineSubscriberTest.php @@ -13,19 +13,18 @@ use Doctrine\Persistence\Event\LifecycleEventArgs; use Doctrine\Persistence\Mapping\ClassMetadata; +use Doctrine\Persistence\ObjectManager; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Xiidea\EasyAuditBundle\Subscriber\DoctrineSubscriber; use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\DummyEntity; use Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie; -use Doctrine\Persistence\ObjectManager; class DoctrineSubscriberTest extends TestCase { /** @var MockObject */ private $dispatcher; - /** @var MockObject */ private $entityManager; @@ -43,86 +42,77 @@ public function setUp(): void } - public function testCreateEventForAttributedEntity() - { - $subscriber = new DoctrineSubscriber(array()); - - $this->invokeCreatedEventCall($subscriber); - } - - public function testCreateEventForEntityNotConfiguredToTrack() - { - $subscriber = new DoctrineSubscriber(array()); - $this->invokeCreatedEventCall($subscriber, new DummyEntity()); - } - - public function testCreateEventForEntityConfiguredToTrack() - { - $subscriber = new DoctrineSubscriber( - array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array('created')) - ); - - $this->invokeCreatedEventCall($subscriber); - } - - public function testCreateEventForEntityConfiguredToTrackAllEvents() + public function testEntityConfigureToTrackForAttributedEntity() { - $subscriber = new DoctrineSubscriber(array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array())); + $subscriber = new DoctrineSubscriber([]); - $this->invokeCreatedEventCall($subscriber); + $subscriber->setDispatcher($this->dispatcher); + $subscriber->postPersist(new LifecycleEventArgs(new Movie(), $this->entityManager)); + $subscriber->postUpdate(new LifecycleEventArgs(new Movie(), $this->entityManager)); + $subscriber->preRemove(new LifecycleEventArgs(new Movie(), $this->entityManager)); + $subscriber->postRemove(new LifecycleEventArgs(new Movie(), $this->entityManager)); + $this->assertTrue(true); } - public function testUpdateEventForEntityNotConfiguredToTrack() + public function testEntityNotConfiguredToTrack() { - $subscriber = new DoctrineSubscriber(array()); - $this->invokeUpdatedEventCall($subscriber, new DummyEntity()); + $subscriber = new DoctrineSubscriber([]); + $subscriber->setDispatcher($this->dispatcher); + $subscriber->postPersist(new LifecycleEventArgs(new DummyEntity(), $this->entityManager)); + $this->assertTrue(true); } public function testRemovedEventForEntityNotConfiguredToTrack() { - $subscriber = new DoctrineSubscriber(array()); + $subscriber = new DoctrineSubscriber([]); $this->invokeDeletedEventCall($subscriber); + $this->assertTrue(true); } public function testRemovedEventForEntityConfiguredToTrackAllEvent() { $this->mockMetaData(); - $subscriber = new DoctrineSubscriber(array('Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => array())); + $subscriber = new DoctrineSubscriber(['Xiidea\EasyAuditBundle\Tests\Fixtures\ORM\Movie' => []]); $this->invokeDeletedEventCall($subscriber); + $this->assertTrue(true); } - /** - * @param DoctrineSubscriber $subscriber + * @return void */ - private function invokeCreatedEventCall($subscriber, $entity = null) + public function testCreatedEventTrackEntityViaYaml(): void { + $subscriber = new DoctrineSubscriber([DummyEntity::class => ['created']]); $subscriber->setDispatcher($this->dispatcher); - $subscriber->postPersist(new LifecycleEventArgs($entity ?? new Movie(), $this->entityManager)); + $dummyClass = new DummyEntity(); + $subscriber->preRemove(new LifecycleEventArgs($dummyClass, $this->entityManager)); + $subscriber->postRemove(new LifecycleEventArgs($dummyClass, $this->entityManager)); $this->assertTrue(true); } - /** - * @param DoctrineSubscriber $subscriber - */ - private function invokeUpdatedEventCall($subscriber, $entity = null) + + public function testAnyEventToTrackConfigureViaYaml(): void { + $subscriber = new DoctrineSubscriber([DummyEntity::class => []]); $subscriber->setDispatcher($this->dispatcher); - - $subscriber->postUpdate(new LifecycleEventArgs($entity ?? new Movie(), $this->entityManager)); + $dummyClass = new DummyEntity(); + $subscriber->preRemove(new LifecycleEventArgs($dummyClass, $this->entityManager)); + $subscriber->postRemove(new LifecycleEventArgs($dummyClass, $this->entityManager)); $this->assertTrue(true); } + + /** * @param DoctrineSubscriber $subscriber */ private function invokeDeletedEventCall($subscriber) { $subscriber->setDispatcher($this->dispatcher); + $movie = new Movie(); $subscriber->preRemove(new LifecycleEventArgs($movie, $this->entityManager)); $subscriber->postRemove(new LifecycleEventArgs($movie, $this->entityManager)); - $this->assertTrue(true); } private function mockMetaData($data = ['id' => 1])