Skip to content

Commit

Permalink
Merge pull request #464 from patchlevel/psr-event-dispatcher
Browse files Browse the repository at this point in the history
add psr 14 support
  • Loading branch information
DavidBadura authored Jan 11, 2024
2 parents 65b5875 + 179722f commit 7b52991
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"patchlevel/worker": "^1.1.0",
"psr/cache": "^2.0.0|^3.0.0",
"psr/clock": "^1.0",
"psr/event-dispatcher": "^1.0",
"psr/log": "^2.0.0|^3.0.0",
"psr/simple-cache": "^2.0.0|^3.0.0",
"ramsey/uuid": "^4.7",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/EventBus/Psr14EventBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\EventBus;

use Psr\EventDispatcher\EventDispatcherInterface;

final class Psr14EventBus implements EventBus
{
public function __construct(
private readonly EventDispatcherInterface $eventDispatcher,
) {
}

public function dispatch(Message ...$messages): void
{
foreach ($messages as $message) {
$this->eventDispatcher->dispatch($message);
}
}
}
60 changes: 60 additions & 0 deletions tests/Unit/EventBus/Psr14EventBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Tests\Unit\EventBus;

use Patchlevel\EventSourcing\EventBus\Message;
use Patchlevel\EventSourcing\EventBus\Psr14EventBus;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileId;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Psr\EventDispatcher\EventDispatcherInterface;

/** @covers \Patchlevel\EventSourcing\EventBus\Psr14EventBus */
final class Psr14EventBusTest extends TestCase
{
use ProphecyTrait;

public function testDispatchEvent(): void
{
$message = new Message(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
);

$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch($message)->shouldBeCalled();

$eventBus = new Psr14EventBus($eventDispatcher->reveal());
$eventBus->dispatch($message);
}

public function testDispatchMultipleMessages(): void
{
$message1 = new Message(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
);

$message2 = new Message(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('[email protected]'),
),
);

$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch($message1)->shouldBeCalled();
$eventDispatcher->dispatch($message2)->shouldBeCalled();

$eventBus = new Psr14EventBus($eventDispatcher->reveal());
$eventBus->dispatch($message1, $message2);
}
}

0 comments on commit 7b52991

Please sign in to comment.