Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ final class ProfileTest extends AggregateRootTestCase

For testing a subscriber there is a utility class which you can use. Using `SubscriberUtilities` will provide you a
bunch of dx features which makes the testing easier. First, you will need to provide the utility class the subscriptions
you will want to test, this is done when initialiszing the class. After that, you can call these 3 methods:
you will want to test, this is done when initializing the class. After that, you can call these 3 methods:
`executeSetup`, `executeRun` and `executeTeardown`. These methods will be calling the right methods which are defined
via the attributes. For our example we are taking as simplified subscriber:

Expand Down Expand Up @@ -232,4 +232,56 @@ final class ProfileSubscriberTest extends TestCase
}
```

This Util class can be used for integration or unit tests.
This Util class can be used for integration or unit tests.

You can also pass `Message` instances with additional headers to the `executeRun` method. This allows testing
subscribers that rely on additional parameters like header information:


```php
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Subscriber;
use DateTimeImmutable;

#[Subscriber('profile_subscriber', RunMode::FromBeginning)]
final class ProfileSubscriber
{
#[Subscribe(ProfileCreated::class)]
public function run(ProfileCreated $event, DateTimeImmutable $recordedOn): void
{
}
}
```

Add any headers you want in the test:

```php
use Patchlevel\EventSourcing\Attribute\Subscriber;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
use Patchlevel\EventSourcing\Subscription\RunMode;
use Patchlevel\EventSourcing\PhpUnit\Test\SubscriberUtilities;
use DateTimeImmutable;

final class ProfileSubscriberTest extends TestCase
{
use SubscriberUtilities;

public function testProfileCreated(): void
{
/* Setup and Teardown as before */

$util->executeRun(
Message::createWithHeaders(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('hq@patchlevel.de'),
),
[new RecordedOnHeader(new DateTimeImmutable('now'))],
)
);

/* Your assertions */
}
}
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
"patchlevel/event-sourcing": "^3.10.0",
"patchlevel/event-sourcing": "^3.13.0",
"phpunit/phpunit": "^10.5.45 || ^11.0.0 || ^12.0.0"
},
"require-dev": {
Expand Down
8 changes: 6 additions & 2 deletions src/Test/SubscriberUtilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ public function executeRun(object ...$events): self
$subscriberAccessors = $this->subscriberAccessorRepository->all();

foreach ($events as $event) {
if (!$event instanceof Message) {
$event = Message::create($event);
}

foreach ($subscriberAccessors as $subscriberAccessor) {
foreach ($subscriberAccessor->subscribeMethods($event::class) as $subscribeMethod) {
$subscribeMethod(Message::create($event));
foreach ($subscriberAccessor->subscribeMethods($event->event()::class) as $subscribeMethod) {
$subscribeMethod($event);
}
}
}
Expand Down
31 changes: 31 additions & 0 deletions tests/Unit/Test/SubscriberUtilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@

namespace Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Test;

use DateTimeImmutable;
use Patchlevel\EventSourcing\Attribute\Projector;
use Patchlevel\EventSourcing\Attribute\Setup;
use Patchlevel\EventSourcing\Attribute\Subscribe;
use Patchlevel\EventSourcing\Attribute\Teardown;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\PhpUnit\Test\SubscriberUtilities;
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\Email;
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileCreated;
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileId;
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -42,6 +45,34 @@ public function run(): void
self::assertSame(1, $subscriber->called);
}

public function testCanPassMessagesWithHeaders(): void
{
$recordedOn = new DateTimeImmutable('now');
$subscriber = new #[Projector('test')]
class {
public DateTimeImmutable|null $recordedOn = null;

#[Subscribe(ProfileCreated::class)]
public function run(ProfileCreated $event, DateTimeImmutable $recordedOn): void
{
$this->recordedOn = $recordedOn;
}
};

$util = new SubscriberUtilities($subscriber);
$util->executeRun(
Message::createWithHeaders(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('hq@patchlevel.de'),
),
[new RecordedOnHeader($recordedOn)],
),
);

self::assertEquals($recordedOn, $subscriber->recordedOn);
}

public function testRunNotFound(): void
{
$subscriber = new #[Projector('test')]
Expand Down