Completed
Push — master ( 70f8a5...37b9af )
by Frank
02:03
created

UpcastingEventsTest::upcasting_works()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 30
nc 1
nop 0
1
<?php
2
3
namespace EventSauce\EventSourcing\Integration\Upcasting;
4
5
use EventSauce\EventSourcing\AggregateRootId;
6
use EventSauce\EventSourcing\Message;
7
use EventSauce\EventSourcing\Serialization\ConstructingMessageSerializer;
8
use EventSauce\EventSourcing\Serialization\EventType;
9
use EventSauce\EventSourcing\Serialization\UpcastingMessageSerializer;
10
use EventSauce\EventSourcing\Upcasting\DelegatingUpcaster;
11
use function iterator_to_array;
12
use PHPUnit\Framework\TestCase;
13
use EventSauce\Time\TestClock;
14
15
class UpcastingEventsTest extends TestCase
16
{
17
    /**
18
     * @test
19
     */
20
    public function upcasting_works()
21
    {
22
        $clock = new TestClock();
23
        $pointInTime = $clock->pointInTime();
24
25
        $payload = [
26
            'type' => EventType::fromClassName(UpcastedEventStub::class)->toEventName(),
27
            'version' => 0,
28
            'aggregateRootId' => $uuid = AggregateRootId::create()->toString(),
29
            'timeOfRecording' => $pointInTime->toString(),
30
            'metadata' => [],
31
            'data' => [],
32
        ];
33
34
        $upcaster = new DelegatingUpcaster(new UpcasterStub());
35
        $serializer = new UpcastingMessageSerializer(new ConstructingMessageSerializer(), $upcaster);
36
37
        $message = iterator_to_array($serializer->unserializePayload($payload))[0];
38
        $expected = new Message(new UpcastedEventStub(
39
            new AggregateRootId($uuid),
40
            $pointInTime,
41
            'upcasted'
42
        ));
43
44
        $this->assertEquals($expected, $message);
45
46
        $serializeMessage = $serializer->serializeMessage($message);
47
        $expectedPayload = $payload = [
48
            'type' => EventType::fromClassName(UpcastedEventStub::class)->toEventName(),
49
            'version' => 1,
50
            'aggregateRootId' => $uuid,
51
            'timeOfRecording' => $pointInTime->toString(),
52
            'metadata' => [],
53
            'data' => [
54
                'property' => 'upcasted',
55
            ],
56
        ];
57
58
        $this->assertEquals($expectedPayload, $serializeMessage);
59
        $messageFromSerializedPayload = iterator_to_array($serializer->unserializePayload($expectedPayload))[0];
60
61
        $this->assertEquals($expected, $messageFromSerializedPayload);
62
    }
63
}