Skip to content

Commit

Permalink
Merge pull request #286 from nlx-lars/bugfix/clear-pending-events
Browse files Browse the repository at this point in the history
BUGFIX: Clear $pendingEvents when invoking DeferEventPublisher
  • Loading branch information
bwaidelich authored Mar 9, 2021
2 parents fc04603 + 0f59af4 commit d2eef41
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions Classes/EventPublisher/DeferEventPublisher.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function invoke(): void
{
if (!$this->pendingEvents->isEmpty()) {
$this->wrappedEventPublisher->publish($this->pendingEvents);
$this->pendingEvents = DomainEvents::createEmpty();
}
}

Expand Down
61 changes: 61 additions & 0 deletions Tests/Unit/EventPublisher/DeferEventPublisherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
declare(strict_types=1);
namespace Neos\EventSourcing\Tests\Unit\EventPublisher;

use Neos\EventSourcing\Event\DomainEventInterface;
use Neos\EventSourcing\Event\DomainEvents;
use Neos\EventSourcing\EventPublisher\DeferEventPublisher;
use Neos\EventSourcing\EventPublisher\EventPublisherInterface;
use Neos\Flow\Tests\UnitTestCase;

class DeferEventPublisherTest extends UnitTestCase
{

/**
* @test
*/
public function withoutPendingEventsTheWrappedEventPublisherIsNotCalled(): void
{
$mockPublisher = self::getMockBuilder(EventPublisherInterface::class)->getMock();

$publisher = DeferEventPublisher::forPublisher(
$mockPublisher
);

$mockPublisher
->expects(self::never())
->method('publish');

$publisher->publish(DomainEvents::createEmpty());
$publisher->invoke();
}

/**
* @test
*/
public function pendingEventsAreClearedAfterInvoke(): void
{
$mockPublisher = self::getMockBuilder(EventPublisherInterface::class)->getMock();
$eventA = self::getMockBuilder(DomainEventInterface::class)->getMock();
$eventB = self::getMockBuilder(DomainEventInterface::class)->getMock();

$publisher = DeferEventPublisher::forPublisher(
$mockPublisher
);

$mockPublisher
->expects(self::exactly(2))
->method('publish')
->withConsecutive(
[DomainEvents::withSingleEvent($eventA)],
[DomainEvents::withSingleEvent($eventB)]
);

$publisher->publish(DomainEvents::withSingleEvent($eventA));
$publisher->invoke();

$publisher->publish(DomainEvents::withSingleEvent($eventB));
$publisher->invoke();
}

}

0 comments on commit d2eef41

Please sign in to comment.