diff --git a/composer.json b/composer.json index f2b34ec..499ee60 100755 --- a/composer.json +++ b/composer.json @@ -31,19 +31,19 @@ } }, "require-dev": { - "symfony/yaml": "^5.0|^6.0", - "symfony/validator": "^5.2|^6.0", + "symfony/yaml": "^6.4", + "symfony/validator": "^6.4", "phpstan/phpstan-symfony": "^1.0", - "assoconnect/php-quality-config": "^1.10", - "symfony/security-core": "^5.0|^6.0", + "assoconnect/php-quality-config": "^1.16", + "symfony/security-core": "^6.4", "rector/rector": "^1.2" }, "require": { "php": "^8.2", - "symfony/framework-bundle": "^5.3|^6.0|^7.0", + "symfony/framework-bundle": "^6.4|^7.0", "doctrine/dbal": "^2.10|^3.0", - "symfony/serializer": "^5.1|^6.0", - "symfony/property-access": "^5.4|^6.0", + "symfony/serializer": "^6.4", + "symfony/property-access": "^6.4", "doctrine/doctrine-bundle": "^2.11", "doctrine/orm": "^2.9", "moneyphp/money": "^3.2|^4.0", diff --git a/src/Subscriber/LoggerSubscriber.php b/src/Subscriber/LoggerSubscriber.php index 81b870c..a038e29 100755 --- a/src/Subscriber/LoggerSubscriber.php +++ b/src/Subscriber/LoggerSubscriber.php @@ -8,7 +8,7 @@ use AssoConnect\LogBundle\Factory\LogFactoryInterface; use AssoConnect\LogBundle\Factory\RequestContextAwareLogFactoryInterface; use AssoConnect\LogBundle\Factory\SecurityContextAwareLogFactoryInterface; -use Doctrine\Common\EventSubscriber; +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\Events; @@ -16,7 +16,8 @@ * This Doctrine subscriber creates a Log entity every time * a fully Doctrine-managed entity is persisted, updated, or removed. */ -class LoggerSubscriber implements EventSubscriber +#[AsDoctrineListener(event: Events::onFlush)] +class LoggerSubscriber { public function __construct( private readonly LogFactoryInterface $factory, @@ -25,11 +26,6 @@ public function __construct( ) { } - public function getSubscribedEvents(): array - { - return [Events::onFlush]; - } - public function onFlush(OnFlushEventArgs $eventArgs): void { $em = $eventArgs->getEntityManager(); diff --git a/tests/Factory/LogDataFactoryTest.php b/tests/Factory/LogDataFactoryTest.php index 7831492..95d39e6 100644 --- a/tests/Factory/LogDataFactoryTest.php +++ b/tests/Factory/LogDataFactoryTest.php @@ -71,7 +71,7 @@ public function testUpdatedEntityIsLogged(): void $unitOfWork->method('getEntityChangeSet')->with($updatedAuthor)->willReturn( [ 'email' => ['test@gmail.com'], - 'registeredAt' => [new \DateTime('2020-10-06')], + 'registeredAt' => [new \DateTimeImmutable('2020-10-06')], 'unmappedField' => ['test'], 'address' => [new Address()], ] diff --git a/tests/Functional/App/config/config.yml b/tests/Functional/App/config/config.yml index f130885..386375d 100755 --- a/tests/Functional/App/config/config.yml +++ b/tests/Functional/App/config/config.yml @@ -5,7 +5,7 @@ framework: enabled: false validation: enabled: true - enable_annotations: true + enable_attributes: true doctrine: orm: mappings: diff --git a/tests/Functional/Entity/AbstractEntity.php b/tests/Functional/Entity/AbstractEntity.php index c104ca2..f0ccc2c 100755 --- a/tests/Functional/Entity/AbstractEntity.php +++ b/tests/Functional/Entity/AbstractEntity.php @@ -4,7 +4,6 @@ namespace AssoConnect\LogBundle\Tests\Functional\Entity; -use DateTime; use DateTimeInterface; use Doctrine\ORM\Mapping as ORM; @@ -16,8 +15,8 @@ abstract class AbstractEntity public function __construct() { $this->id = rand(0, 10000000); - $this->createdAt = new DateTime(); - $this->updatedAt = new DateTime(); + $this->createdAt = new \DateTimeImmutable(); + $this->updatedAt = new \DateTimeImmutable(); } public function __toString(): string diff --git a/tests/Functional/Entity/Author.php b/tests/Functional/Entity/Author.php index 7ae3019..133153d 100755 --- a/tests/Functional/Entity/Author.php +++ b/tests/Functional/Entity/Author.php @@ -13,7 +13,7 @@ class Author extends AbstractEntity public function __construct() { parent::__construct(); - $this->registeredAt = new \DateTime(); + $this->registeredAt = new \DateTimeImmutable(); } #[ORM\Column(type: 'email')] diff --git a/tests/Serializer/LogSerializerTest.php b/tests/Serializer/LogSerializerTest.php index 122784e..45eff35 100755 --- a/tests/Serializer/LogSerializerTest.php +++ b/tests/Serializer/LogSerializerTest.php @@ -12,7 +12,6 @@ use AssoConnect\LogBundle\Tests\Functional\Entity\ObjectWithoutId; use AssoConnect\LogBundle\Tests\Functional\Entity\Post; use AssoConnect\LogBundle\Tests\Functional\Entity\Tag; -use DateTime; use DateTimeZone; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\ORM\EntityManagerInterface; @@ -104,8 +103,8 @@ public function providerFormatValueAsString(): iterable yield [1.5, '1.5']; yield [[1.5], '[1.5]']; - yield [new DateTime('@1529500134'), '"2018-06-20T13:08:54+0000"']; - yield [[new DateTime('@1529500134')], '["2018-06-20T13:08:54+0000"]']; + yield [new \DateTimeImmutable('@1529500134'), '"2018-06-20T13:08:54+0000"']; + yield [[new \DateTimeImmutable('@1529500134')], '["2018-06-20T13:08:54+0000"]']; yield [new DateTimeZone('Europe/Paris'), '"Europe\/Paris"']; yield [[new DateTimeZone('Europe/Paris')], '["Europe\/Paris"]']; diff --git a/tests/Subscriber/LoggerSubscriberTest.php b/tests/Subscriber/LoggerSubscriberTest.php index 78e70d4..a64405b 100755 --- a/tests/Subscriber/LoggerSubscriberTest.php +++ b/tests/Subscriber/LoggerSubscriberTest.php @@ -6,11 +6,10 @@ use AssoConnect\LogBundle\Entity\Log; use AssoConnect\LogBundle\Factory\LogDataFactory; -use AssoConnect\LogBundle\Factory\LogFactoryInterface; use AssoConnect\LogBundle\Subscriber\LoggerSubscriber; use AssoConnect\LogBundle\Tests\Functional\Entity\Address; use AssoConnect\LogBundle\Tests\Functional\Service\LogFactory; -use Doctrine\Common\EventManager; +use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\Events; @@ -21,16 +20,15 @@ class LoggerSubscriberTest extends KernelTestCase { - public function testEventSubscription(): void + public function testEventSubscriptionAsAnAttribute(): void { - self::assertSame( - [Events::onFlush], - (new LoggerSubscriber( - $this->createMock(LogFactoryInterface::class), - $this->createMock(LogDataFactory::class), - realpath(__DIR__ . '/../..') - ))->getSubscribedEvents() - ); + $reflection = new \ReflectionClass(LoggerSubscriber::class); + $attributes = $reflection->getAttributes(AsDoctrineListener::class); + + self::assertCount(1, $attributes, 'LoggerSubscriber should have AsDoctrineListener attribute'); + + $attribute = $attributes[0]->newInstance(); + self::assertSame(Events::onFlush, $attribute->event); } public function testSubscriberPersistsLogs(): void @@ -71,8 +69,6 @@ public function testSubscriberPersistsLogs(): void realpath(__DIR__ . '/../..') ); - $dispatcher = new EventManager(); - $dispatcher->addEventSubscriber($subscriber); - $dispatcher->dispatchEvent(Events::onFlush, $event); + $subscriber->onFlush($event); } }