From 12553e5765201ab9764ace561348d58423e53825 Mon Sep 17 00:00:00 2001 From: Maxime Veber Date: Thu, 1 Feb 2024 11:20:42 +0100 Subject: [PATCH] Support proxy This commit also comes with a dependencies upgrade: - PHP 7.4 & 8.0 are no more supported - Symfony 4.x is not supported anymore --- .github/workflows/ci.yaml | 2 +- Tests/Event/DelayedListenerTest.php | 38 ++------------- ...ostLoadDispatcherInjectionListenerTest.php | 27 +++++++++++ Tests/SetupDatabaseTrait.php | 45 ++++++++++++++++++ Tests/fixtures/Entity/FakeModel.php | 41 +++++----------- Tests/fixtures/dbtest/fake_model.db | Bin 12288 -> 12288 bytes composer.json | 6 +-- phpunit.xml.dist | 43 ++++++++--------- phpunit.xml.dist.bak | 25 ++++++++++ .../PostLoadDispatcherInjectionListener.php | 33 +++++++++++++ .../EntitiesHasDispatcherChecker.php | 2 +- 11 files changed, 170 insertions(+), 92 deletions(-) create mode 100644 Tests/Model/Instantiator/PostLoadDispatcherInjectionListenerTest.php create mode 100644 Tests/SetupDatabaseTrait.php create mode 100644 phpunit.xml.dist.bak create mode 100644 src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5fa789c..65f55cd 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,7 +14,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - php: ['7.4', '8.0', '8.1'] + php: ['8.1', '8.2'] steps: - uses: actions/checkout@v2 diff --git a/Tests/Event/DelayedListenerTest.php b/Tests/Event/DelayedListenerTest.php index 1f17c01..2c8a709 100644 --- a/Tests/Event/DelayedListenerTest.php +++ b/Tests/Event/DelayedListenerTest.php @@ -7,17 +7,16 @@ use Biig\Component\Domain\Event\DomainEventDispatcher; use Biig\Component\Domain\Exception\InvalidDomainEvent; use Biig\Component\Domain\Model\DomainModel; -use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory; use Biig\Component\Domain\PostPersistListener\DoctrinePostPersistListener; use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface; use Biig\Component\Domain\Tests\fixtures\Entity\FakeModel; +use Biig\Component\Domain\Tests\SetupDatabaseTrait; use Doctrine\ORM\EntityManager; -use Doctrine\ORM\ORMSetup; use PHPUnit\Framework\TestCase; class DelayedListenerTest extends TestCase { - private $dbPath; + use SetupDatabaseTrait; public function testICanInstantiateDelayedListener() { @@ -62,7 +61,7 @@ public function testItFailsToRegisterOtherThanCurrentModel() public function testItInsertInBddAfterFlushing() { $dispatcher = new DomainEventDispatcher(); - $entityManager = $this->setupDatabase($dispatcher); + $entityManager = $this->setupDatabase($dispatcher, 'testItInsertInBddAfterFlushing'); $model = new FakeModel(); $model->setFoo('Model1'); @@ -104,7 +103,7 @@ public function testItDoesNotExecuteManyTimesSameEvent() { // Test setup $dispatcher = new DomainEventDispatcher(); - $entityManager = $this->setupDatabase($dispatcher); + $entityManager = $this->setupDatabase($dispatcher, 'testItInsertInBddAfterFlushing'); $model = new FakeModel(); $model->setFoo(0); @@ -122,35 +121,6 @@ public function testItDoesNotExecuteManyTimesSameEvent() $this->assertEquals(2, $model->getFoo()); $this->dropDatabase(); } - - private function setupDatabase(DomainEventDispatcher $dispatcher) - { - $this->dbPath = \sys_get_temp_dir() . '/testItInsertInBddAfterFlushing.' . \microtime() . '.sqlite'; - copy(__DIR__ . '/../fixtures/dbtest/initial_fake_model.db', $this->dbPath); - - $config = ORMSetup::createAnnotationMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true); - $config->setClassMetadataFactoryName(ClassMetadataFactory::class); - $conn = [ - 'driver' => 'pdo_sqlite', - 'path' => $this->dbPath, - ]; - - $entityManager = EntityManager::create($conn, $config); - $entityManager->getEventManager()->addEventSubscriber(new DoctrinePostPersistListener($dispatcher)); - - $entityManager->getMetadataFactory()->setDispatcher($dispatcher); - - return $entityManager; - } - - private function dropDatabase() - { - if (!$this->dbPath) { - return; - } - - @unlink($this->dbPath); - } } class FakeDomainModel extends DomainModel diff --git a/Tests/Model/Instantiator/PostLoadDispatcherInjectionListenerTest.php b/Tests/Model/Instantiator/PostLoadDispatcherInjectionListenerTest.php new file mode 100644 index 0000000..0ccfca8 --- /dev/null +++ b/Tests/Model/Instantiator/PostLoadDispatcherInjectionListenerTest.php @@ -0,0 +1,27 @@ +setupDatabase($dispatcher, 'testPostLoadDispatcherInjection'); + $entity = $entityManager->getRepository(FakeModel::class)->find(1); + + $this->assertTrue($entity->hasDispatcher()); + } + + public function testItLoadsDispatcherInProxyEntity() + { + + } +} diff --git a/Tests/SetupDatabaseTrait.php b/Tests/SetupDatabaseTrait.php new file mode 100644 index 0000000..2c2494b --- /dev/null +++ b/Tests/SetupDatabaseTrait.php @@ -0,0 +1,45 @@ +dbPath = \sys_get_temp_dir() . '/'.$name.'.' . \microtime() . '.sqlite'; + copy(__DIR__ . '/fixtures/dbtest/initial_fake_model.db', $this->dbPath); + + $config = ORMSetup::createAttributeMetadataConfiguration(array(__DIR__ . '/../fixtures/Entity'), true); + $config->setClassMetadataFactoryName(ClassMetadataFactory::class); + $conn = [ + 'driver' => 'pdo_sqlite', + 'path' => $this->dbPath, + ]; + + $entityManager = EntityManager::create($conn, $config); + $entityManager->getEventManager()->addEventSubscriber(new DoctrinePostPersistListener($dispatcher)); + $entityManager->getEventManager()->addEventSubscriber(new PostLoadDispatcherInjectionListener($dispatcher)); + + $entityManager->getMetadataFactory()->setDispatcher($dispatcher); + + return $entityManager; + } + + private function dropDatabase() + { + if (!$this->dbPath) { + return; + } + + @unlink($this->dbPath); + } +} diff --git a/Tests/fixtures/Entity/FakeModel.php b/Tests/fixtures/Entity/FakeModel.php index 780319a..1f1cf14 100644 --- a/Tests/fixtures/Entity/FakeModel.php +++ b/Tests/fixtures/Entity/FakeModel.php @@ -3,22 +3,18 @@ use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity() - * @ORM\Table(name="fake_model") - */ +#[ORM\Entity] +#[ORM\Table(name: 'fake_model')] class FakeModel implements \Biig\Component\Domain\Model\ModelInterface { use \Biig\Component\Domain\Model\DomainModelTrait; - /** - * @ORM\Id - * @ORM\GeneratedValue - * @ORM\Column(type="integer") - */ + #[ORM\Id()] + #[ORM\GeneratedValue] + #[ORM\Column(type: 'integer')] private $id; - /** @ORM\Column() */ + #[ORM\Column] private $foo; private $something; @@ -27,42 +23,27 @@ public function hasDispatcher() return null !== $this->dispatcher; } - /** - * @return int - */ - public function getId() + public function getId(): int { return $this->id; } - /** - * @return string - */ - public function getFoo() + public function getFoo(): string { return $this->foo; } - /** - * @return mixed - */ - public function getSomething() + public function getSomething(): mixed { return $this->something; } - /** - * @param mixed $something - */ - public function setSomething($something) + public function setSomething(string $something) { $this->something = $something; } - /** - * @param string $foo - */ - public function setFoo($foo) + public function setFoo(string $foo) { $this->foo = $foo; } diff --git a/Tests/fixtures/dbtest/fake_model.db b/Tests/fixtures/dbtest/fake_model.db index c86adb6b3ab44229f21c629a4531817a901d7e1c..c43729ed9a32ec3d1c62eb7a82774db5fc6703c1 100644 GIT binary patch delta 78 zcmZojXh@hK&6qb)##u0rK`(D7F9QPu6TdYBe=ooFW=7.4", + "php": ">=8.1", "symfony/event-dispatcher": "^4.3|^5.0|^6.0", "doctrine/doctrine-bundle": "^1.8|^2.0", "doctrine/orm": "^2.6.3" @@ -15,10 +15,10 @@ } ], "require-dev": { - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^9.5 || ^10", "doctrine/orm": "^2.6.3", "friendsofphp/php-cs-fixer": "^3.11.0", - "symfony/symfony": "^4.3 || ^5.0 || ^6.0", + "symfony/symfony": "^5.0 || ^6.0 || ^7.0", "phpspec/prophecy-phpunit": "^2.0.1", "monolog/monolog": "^2.8.0 || ^3.2.0" }, diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e7c2df6..f44639b 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,25 +1,22 @@ - - - - - - - - - - ./Tests - - - - - - ./ - - ./docs - ./Tests - ./vendor - - - + + + + ./ + + + ./docs + ./Tests + ./vendor + + + + + + + + + ./Tests + + diff --git a/phpunit.xml.dist.bak b/phpunit.xml.dist.bak new file mode 100644 index 0000000..e7c2df6 --- /dev/null +++ b/phpunit.xml.dist.bak @@ -0,0 +1,25 @@ + + + + + + + + + + + ./Tests + + + + + + ./ + + ./docs + ./Tests + ./vendor + + + + diff --git a/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php b/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php new file mode 100644 index 0000000..d0b1561 --- /dev/null +++ b/src/Model/Instantiator/DoctrineConfig/PostLoadDispatcherInjectionListener.php @@ -0,0 +1,33 @@ +getObject(); + + if ($entity instanceof DomainModel) { + $entity->setDispatcher($this->dispatcher); + } + } +} diff --git a/src/PostFlushListener/EntitiesHasDispatcherChecker.php b/src/PostFlushListener/EntitiesHasDispatcherChecker.php index 793c545..ee8bf78 100644 --- a/src/PostFlushListener/EntitiesHasDispatcherChecker.php +++ b/src/PostFlushListener/EntitiesHasDispatcherChecker.php @@ -28,7 +28,7 @@ public function postFlush(PostFlushEventArgs $args): void private function checkModelEntityHasDispatcher(object $entity, EntityManagerInterface $entityManager): void { - if (!$entity instanceof ModelInterface || $entity instanceof Proxy) { + if (!$entity instanceof ModelInterface) { return; }