|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
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\Core\Validator\Exception\ValidationException; |
|
15
|
|
|
use Cubiche\Domain\EventSourcing\EventStore\EventStream; |
|
16
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Event\PostTitleWasChanged; |
|
17
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Event\PostWasCreated; |
|
18
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\PostEventSourced; |
|
19
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\PostEventSourcedFactory; |
|
20
|
|
|
use Cubiche\Domain\EventSourcing\Versioning\Version; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* EventSourcedAggregateRootTests class. |
|
24
|
|
|
* |
|
25
|
|
|
* Generated by TestGenerator on 2016-06-28 at 14:36:54. |
|
26
|
|
|
*/ |
|
27
|
|
|
class EventSourcedAggregateRootTests extends TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Test recordApplyAndPublishEvent method. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testRecordApplyAndPublishEvent() |
|
33
|
|
|
{ |
|
34
|
|
|
$this |
|
35
|
|
|
->given( |
|
36
|
|
|
$post = PostEventSourcedFactory::create( |
|
37
|
|
|
$this->faker->sentence, |
|
38
|
|
|
$this->faker->paragraph |
|
39
|
|
|
) |
|
40
|
|
|
) |
|
41
|
|
|
->then() |
|
42
|
|
|
->array($post->recordedEvents()) |
|
43
|
|
|
->hasSize(1) |
|
44
|
|
|
; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Test applyEvent method. |
|
49
|
|
|
*/ |
|
50
|
|
|
public function testApplyEventWithoutApplyMethod() |
|
51
|
|
|
{ |
|
52
|
|
|
$this |
|
53
|
|
|
->given( |
|
54
|
|
|
$post = PostEventSourcedFactory::create( |
|
55
|
|
|
$this->faker->sentence, |
|
56
|
|
|
$this->faker->paragraph |
|
57
|
|
|
) |
|
58
|
|
|
) |
|
59
|
|
|
->then() |
|
60
|
|
|
->exception(function () use ($post) { |
|
61
|
|
|
$post->remove(); |
|
62
|
|
|
})->isInstanceOf(\BadMethodCallException::class) |
|
63
|
|
|
; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Test ClearEvents method. |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testClearEvents() |
|
70
|
|
|
{ |
|
71
|
|
|
$this |
|
72
|
|
|
->given( |
|
73
|
|
|
$post = PostEventSourcedFactory::create( |
|
74
|
|
|
$this->faker->sentence, |
|
75
|
|
|
$this->faker->paragraph |
|
76
|
|
|
) |
|
77
|
|
|
) |
|
78
|
|
|
->then() |
|
79
|
|
|
->array($post->recordedEvents()) |
|
80
|
|
|
->hasSize(1) |
|
81
|
|
|
->and() |
|
82
|
|
|
->when($post->clearEvents()) |
|
83
|
|
|
->then() |
|
84
|
|
|
->array($post->recordedEvents()) |
|
85
|
|
|
->isEmpty() |
|
86
|
|
|
; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Test LoadFromHistory method. |
|
91
|
|
|
*/ |
|
92
|
|
|
public function testLoadFromHistory() |
|
93
|
|
|
{ |
|
94
|
|
|
$this |
|
95
|
|
|
->given($title = $this->faker->sentence()) |
|
96
|
|
|
->and($content = $this->faker->paragraph()) |
|
97
|
|
|
->and($newTitle = $this->faker->sentence()) |
|
98
|
|
|
->and( |
|
99
|
|
|
$post = PostEventSourcedFactory::create( |
|
100
|
|
|
$title, |
|
101
|
|
|
$content |
|
102
|
|
|
) |
|
103
|
|
|
) |
|
104
|
|
|
->and($post->changeTitle($newTitle)) |
|
105
|
|
|
->and( |
|
106
|
|
|
$eventStream = new EventStream( |
|
107
|
|
|
PostEventSourced::class.'-'.$post->id(), |
|
108
|
|
|
$post->id(), |
|
109
|
|
|
[ |
|
110
|
|
|
new PostWasCreated($post->id(), $title, $content), |
|
|
|
|
|
|
111
|
|
|
new PostTitleWasChanged($post->id(), $newTitle), |
|
|
|
|
|
|
112
|
|
|
] |
|
113
|
|
|
) |
|
114
|
|
|
) |
|
115
|
|
|
->when($other = PostEventSourced::loadFromHistory($eventStream)) |
|
116
|
|
|
->then() |
|
117
|
|
|
->boolean($post->equals($other)) |
|
118
|
|
|
->isTrue() |
|
119
|
|
|
; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Test version. |
|
124
|
|
|
*/ |
|
125
|
|
|
public function testVersion() |
|
126
|
|
|
{ |
|
127
|
|
|
$this |
|
128
|
|
|
->given( |
|
129
|
|
|
$post = PostEventSourcedFactory::create( |
|
130
|
|
|
$this->faker->sentence, |
|
131
|
|
|
$this->faker->paragraph |
|
132
|
|
|
) |
|
133
|
|
|
) |
|
134
|
|
|
->when($post->changeTitle($this->faker->sentence)) |
|
135
|
|
|
->then() |
|
136
|
|
|
->integer($post->version()) |
|
137
|
|
|
->isEqualTo(2) |
|
138
|
|
|
->and() |
|
139
|
|
|
->when($post->setVersion(8)) |
|
140
|
|
|
->then() |
|
141
|
|
|
->integer($post->version()) |
|
142
|
|
|
->isEqualTo(8) |
|
143
|
|
|
; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Test validation. |
|
148
|
|
|
*/ |
|
149
|
|
|
public function testValidation() |
|
150
|
|
|
{ |
|
151
|
|
|
$this |
|
152
|
|
|
->given( |
|
153
|
|
|
$post = PostEventSourcedFactory::create( |
|
154
|
|
|
$this->faker->sentence, |
|
155
|
|
|
$this->faker->paragraph |
|
156
|
|
|
) |
|
157
|
|
|
) |
|
158
|
|
|
->when($post->changeTitle($this->faker->sentence())) |
|
159
|
|
|
->then() |
|
160
|
|
|
->array($post->recordedEvents()) |
|
161
|
|
|
->hasSize(2) |
|
162
|
|
|
->and() |
|
163
|
|
|
->exception(function () use ($post) { |
|
164
|
|
|
$post->changeTitle(''); |
|
165
|
|
|
})->isInstanceOf(ValidationException::class) |
|
166
|
|
|
->exception(function () use ($post) { |
|
167
|
|
|
$post->changeTitle(10); |
|
168
|
|
|
})->isInstanceOf(ValidationException::class) |
|
169
|
|
|
; |
|
170
|
|
|
|
|
171
|
|
|
$this |
|
172
|
|
|
->exception(function () { |
|
173
|
|
|
PostEventSourcedFactory::create('', $this->faker->paragraph); |
|
174
|
|
|
})->isInstanceOf(ValidationException::class) |
|
175
|
|
|
; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.