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\EventPublisher\DomainEventPublisher; |
15
|
|
|
use Cubiche\Domain\EventSourcing\AggregateRepository; |
16
|
|
|
use Cubiche\Domain\EventSourcing\EventStore\InMemoryEventStore; |
17
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Listener\PostPersistSubscriber; |
18
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Listener\PostRemoveSubscriber; |
19
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Listener\PrePersistSubscriber; |
20
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\Listener\PreRemoveSubscriber; |
21
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\PostEventSourced; |
22
|
|
|
use Cubiche\Domain\EventSourcing\Tests\Fixtures\PostEventSourcedFactory; |
23
|
|
|
use Cubiche\Domain\Model\Tests\Fixtures\Post; |
24
|
|
|
use Cubiche\Domain\Model\Tests\Fixtures\PostId; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* AggregateRepositoryTests class. |
28
|
|
|
* |
29
|
|
|
* Generated by TestGenerator on 2016-06-28 at 14:36:54. |
30
|
|
|
*/ |
31
|
|
|
class AggregateRepositoryTests extends TestCase |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @return AggregateRepository |
35
|
|
|
*/ |
36
|
|
|
protected function createRepository() |
37
|
|
|
{ |
38
|
|
|
return new AggregateRepository(new InMemoryEventStore(), PostEventSourced::class); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Test Persist method. |
43
|
|
|
*/ |
44
|
|
|
public function testPersist() |
45
|
|
|
{ |
46
|
|
|
$this |
47
|
|
|
->given($repository = $this->createRepository()) |
48
|
|
|
->and( |
49
|
|
|
$post = PostEventSourcedFactory::create( |
50
|
|
|
$this->faker->sentence, |
51
|
|
|
$this->faker->paragraph |
52
|
|
|
) |
53
|
|
|
) |
54
|
|
|
->and($post->changeTitle($this->faker->sentence)) |
55
|
|
|
->when($repository->persist($post)) |
56
|
|
|
->then() |
57
|
|
|
->object($repository->get($post->id())) |
58
|
|
|
->isEqualTo($post) |
59
|
|
|
; |
60
|
|
|
|
61
|
|
|
$this |
62
|
|
|
->given($repository = $this->createRepository()) |
63
|
|
|
->and( |
64
|
|
|
$post = PostEventSourcedFactory::create( |
65
|
|
|
$this->faker->sentence, |
66
|
|
|
$this->faker->paragraph |
67
|
|
|
) |
68
|
|
|
) |
69
|
|
|
->and($post->clearEvents()) |
70
|
|
|
->when($repository->persist($post)) |
71
|
|
|
->then() |
72
|
|
|
->variable($repository->get($post->id())) |
73
|
|
|
->isNull() |
74
|
|
|
; |
75
|
|
|
|
76
|
|
|
$this |
77
|
|
|
->given($repository = $this->createRepository()) |
78
|
|
|
->and( |
79
|
|
|
$post = new Post( |
80
|
|
|
PostId::fromNative(md5(rand())), |
81
|
|
|
$this->faker->sentence, |
82
|
|
|
$this->faker->paragraph |
83
|
|
|
) |
84
|
|
|
) |
85
|
|
|
->then() |
86
|
|
|
->exception(function () use ($repository, $post) { |
87
|
|
|
$repository->persist($post); |
88
|
|
|
}) |
89
|
|
|
->isInstanceOf(\InvalidArgumentException::class) |
90
|
|
|
; |
91
|
|
|
|
92
|
|
|
$this |
93
|
|
|
->given($repository = $this->createRepository()) |
94
|
|
|
->and( |
95
|
|
|
$post = PostEventSourcedFactory::create( |
96
|
|
|
$this->faker->sentence, |
97
|
|
|
$this->faker->paragraph |
98
|
|
|
) |
99
|
|
|
) |
100
|
|
|
->and($post->changeTitle($this->faker->sentence)) |
101
|
|
|
->and($prePersistSubscriber = new PrePersistSubscriber(42)) |
102
|
|
|
->and($postPersistSubscriber = new PostPersistSubscriber()) |
103
|
|
|
->and(DomainEventPublisher::subscribe($prePersistSubscriber)) |
104
|
|
|
->and(DomainEventPublisher::subscribe($postPersistSubscriber)) |
105
|
|
|
->when($repository->persist($post)) |
106
|
|
|
->then() |
107
|
|
|
->integer($post->version()) |
108
|
|
|
->isEqualTo(84) |
109
|
|
|
; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Test PersistAll method. |
114
|
|
|
*/ |
115
|
|
|
public function testPersistAll() |
116
|
|
|
{ |
117
|
|
|
$this |
118
|
|
|
->given($repository = $this->createRepository()) |
119
|
|
|
->and( |
120
|
|
|
$post1 = PostEventSourcedFactory::create( |
121
|
|
|
$this->faker->sentence, |
122
|
|
|
$this->faker->paragraph |
123
|
|
|
) |
124
|
|
|
) |
125
|
|
|
->and( |
126
|
|
|
$post2 = PostEventSourcedFactory::create( |
127
|
|
|
$this->faker->sentence, |
128
|
|
|
$this->faker->paragraph |
129
|
|
|
) |
130
|
|
|
) |
131
|
|
|
->and($post1->changeTitle($this->faker->sentence)) |
132
|
|
|
->and($post2->changeTitle($this->faker->sentence)) |
133
|
|
|
->when($repository->persistAll(array($post1, $post2))) |
134
|
|
|
->then() |
135
|
|
|
->object($repository->get($post1->id())) |
136
|
|
|
->isEqualTo($post1) |
137
|
|
|
->and() |
138
|
|
|
->object($repository->get($post2->id())) |
139
|
|
|
->isEqualTo($post2) |
140
|
|
|
; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Test Remove method. |
145
|
|
|
*/ |
146
|
|
|
public function testRemove() |
147
|
|
|
{ |
148
|
|
|
$this |
149
|
|
|
->given($repository = $this->createRepository()) |
150
|
|
|
->and( |
151
|
|
|
$post = PostEventSourcedFactory::create( |
152
|
|
|
$this->faker->sentence, |
153
|
|
|
$this->faker->paragraph |
154
|
|
|
) |
155
|
|
|
) |
156
|
|
|
->and($post->changeTitle($this->faker->sentence)) |
157
|
|
|
->when($repository->persist($post)) |
158
|
|
|
->then() |
159
|
|
|
->object($repository->get($post->id())) |
160
|
|
|
->isEqualTo($post) |
161
|
|
|
->and() |
162
|
|
|
->when($repository->remove($post)) |
163
|
|
|
->then() |
164
|
|
|
->variable($repository->get($post->id())) |
165
|
|
|
->isNull() |
166
|
|
|
; |
167
|
|
|
|
168
|
|
|
$this |
169
|
|
|
->given($repository = $this->createRepository()) |
170
|
|
|
->and( |
171
|
|
|
$post = new Post( |
172
|
|
|
PostId::fromNative(md5(rand())), |
173
|
|
|
$this->faker->sentence, |
174
|
|
|
$this->faker->paragraph |
175
|
|
|
) |
176
|
|
|
) |
177
|
|
|
->then() |
178
|
|
|
->exception(function () use ($repository, $post) { |
179
|
|
|
$repository->remove($post); |
180
|
|
|
}) |
181
|
|
|
->isInstanceOf(\InvalidArgumentException::class) |
182
|
|
|
; |
183
|
|
|
|
184
|
|
|
$this |
185
|
|
|
->given($repository = $this->createRepository()) |
186
|
|
|
->and( |
187
|
|
|
$post = PostEventSourcedFactory::create( |
188
|
|
|
$this->faker->sentence, |
189
|
|
|
$this->faker->paragraph |
190
|
|
|
) |
191
|
|
|
) |
192
|
|
|
->and($repository->persist($post)) |
193
|
|
|
->and($preRemoveSubscriber = new PreRemoveSubscriber(42)) |
194
|
|
|
->and($postRemoveSubscriber = new PostRemoveSubscriber()) |
195
|
|
|
->and(DomainEventPublisher::subscribe($preRemoveSubscriber)) |
196
|
|
|
->and(DomainEventPublisher::subscribe($postRemoveSubscriber)) |
197
|
|
|
->when($repository->remove($post)) |
198
|
|
|
->then() |
199
|
|
|
->integer($post->version()) |
200
|
|
|
->isEqualTo(21) |
201
|
|
|
; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|