diff --git a/Tests/Event/DelayedListenerTest.php b/Tests/Event/DelayedListenerTest.php index 7182f41..76512e9 100644 --- a/Tests/Event/DelayedListenerTest.php +++ b/Tests/Event/DelayedListenerTest.php @@ -2,9 +2,16 @@ namespace Biig\Component\Domain\Tests\Event; +require_once __DIR__ . '/../fixtures/FakeModel.php'; + use Biig\Component\Domain\Event\DelayedListener; use Biig\Component\Domain\Event\DomainEvent; +use Biig\Component\Domain\Event\DomainEventDispatcher; use Biig\Component\Domain\Model\DomainModel; +use Biig\Component\Domain\Model\Instantiator\DoctrineConfig\ClassMetadataFactory; +use Biig\Component\Domain\Rule\PostPersistDomainRuleInterface; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Tools\Setup; use PHPUnit\Framework\TestCase; class DelayedListenerTest extends TestCase @@ -47,6 +54,57 @@ public function testItFailsToRegisterOtherThanCurrentModel() $listener = new DelayedListener('foo', function () {}); $listener->occur(new DomainEvent($model)); } + + public function testItInsertInBddAfterFlushing() + { + $tmpPath = \sys_get_temp_dir() . '/testItInsertInBddAfterFlushing.' . \microtime() . '.sqlite'; + copy(__DIR__ . '/../fixtures/dbtest/fake_model.db', $tmpPath); + + $config = Setup::createYAMLMetadataConfiguration(array(__DIR__ . '/../fixtures/config'), true); + $config->setClassMetadataFactoryName(ClassMetadataFactory::class); + $conn = [ + 'driver' => 'pdo_sqlite', + 'path' => $tmpPath, + ]; + $entityManager = EntityManager::create($conn, $config); + + $model = new \FakeModel(); + $model->setFoo('Model 1'); + $dispatcher = new DomainEventDispatcher(); + $model->setDispatcher($dispatcher); + + $entityManager->getMetadataFactory()->setDispatcher($dispatcher); + $rule = new class($entityManager) implements PostPersistDomainRuleInterface { + private $entityManager; + + public function __construct(EntityManager $entityManager) + { + $this->entityManager = $entityManager; + } + + public function after() + { + return [\FakeModel::class => 'action']; + } + + public function execute(\Biig\Component\Domain\Event\DomainEvent $event) + { + $model = new \FakeModel(); + $model->setFoo('RulePostPersist'); + $this->entityManager->persist($model); + $this->entityManager->flush($model); + } + }; + $dispatcher->addRule($rule); + + $entityManager->persist($model); + $entityManager->flush($model); + $model->doAction(); + $dispatcher->persistModel($model); + + $this->assertEquals(count($entityManager->getRepository(\FakeModel::class)->findAll()), 3); + @unlink($tmpPath); + } } class FakeDomainModel extends DomainModel diff --git a/Tests/Symfony/DependencyInjection/DomainExtensionTest.php b/Tests/Symfony/DependencyInjection/DomainExtensionTest.php index 372bb3c..45494d2 100644 --- a/Tests/Symfony/DependencyInjection/DomainExtensionTest.php +++ b/Tests/Symfony/DependencyInjection/DomainExtensionTest.php @@ -43,7 +43,7 @@ public function testItSetEntityManagersConfigAsParameterOfContainer() $config = [[ 'entity_managers' => [ 'default', - 'customManager' + 'customManager', ], ]]; diff --git a/src/Event/DelayedListener.php b/src/Event/DelayedListener.php index 6190266..1b29811 100644 --- a/src/Event/DelayedListener.php +++ b/src/Event/DelayedListener.php @@ -63,11 +63,10 @@ public function occur(DomainEvent $event) */ public function process(ModelInterface $model) { - $stack = $this->eventStack; - foreach ($stack as $key => $event) { + foreach ($this->eventStack as $key => $event) { if (spl_object_hash($event->getSubject()) === spl_object_hash($model)) { - \call_user_func($this->listener, $event); unset($this->eventStack[$key]); + \call_user_func($this->listener, $event); } } }