Skip to content

Commit

Permalink
Merge pull request #32 from biig-io/feature/correct-error-on-db-flushing
Browse files Browse the repository at this point in the history
Feature/correct error on db flushing
  • Loading branch information
Maxime Veber authored Apr 10, 2018
2 parents 3b0f9d6 + 26313a0 commit 3583af5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
58 changes: 58 additions & 0 deletions Tests/Event/DelayedListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Tests/Symfony/DependencyInjection/DomainExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testItSetEntityManagersConfigAsParameterOfContainer()
$config = [[
'entity_managers' => [
'default',
'customManager'
'customManager',
],
]];

Expand Down
5 changes: 2 additions & 3 deletions src/Event/DelayedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down

0 comments on commit 3583af5

Please sign in to comment.