Skip to content

Commit

Permalink
fix: collect events at the beginning of `dispatchPostFlushDomainEvent…
Browse files Browse the repository at this point in the history
…s()`, fix manual dispatching postFlush events without preFlush. (#98)

* fix: collect events at the beginning of `dispatchPostFlushDomainEvents()`, fix manual dispatching postFlush events without preFlush.

* add tests

* cs
  • Loading branch information
priyadi authored May 30, 2024
1 parent 38ac31b commit 7a34180
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 2.3.4

* fix: collect events at the beginning of `dispatchPostFlushDomainEvents()`, fix
manual dispatching postFlush events without preFlush.

## 2.3.3

* fix: Should fix `ManagerRegistry::resetManager()`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ private function preFlushDispatch(): int

public function dispatchPostFlushDomainEvents(): int
{
$this->collectEvents();

$num = \count($this->postFlushDomainEvents);
$events = $this->postFlushDomainEvents->pop();
// for safeguard we also clear preflush events here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function onRemoveCalled(): bool
{
return $this->onRemoveCalled;
}

public function onChangeCalled(): int
{
return $this->onChangeCalled;
Expand Down
37 changes: 37 additions & 0 deletions tests/Framework/Tests/BasicDomainEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,41 @@ public function testPostFlushListener(): void

$this->assertTrue($listener->onCreateCalled());
}

public function testManualPreFlush(): void
{
$entityManager = static::getEntityManager();
$listener = static::getContainer()->get(BookEventPreFlushListener::class);
$this->assertInstanceOf(BookEventPreFlushListener::class, $listener);

$entityManager->setAutoDispatchDomainEvents(false);

$this->assertFalse($listener->onCreateCalled());

$book = new Book('title', 'description');
$entityManager->persist($book);
$entityManager->dispatchPreFlushDomainEvents();
$entityManager->flush();
$entityManager->clearDomainEvents();

$this->assertTrue($listener->onCreateCalled());
}

public function testManualPostFlush(): void
{
$entityManager = static::getEntityManager();
$listener = static::getContainer()->get(BookEventPostFlushListener::class);
$this->assertInstanceOf(BookEventPostFlushListener::class, $listener);

$entityManager->setAutoDispatchDomainEvents(false);

$this->assertFalse($listener->onCreateCalled());

$book = new Book('title', 'description');
$entityManager->persist($book);
$entityManager->flush();
$entityManager->dispatchPostFlushDomainEvents();

$this->assertTrue($listener->onCreateCalled());
}
}

0 comments on commit 7a34180

Please sign in to comment.