Skip to content

Commit d8e7ffd

Browse files
authored
Merge pull request #19 from fritz-gerneth/issue-18
Support passing preconfigured messages when testing Subscribers
2 parents e4c952a + 9b4c5fa commit d8e7ffd

4 files changed

Lines changed: 92 additions & 5 deletions

File tree

README.md

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ final class ProfileTest extends AggregateRootTestCase
167167

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

@@ -232,4 +232,56 @@ final class ProfileSubscriberTest extends TestCase
232232
}
233233
```
234234

235-
This Util class can be used for integration or unit tests.
235+
This Util class can be used for integration or unit tests.
236+
237+
You can also pass `Message` instances with additional headers to the `executeRun` method. This allows testing
238+
subscribers that rely on additional parameters like header information:
239+
240+
241+
```php
242+
use Patchlevel\EventSourcing\Attribute\Subscribe;
243+
use Patchlevel\EventSourcing\Attribute\Subscriber;
244+
use DateTimeImmutable;
245+
246+
#[Subscriber('profile_subscriber', RunMode::FromBeginning)]
247+
final class ProfileSubscriber
248+
{
249+
#[Subscribe(ProfileCreated::class)]
250+
public function run(ProfileCreated $event, DateTimeImmutable $recordedOn): void
251+
{
252+
}
253+
}
254+
```
255+
256+
Add any headers you want in the test:
257+
258+
```php
259+
use Patchlevel\EventSourcing\Attribute\Subscriber;
260+
use Patchlevel\EventSourcing\Message\Message;
261+
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
262+
use Patchlevel\EventSourcing\Subscription\RunMode;
263+
use Patchlevel\EventSourcing\PhpUnit\Test\SubscriberUtilities;
264+
use DateTimeImmutable;
265+
266+
final class ProfileSubscriberTest extends TestCase
267+
{
268+
use SubscriberUtilities;
269+
270+
public function testProfileCreated(): void
271+
{
272+
/* Setup and Teardown as before */
273+
274+
$util->executeRun(
275+
Message::createWithHeaders(
276+
new ProfileCreated(
277+
ProfileId::fromString('1'),
278+
Email::fromString('hq@patchlevel.de'),
279+
),
280+
[new RecordedOnHeader(new DateTimeImmutable('now'))],
281+
)
282+
);
283+
284+
/* Your assertions */
285+
}
286+
}
287+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
],
2323
"require": {
2424
"php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
25-
"patchlevel/event-sourcing": "^3.10.0",
25+
"patchlevel/event-sourcing": "^3.13.0",
2626
"phpunit/phpunit": "^10.5.45 || ^11.0.0 || ^12.0.0"
2727
},
2828
"require-dev": {

src/Test/SubscriberUtilities.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ public function executeRun(object ...$events): self
5555
$subscriberAccessors = $this->subscriberAccessorRepository->all();
5656

5757
foreach ($events as $event) {
58+
if (!$event instanceof Message) {
59+
$event = Message::create($event);
60+
}
61+
5862
foreach ($subscriberAccessors as $subscriberAccessor) {
59-
foreach ($subscriberAccessor->subscribeMethods($event::class) as $subscribeMethod) {
60-
$subscribeMethod(Message::create($event));
63+
foreach ($subscriberAccessor->subscribeMethods($event->event()::class) as $subscribeMethod) {
64+
$subscribeMethod($event);
6165
}
6266
}
6367
}

tests/Unit/Test/SubscriberUtilitiesTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44

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

7+
use DateTimeImmutable;
78
use Patchlevel\EventSourcing\Attribute\Projector;
89
use Patchlevel\EventSourcing\Attribute\Setup;
910
use Patchlevel\EventSourcing\Attribute\Subscribe;
1011
use Patchlevel\EventSourcing\Attribute\Teardown;
12+
use Patchlevel\EventSourcing\Message\Message;
1113
use Patchlevel\EventSourcing\PhpUnit\Test\SubscriberUtilities;
1214
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\Email;
1315
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileCreated;
1416
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileId;
17+
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
1518
use PHPUnit\Framework\Attributes\CoversClass;
1619
use PHPUnit\Framework\TestCase;
1720

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

48+
public function testCanPassMessagesWithHeaders(): void
49+
{
50+
$recordedOn = new DateTimeImmutable('now');
51+
$subscriber = new #[Projector('test')]
52+
class {
53+
public DateTimeImmutable|null $recordedOn = null;
54+
55+
#[Subscribe(ProfileCreated::class)]
56+
public function run(ProfileCreated $event, DateTimeImmutable $recordedOn): void
57+
{
58+
$this->recordedOn = $recordedOn;
59+
}
60+
};
61+
62+
$util = new SubscriberUtilities($subscriber);
63+
$util->executeRun(
64+
Message::createWithHeaders(
65+
new ProfileCreated(
66+
ProfileId::fromString('1'),
67+
Email::fromString('hq@patchlevel.de'),
68+
),
69+
[new RecordedOnHeader($recordedOn)],
70+
),
71+
);
72+
73+
self::assertEquals($recordedOn, $subscriber->recordedOn);
74+
}
75+
4576
public function testRunNotFound(): void
4677
{
4778
$subscriber = new #[Projector('test')]

0 commit comments

Comments
 (0)