1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cubiche/EventSourcing component. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) Cubiche |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Cubiche\Domain\EventSourcing\Tests\Units; |
13
|
|
|
|
14
|
|
|
use Cubiche\Domain\EventSourcing\DomainEventId; |
15
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Event\PostWasCreated; |
16
|
|
|
use Cubiche\Domain\Model\Tests\Fixtures\PostId; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DomainEventTests class. |
20
|
|
|
* |
21
|
|
|
* Generated by TestGenerator on 2016-06-28 at 14:36:54. |
22
|
|
|
*/ |
23
|
|
|
class DomainEventTests extends TestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Test EventId method. |
27
|
|
|
*/ |
28
|
|
View Code Duplication |
public function testEventId() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this |
31
|
|
|
->given($postId = PostId::fromNative(md5(rand()))) |
32
|
|
|
->when( |
33
|
|
|
$event = new PostWasCreated( |
34
|
|
|
$postId, |
35
|
|
|
$this->faker->sentence, |
36
|
|
|
$this->faker->paragraph |
37
|
|
|
) |
38
|
|
|
) |
39
|
|
|
->then() |
40
|
|
|
->object($event->eventId()) |
41
|
|
|
->isInstanceOf(DomainEventId::class) |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Test AggregateId method. |
47
|
|
|
*/ |
48
|
|
View Code Duplication |
public function testAggregateId() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$this |
51
|
|
|
->given($postId = PostId::fromNative(md5(rand()))) |
52
|
|
|
->when( |
53
|
|
|
$event = new PostWasCreated( |
54
|
|
|
$postId, |
55
|
|
|
$this->faker->sentence, |
56
|
|
|
$this->faker->paragraph |
57
|
|
|
) |
58
|
|
|
) |
59
|
|
|
->then() |
60
|
|
|
->object($event->aggregateId()) |
61
|
|
|
->isEqualTo($postId) |
62
|
|
|
; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Test Version method. |
67
|
|
|
*/ |
68
|
|
|
public function testVersion() |
69
|
|
|
{ |
70
|
|
|
$this |
71
|
|
|
->given($postId = PostId::fromNative(md5(rand()))) |
72
|
|
|
->when( |
73
|
|
|
$event = new PostWasCreated( |
74
|
|
|
$postId, |
75
|
|
|
$this->faker->sentence, |
76
|
|
|
$this->faker->paragraph |
77
|
|
|
) |
78
|
|
|
) |
79
|
|
|
->then() |
80
|
|
|
->integer($event->version()) |
81
|
|
|
->isEqualTo(0) |
82
|
|
|
->and() |
83
|
|
|
->when($event->setVersion(165)) |
84
|
|
|
->then() |
85
|
|
|
->integer($event->version()) |
86
|
|
|
->isEqualTo(165) |
87
|
|
|
; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Test ToArray method. |
92
|
|
|
*/ |
93
|
|
|
public function testToArray() |
94
|
|
|
{ |
95
|
|
|
$this |
96
|
|
|
->given( |
97
|
|
|
$postId = PostId::fromNative(md5(rand())), |
98
|
|
|
$title = $this->faker->sentence, |
99
|
|
|
$content = $this->faker->sentence |
100
|
|
|
) |
101
|
|
|
->and() |
102
|
|
|
->when($event = new PostWasCreated($postId, $title, $content)) |
103
|
|
|
->and($eventId = $event->eventId()) |
104
|
|
|
->then() |
105
|
|
|
->array($event->toArray()) |
106
|
|
|
->object['eventId'] |
107
|
|
|
->isEqualTo($eventId) |
108
|
|
|
->string['eventType'] |
109
|
|
|
->isEqualTo(PostWasCreated::class) |
110
|
|
|
->child['metadata'](function ($metadata) use ($postId) { |
111
|
|
|
$metadata |
112
|
|
|
->hasKey('occurredOn') |
113
|
|
|
->object['aggregateId'] |
114
|
|
|
->isEqualTo($postId) |
115
|
|
|
; |
116
|
|
|
}) |
117
|
|
|
->child['payload'](function ($payload) use ($title, $content) { |
118
|
|
|
$payload |
119
|
|
|
->isEqualTo(array( |
120
|
|
|
'title' => $title, |
121
|
|
|
'content' => $content, |
122
|
|
|
)) |
123
|
|
|
; |
124
|
|
|
}) |
125
|
|
|
; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Test FromArray method. |
130
|
|
|
*/ |
131
|
|
|
public function testFromArray() |
132
|
|
|
{ |
133
|
|
|
$this |
134
|
|
|
->given( |
135
|
|
|
$postId = PostId::fromNative(md5(rand())), |
136
|
|
|
$title = $this->faker->sentence, |
137
|
|
|
$content = $this->faker->sentence |
138
|
|
|
) |
139
|
|
|
->and() |
140
|
|
|
->when($event = new PostWasCreated($postId, $title, $content)) |
141
|
|
|
->and($eventId = $event->eventId()) |
142
|
|
|
->and($eventFromArray = PostWasCreated::fromArray($event->toArray())) |
143
|
|
|
->then() |
144
|
|
|
->object($eventFromArray) |
145
|
|
|
->isInstanceOf(PostWasCreated::class) |
146
|
|
|
->object($event->eventId()) |
147
|
|
|
->isEqualTo($eventFromArray->eventId()) |
148
|
|
|
->object($event->occurredOn()) |
149
|
|
|
->isEqualTo($eventFromArray->occurredOn()) |
150
|
|
|
->string($event->eventName()) |
151
|
|
|
->isEqualTo($eventFromArray->eventName()) |
152
|
|
|
->string($event->title()) |
153
|
|
|
->isEqualTo($eventFromArray->title()) |
154
|
|
|
->string($event->content()) |
155
|
|
|
->isEqualTo($eventFromArray->content()) |
156
|
|
|
->boolean($event->isPropagationStopped()) |
157
|
|
|
->isEqualTo($eventFromArray->isPropagationStopped()) |
158
|
|
|
; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.