|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Tests\Domain\Book; |
|
6
|
|
|
|
|
7
|
|
|
use App\Domain\Book\Book; |
|
8
|
|
|
use App\Domain\Book\Event\BookDescriptionWasChanged; |
|
9
|
|
|
use App\Domain\Book\Event\BookNameWasChanged; |
|
10
|
|
|
use App\Domain\Book\Event\BookWasCreated; |
|
11
|
|
|
use App\Domain\Book\Event\BookWasDeleted; |
|
12
|
|
|
use App\Domain\Book\Exception\SameDescryptionException; |
|
13
|
|
|
use App\Domain\Book\ValueObject\Description; |
|
14
|
|
|
use App\Domain\Category\Exception\SameNameException; |
|
15
|
|
|
use App\Domain\Common\ValueObject\AggregateRootId; |
|
16
|
|
|
use App\Domain\Common\ValueObject\Name; |
|
17
|
|
|
use Tests\TestCase; |
|
18
|
|
|
|
|
19
|
|
|
class BookTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
public function test_book_it_create() |
|
22
|
|
|
{ |
|
23
|
|
|
$book = Book::create( |
|
24
|
|
|
AggregateRootId::generate(), |
|
25
|
|
|
Name::fromString('test'), |
|
26
|
|
|
Description::fromString('test'), |
|
27
|
|
|
'55ede857-82a9-4cff-9d23-07c35f63b206', |
|
28
|
|
|
'8e9c0764-4994-11e9-8646-d663bd873d93' |
|
29
|
|
|
); |
|
30
|
|
|
$this->assertInstanceOf(Book::class, $book); |
|
31
|
|
|
$events = $this->popRecordedEvent($book); |
|
32
|
|
|
$this->assertEquals(1, \count($events)); |
|
33
|
|
|
$this->assertInstanceOf(BookWasCreated::class, $events[0]); |
|
34
|
|
|
$expectedPayload = [ |
|
35
|
|
|
'name' => 'test', |
|
36
|
|
|
'description' => 'test', |
|
37
|
|
|
'category' => '55ede857-82a9-4cff-9d23-07c35f63b206', |
|
38
|
|
|
'author' => '8e9c0764-4994-11e9-8646-d663bd873d93', |
|
39
|
|
|
]; |
|
40
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function test_book_it_change_name() |
|
44
|
|
|
{ |
|
45
|
|
|
$book = Book::create( |
|
46
|
|
|
AggregateRootId::generate(), |
|
47
|
|
|
Name::fromString('test'), |
|
48
|
|
|
Description::fromString('test'), |
|
49
|
|
|
'55ede857-82a9-4cff-9d23-07c35f63b206', |
|
50
|
|
|
'8e9c0764-4994-11e9-8646-d663bd873d93' |
|
51
|
|
|
); |
|
52
|
|
|
$this->assertInstanceOf(Book::class, $book); |
|
53
|
|
|
$events = $this->popRecordedEvent($book); |
|
54
|
|
|
$this->assertEquals(1, \count($events)); |
|
55
|
|
|
$this->assertInstanceOf(BookWasCreated::class, $events[0]); |
|
56
|
|
|
$expectedPayload = [ |
|
57
|
|
|
'name' => 'test', |
|
58
|
|
|
'description' => 'test', |
|
59
|
|
|
'category' => '55ede857-82a9-4cff-9d23-07c35f63b206', |
|
60
|
|
|
'author' => '8e9c0764-4994-11e9-8646-d663bd873d93', |
|
61
|
|
|
]; |
|
62
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
63
|
|
|
$book->changeName('test2'); |
|
64
|
|
|
$events = $this->popRecordedEvent($book); |
|
65
|
|
|
$this->assertEquals(1, \count($events)); |
|
66
|
|
|
$this->assertInstanceOf(BookNameWasChanged::class, $events[0]); |
|
67
|
|
|
$expectedPayload = [ |
|
68
|
|
|
'name' => 'test2', |
|
69
|
|
|
]; |
|
70
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function test_book_it_change_same_name() |
|
74
|
|
|
{ |
|
75
|
|
|
$this->expectException(SameNameException::class); |
|
76
|
|
|
$book = Book::create( |
|
77
|
|
|
AggregateRootId::generate(), |
|
78
|
|
|
Name::fromString('test'), |
|
79
|
|
|
Description::fromString('test'), |
|
80
|
|
|
'55ede857-82a9-4cff-9d23-07c35f63b206', |
|
81
|
|
|
'8e9c0764-4994-11e9-8646-d663bd873d93' |
|
82
|
|
|
); |
|
83
|
|
|
$this->assertInstanceOf(Book::class, $book); |
|
84
|
|
|
$events = $this->popRecordedEvent($book); |
|
85
|
|
|
$this->assertEquals(1, \count($events)); |
|
86
|
|
|
$this->assertInstanceOf(BookWasCreated::class, $events[0]); |
|
87
|
|
|
$expectedPayload = [ |
|
88
|
|
|
'name' => 'test', |
|
89
|
|
|
'description' => 'test', |
|
90
|
|
|
'category' => '55ede857-82a9-4cff-9d23-07c35f63b206', |
|
91
|
|
|
'author' => '8e9c0764-4994-11e9-8646-d663bd873d93', |
|
92
|
|
|
]; |
|
93
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
94
|
|
|
$book->changeName('test'); |
|
95
|
|
|
$events = $this->popRecordedEvent($book); |
|
96
|
|
|
$this->assertEquals(1, \count($events)); |
|
97
|
|
|
$this->assertInstanceOf(BookNameWasChanged::class, $events[0]); |
|
98
|
|
|
$expectedPayload = [ |
|
99
|
|
|
'name' => 'test', |
|
100
|
|
|
]; |
|
101
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function test_book_it_change_same_description() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->expectException(SameDescryptionException::class); |
|
107
|
|
|
$book = Book::create( |
|
108
|
|
|
AggregateRootId::generate(), |
|
109
|
|
|
Name::fromString('test'), |
|
110
|
|
|
Description::fromString('test'), |
|
111
|
|
|
'55ede857-82a9-4cff-9d23-07c35f63b206', |
|
112
|
|
|
'8e9c0764-4994-11e9-8646-d663bd873d93' |
|
113
|
|
|
); |
|
114
|
|
|
$this->assertInstanceOf(Book::class, $book); |
|
115
|
|
|
$events = $this->popRecordedEvent($book); |
|
116
|
|
|
$this->assertEquals(1, \count($events)); |
|
117
|
|
|
$this->assertInstanceOf(BookWasCreated::class, $events[0]); |
|
118
|
|
|
$expectedPayload = [ |
|
119
|
|
|
'name' => 'test', |
|
120
|
|
|
'description' => 'test', |
|
121
|
|
|
'category' => '55ede857-82a9-4cff-9d23-07c35f63b206', |
|
122
|
|
|
'author' => '8e9c0764-4994-11e9-8646-d663bd873d93', |
|
123
|
|
|
]; |
|
124
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
125
|
|
|
$book->changeDescription('test'); |
|
126
|
|
|
$events = $this->popRecordedEvent($book); |
|
127
|
|
|
$this->assertEquals(1, \count($events)); |
|
128
|
|
|
$this->assertInstanceOf(BookDescriptionWasChanged::class, $events[0]); |
|
129
|
|
|
$expectedPayload = [ |
|
130
|
|
|
'description' => 'test', |
|
131
|
|
|
]; |
|
132
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function test_book_it_change_description() |
|
136
|
|
|
{ |
|
137
|
|
|
$book = Book::create( |
|
138
|
|
|
AggregateRootId::generate(), |
|
139
|
|
|
Name::fromString('test'), |
|
140
|
|
|
Description::fromString('test'), |
|
141
|
|
|
'55ede857-82a9-4cff-9d23-07c35f63b206', |
|
142
|
|
|
'8e9c0764-4994-11e9-8646-d663bd873d93' |
|
143
|
|
|
); |
|
144
|
|
|
$this->assertInstanceOf(Book::class, $book); |
|
145
|
|
|
$events = $this->popRecordedEvent($book); |
|
146
|
|
|
$this->assertEquals(1, \count($events)); |
|
147
|
|
|
$this->assertInstanceOf(BookWasCreated::class, $events[0]); |
|
148
|
|
|
$expectedPayload = [ |
|
149
|
|
|
'name' => 'test', |
|
150
|
|
|
'description' => 'test', |
|
151
|
|
|
'category' => '55ede857-82a9-4cff-9d23-07c35f63b206', |
|
152
|
|
|
'author' => '8e9c0764-4994-11e9-8646-d663bd873d93', |
|
153
|
|
|
]; |
|
154
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
155
|
|
|
$book->changeDescription('test2'); |
|
156
|
|
|
$events = $this->popRecordedEvent($book); |
|
157
|
|
|
$this->assertEquals(1, \count($events)); |
|
158
|
|
|
$this->assertInstanceOf(BookDescriptionWasChanged::class, $events[0]); |
|
159
|
|
|
$expectedPayload = [ |
|
160
|
|
|
'description' => 'test2', |
|
161
|
|
|
]; |
|
162
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function test_book_it_delete() |
|
166
|
|
|
{ |
|
167
|
|
|
$book = Book::create( |
|
168
|
|
|
AggregateRootId::generate(), |
|
169
|
|
|
Name::fromString('test'), |
|
170
|
|
|
Description::fromString('test'), |
|
171
|
|
|
'55ede857-82a9-4cff-9d23-07c35f63b206', |
|
172
|
|
|
'8e9c0764-4994-11e9-8646-d663bd873d93' |
|
173
|
|
|
); |
|
174
|
|
|
$this->assertInstanceOf(Book::class, $book); |
|
175
|
|
|
$events = $this->popRecordedEvent($book); |
|
176
|
|
|
$this->assertEquals(1, \count($events)); |
|
177
|
|
|
$this->assertInstanceOf(BookWasCreated::class, $events[0]); |
|
178
|
|
|
$expectedPayload = [ |
|
179
|
|
|
'name' => 'test', |
|
180
|
|
|
'description' => 'test', |
|
181
|
|
|
'category' => '55ede857-82a9-4cff-9d23-07c35f63b206', |
|
182
|
|
|
'author' => '8e9c0764-4994-11e9-8646-d663bd873d93', |
|
183
|
|
|
]; |
|
184
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
185
|
|
|
$book->delete(); |
|
186
|
|
|
$events = $this->popRecordedEvent($book); |
|
187
|
|
|
$this->assertEquals(1, \count($events)); |
|
188
|
|
|
$this->assertInstanceOf(BookWasDeleted::class, $events[0]); |
|
189
|
|
|
$expectedPayload = []; |
|
190
|
|
|
$this->assertEquals($expectedPayload, $events[0]->payload()); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|