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\EventStore; |
13
|
|
|
|
14
|
|
|
use Cubiche\Domain\EventSourcing\EventStore\EventStoreInterface; |
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\Units\TestCase; |
19
|
|
|
use Cubiche\Domain\Model\Tests\Fixtures\PostId; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* EventStoreTestCase class. |
23
|
|
|
* |
24
|
|
|
* Generated by TestGenerator on 2016-06-28 at 14:36:54. |
25
|
|
|
*/ |
26
|
|
|
abstract class EventStoreTestCase extends TestCase |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @return EventStoreInterface |
30
|
|
|
*/ |
31
|
|
|
abstract protected function createStore(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Test Persist method. |
35
|
|
|
*/ |
36
|
|
|
public function testPersist() |
37
|
|
|
{ |
38
|
|
|
$this |
39
|
|
|
->given($store = $this->createStore()) |
40
|
|
|
->and($postId = PostId::fromNative(md5(rand()))) |
41
|
|
|
->and($streamName = 'Posts-'.$postId) |
42
|
|
|
->and( |
43
|
|
|
$postWasCreated = new PostWasCreated($postId, 'foo', 'bar'), |
44
|
|
|
$postWasCreated->setVersion(1) |
45
|
|
|
) |
46
|
|
|
->and($eventStream = new EventStream($streamName, $postId, [$postWasCreated])) |
47
|
|
|
->when($store->persist($eventStream)) |
48
|
|
|
->then() |
49
|
|
|
->object($store->load($streamName)->aggregateId()) |
50
|
|
|
->isEqualTo($eventStream->aggregateId()) |
51
|
|
|
; |
52
|
|
|
|
53
|
|
|
$this |
54
|
|
|
->given($store = $this->createStore()) |
55
|
|
|
->and($postId = PostId::fromNative(md5(rand()))) |
56
|
|
|
->and($streamName = 'Posts-'.$postId) |
57
|
|
|
->and( |
58
|
|
|
$postWasCreated = new PostWasCreated($postId, 'foo', 'bar'), |
59
|
|
|
$postWasCreated->setVersion(32) |
60
|
|
|
) |
61
|
|
|
->and($eventStream = new EventStream($streamName, $postId, [$postWasCreated])) |
62
|
|
|
->when($expextedVersion = $store->persist($eventStream)) |
63
|
|
|
->then() |
64
|
|
|
->integer($expextedVersion) |
65
|
|
|
->isEqualTo(32) |
66
|
|
|
; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Test Load method. |
71
|
|
|
*/ |
72
|
|
|
public function testLoad() |
73
|
|
|
{ |
74
|
|
|
$this |
75
|
|
|
->given($store = $this->createStore()) |
76
|
|
|
->and($postId = PostId::fromNative(md5(rand()))) |
77
|
|
|
->and($postId1 = PostId::fromNative(md5(rand()))) |
78
|
|
|
->and($streamName = 'Posts-'.$postId) |
79
|
|
|
->and($streamName1 = 'Posts-'.$postId1) |
80
|
|
|
->and( |
81
|
|
|
$postWasCreated = new PostWasCreated($postId1, 'foo', 'bar'), |
82
|
|
|
$postWasCreated->setVersion(1) |
83
|
|
|
) |
84
|
|
|
->and( |
85
|
|
|
$postTitleWasChanged = new PostTitleWasChanged($postId1, 'new title'), |
86
|
|
|
$postTitleWasChanged->setVersion(2) |
87
|
|
|
) |
88
|
|
|
->and($streamNameFake = 'Blogs-'.$postId) |
89
|
|
|
->and($eventStream = new EventStream($streamName, $postId, [])) |
90
|
|
|
->and($eventStream1 = new EventStream($streamName1, $postId1, [$postWasCreated, $postTitleWasChanged])) |
91
|
|
|
->when($store->persist($eventStream)) |
92
|
|
|
->then() |
93
|
|
|
->variable($store->load($streamName)) |
94
|
|
|
->isNull() |
95
|
|
|
->variable($store->load($streamNameFake)) |
96
|
|
|
->isNull() |
97
|
|
|
->and() |
98
|
|
|
->when($store->persist($eventStream1)) |
99
|
|
|
->and($result = $store->load($streamName1)) |
100
|
|
|
->then() |
101
|
|
|
->object($result->aggregateId()) |
102
|
|
|
->isEqualTo($eventStream1->aggregateId()) |
103
|
|
|
; |
104
|
|
|
|
105
|
|
|
$this |
106
|
|
|
->given($store = $this->createStore()) |
107
|
|
|
->and($postId = PostId::fromNative(md5(rand()))) |
108
|
|
|
->and($streamName = 'Posts-'.$postId) |
109
|
|
|
->and( |
110
|
|
|
$postWasCreated = new PostWasCreated($postId, 'foo', 'bar'), |
111
|
|
|
$postWasCreated->setVersion(1) |
112
|
|
|
) |
113
|
|
|
->and( |
114
|
|
|
$postTitleWasChanged = new PostTitleWasChanged($postId, 'new title'), |
115
|
|
|
$postTitleWasChanged->setVersion(2) |
116
|
|
|
) |
117
|
|
|
->and( |
118
|
|
|
$postTitleWasChangedAgain = new PostTitleWasChanged($postId, 'more title'), |
119
|
|
|
$postTitleWasChangedAgain->setVersion(3) |
120
|
|
|
) |
121
|
|
|
->and( |
122
|
|
|
$eventStream = new EventStream( |
123
|
|
|
$streamName, |
124
|
|
|
$postId, |
125
|
|
|
[$postWasCreated, $postTitleWasChanged, $postTitleWasChangedAgain] |
126
|
|
|
) |
127
|
|
|
) |
128
|
|
|
->and($store->persist($eventStream)) |
129
|
|
|
->when($stream = $store->load($streamName, 2)) |
130
|
|
|
->then() |
131
|
|
|
->array($stream->events()) |
132
|
|
|
->hasSize(2) |
133
|
|
|
; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Test Remove method. |
138
|
|
|
*/ |
139
|
|
|
public function testRemove() |
140
|
|
|
{ |
141
|
|
|
$this |
142
|
|
|
->given($store = $this->createStore()) |
143
|
|
|
->and($postId = PostId::fromNative(md5(rand()))) |
144
|
|
|
->and($streamName = 'Posts-'.$postId) |
145
|
|
|
->and($streamNameFake = 'Blogs-'.$postId) |
146
|
|
|
->and( |
147
|
|
|
$postWasCreated = new PostWasCreated($postId, 'foo', 'bar'), |
148
|
|
|
$postWasCreated->setVersion(1) |
149
|
|
|
) |
150
|
|
|
->and( |
151
|
|
|
$postTitleWasChanged = new PostTitleWasChanged($postId, 'new title'), |
152
|
|
|
$postTitleWasChanged->setVersion(2) |
153
|
|
|
) |
154
|
|
|
->and($eventStream = new EventStream($streamName, $postId, [$postWasCreated, $postTitleWasChanged])) |
155
|
|
|
->and($store->persist($eventStream)) |
156
|
|
|
->when($store->remove($streamNameFake)) |
157
|
|
|
->then() |
158
|
|
|
->object($store->load($streamName)->aggregateId()) |
159
|
|
|
->isEqualTo($eventStream->aggregateId()) |
160
|
|
|
->and() |
161
|
|
|
->when($store->remove($streamName)) |
162
|
|
|
->then() |
163
|
|
|
->variable($store->load($streamName)) |
164
|
|
|
->isNull() |
165
|
|
|
; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|