-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathEventSourcingCollectorTest.php
More file actions
86 lines (68 loc) · 3.03 KB
/
Copy pathEventSourcingCollectorTest.php
File metadata and controls
86 lines (68 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
declare(strict_types=1);
namespace Patchlevel\EventSourcingBundle\Tests\Unit\DataCollector;
use DateTimeImmutable;
use Patchlevel\EventSourcing\Identifier\CustomId;
use Patchlevel\EventSourcing\Message\Message;
use Patchlevel\EventSourcing\Metadata\AggregateRoot\AggregateRootRegistry;
use Patchlevel\EventSourcing\Metadata\Event\EventRegistry;
use Patchlevel\EventSourcing\Serializer\Encoder\Encoder;
use Patchlevel\EventSourcing\Serializer\EventSerializer;
use Patchlevel\EventSourcing\Serializer\SerializedEvent;
use Patchlevel\EventSourcing\Store\Header\PlayheadHeader;
use Patchlevel\EventSourcing\Store\Header\RecordedOnHeader;
use Patchlevel\EventSourcing\Store\Header\StreamNameHeader;
use Patchlevel\EventSourcingBundle\DataCollector\EventSourcingCollector;
use Patchlevel\EventSourcingBundle\DataCollector\MessageCollectorEventBus;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\Profile;
use Patchlevel\EventSourcingBundle\Tests\Fixtures\ProfileCreated;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\VarDumper\Cloner\Data;
final class EventSourcingCollectorTest extends TestCase
{
use ProphecyTrait;
public function testCollectData(): void
{
$eventBus = new MessageCollectorEventBus();
$eventRegistry = new EventRegistry([
'profile.created' => ProfileCreated::class,
]);
$aggregateRootRegistry = new AggregateRootRegistry([
'profile' => Profile::class,
]);
$event = new ProfileCreated(new CustomId('1'));
$message = Message::createWithHeaders($event, [
new StreamNameHeader('profile-1'),
new PlayheadHeader(1),
new RecordedOnHeader(new DateTimeImmutable('2022-07-07T18:55:50+02:00'))
]);
$eventSerializer = $this->prophesize(EventSerializer::class);
$eventSerializer->serialize($event, [
Encoder::OPTION_PRETTY_PRINT => true
])->willReturn(new SerializedEvent('profile.created', '{}'));
$collector = new EventSourcingCollector(
$eventBus,
$aggregateRootRegistry,
$eventRegistry,
);
$eventBus->dispatch($message);
$collector->collect(
new Request(),
new Response()
);
self::assertSame(['profile' => Profile::class], $collector->getAggregates());
self::assertSame(['profile.created' => ProfileCreated::class], $collector->getEvents());
$messages = $collector->getMessages();
self::assertCount(1, $messages);
$message = $messages[0];
self::assertEquals(ProfileCreated::class, $message['event_class']);
self::assertEquals('profile.created', $message['event_name']);
self::assertInstanceOf(Data::class, $message['event']);
self::assertIsArray($message['headers']);
self::assertCount(3, $message['headers']);
self::assertInstanceOf(Data::class, $message['headers'][0]);
}
}