|
13 | 13 |
|
14 | 14 | class ClientTest extends TestCase |
15 | 15 | { |
| 16 | + use ClientTestTrait; |
| 17 | + |
| 18 | + private Container $container; |
| 19 | + private Client $client; |
| 20 | + |
| 21 | + protected function setUp(): void |
| 22 | + { |
| 23 | + parent::setUp(); |
| 24 | + $this->container = $this->startContainer(); |
| 25 | + $this->client = $this->container->getClient(); |
| 26 | + } |
| 27 | + protected function tearDown(): void |
| 28 | + { |
| 29 | + $this->container->stop(); |
| 30 | + parent::tearDown(); |
| 31 | + } |
| 32 | + |
| 33 | + |
16 | 34 | public function testPingSucceedsWhenServerIsReachable(): void |
17 | 35 | { |
18 | | - $imageVersion = getImageVersionFromDockerfile(); |
19 | | - $container = new Container()->withImageTag($imageVersion); |
20 | | - $container->start(); |
21 | | - try { |
22 | | - $client = $container->getClient(); |
23 | | - $client->ping(); |
24 | | - $this->expectNotToPerformAssertions(); |
25 | | - } finally { |
26 | | - $container->stop(); |
27 | | - } |
| 36 | + $this->client->ping(); |
| 37 | + $this->expectNotToPerformAssertions(); |
28 | 38 | } |
29 | 39 |
|
30 | 40 | public function testPingFailsWhenServerIsUnreachable(): void |
31 | 41 | { |
32 | | - $imageVersion = getImageVersionFromDockerfile(); |
33 | | - $container = new Container()->withImageTag($imageVersion); |
34 | | - $container->start(); |
35 | | - try { |
36 | | - $port = $container->getMappedPort(); |
37 | | - $client = new Client("http://non-existent-host:{$port}", $container->getApiToken()); |
38 | | - |
39 | | - $this->expectException(\Throwable::class); |
40 | | - $client->ping(); |
41 | | - } finally { |
42 | | - $container->stop(); |
43 | | - } |
| 42 | + $port = $this->container->getMappedPort(); |
| 43 | + $client = new Client("http://non-existent-host:{$port}", $this->container->getApiToken()); |
| 44 | + |
| 45 | + $this->expectException(\Throwable::class); |
| 46 | + $client->ping(); |
44 | 47 | } |
45 | 48 |
|
| 49 | + |
| 50 | + |
46 | 51 | public function testVerifyApiTokenDoesNotThrowAnErrorIfTheTokenIsValid(): void |
47 | 52 | { |
48 | | - $imageVersion = getImageVersionFromDockerfile(); |
49 | | - $container = new Container()->withImageTag($imageVersion); |
50 | | - $container->start(); |
51 | | - try { |
52 | | - $client = $container->getClient(); |
53 | | - $client->verifyApiToken(); |
54 | | - $this->expectNotToPerformAssertions(); |
55 | | - } finally { |
56 | | - $container->stop(); |
57 | | - } |
| 53 | + $client = $this->container->getClient(); |
| 54 | + $client->verifyApiToken(); |
| 55 | + $this->expectNotToPerformAssertions(); |
58 | 56 | } |
59 | 57 |
|
60 | 58 | public function testVerifyApiTokenThrowsAnErrorIfTheTokenIsInvalid(): void |
61 | 59 | { |
62 | | - $imageVersion = getImageVersionFromDockerfile(); |
63 | | - $container = new Container()->withImageTag($imageVersion); |
64 | | - $container->start(); |
65 | | - try { |
66 | | - $baseUrl = $container->getBaseUrl(); |
67 | | - $apiToken = $container->getApiToken() . '-invalid'; |
68 | | - $client = new Client($baseUrl, $apiToken); |
69 | | - $this->expectException(\Throwable::class); |
70 | | - $client->verifyApiToken(); |
71 | | - } finally { |
72 | | - $container->stop(); |
73 | | - } |
| 60 | + $baseUrl = $this->container->getBaseUrl(); |
| 61 | + $apiToken = $this->container->getApiToken() . '-invalid'; |
| 62 | + $client = new Client($baseUrl, $apiToken); |
| 63 | + $this->expectException(\Throwable::class); |
| 64 | + $client->verifyApiToken(); |
74 | 65 | } |
75 | 66 |
|
76 | 67 | public function testWriteEventsWritesASingleEvent(): void |
77 | 68 | { |
78 | | - $imageVersion = getImageVersionFromDockerfile(); |
79 | | - $container = new Container()->withImageTag($imageVersion); |
80 | | - $container->start(); |
81 | | - try { |
82 | | - $client = $container->getClient(); |
83 | | - $event = new EventCandidate( |
84 | | - 'https://www.eventsourcingdb.io', |
85 | | - '/test', |
86 | | - 'io.eventsourcingdb.test', |
87 | | - [ |
88 | | - 'value' => 42, |
89 | | - ], |
90 | | - ); |
91 | | - |
92 | | - $writtenEvents = $client->writeEvents([ |
93 | | - $event, |
94 | | - ]); |
95 | | - |
96 | | - $this->assertCount(1, $writtenEvents); |
97 | | - $this->assertSame('0', $writtenEvents[0]->id); |
98 | | - } finally { |
99 | | - $container->stop(); |
100 | | - } |
| 69 | + $event = new EventCandidate( |
| 70 | + 'https://www.eventsourcingdb.io', |
| 71 | + '/test', |
| 72 | + 'io.eventsourcingdb.test', |
| 73 | + [ |
| 74 | + 'value' => 42, |
| 75 | + ], |
| 76 | + ); |
| 77 | + |
| 78 | + $writtenEvents = $this->client->writeEvents([ |
| 79 | + $event, |
| 80 | + ]); |
| 81 | + |
| 82 | + $this->assertCount(1, $writtenEvents); |
| 83 | + $this->assertSame('0', $writtenEvents[0]->id); |
101 | 84 | } |
102 | 85 |
|
103 | 86 | public function testWriteEventsWritesMultipleEvents(): void |
104 | 87 | { |
105 | | - $imageVersion = getImageVersionFromDockerfile(); |
106 | | - $container = new Container()->withImageTag($imageVersion); |
107 | | - $container->start(); |
108 | | - try { |
109 | | - $client = $container->getClient(); |
110 | | - |
111 | | - $firstEvent = new EventCandidate( |
112 | | - 'https://www.eventsourcingdb.io', |
113 | | - '/test', |
114 | | - 'io.eventsourcingdb.test', |
115 | | - [ |
116 | | - 'value' => 23, |
117 | | - ], |
118 | | - ); |
119 | | - |
120 | | - $secondEvent = new EventCandidate( |
121 | | - 'https://www.eventsourcingdb.io', |
122 | | - '/test', |
123 | | - 'io.eventsourcingdb.test', |
124 | | - [ |
125 | | - 'value' => 42, |
126 | | - ], |
127 | | - ); |
128 | | - |
129 | | - $writtenEvents = $client->writeEvents([ |
130 | | - $firstEvent, |
131 | | - $secondEvent, |
132 | | - ]); |
133 | | - |
134 | | - $this->assertCount(2, $writtenEvents); |
135 | | - $this->assertSame('0', $writtenEvents[0]->id); |
136 | | - $this->assertSame(23, $writtenEvents[0]->data['value']); |
137 | | - $this->assertSame('1', $writtenEvents[1]->id); |
138 | | - $this->assertSame(42, $writtenEvents[1]->data['value']); |
139 | | - } finally { |
140 | | - $container->stop(); |
141 | | - } |
| 88 | + $firstEvent = new EventCandidate( |
| 89 | + 'https://www.eventsourcingdb.io', |
| 90 | + '/test', |
| 91 | + 'io.eventsourcingdb.test', |
| 92 | + [ |
| 93 | + 'value' => 23, |
| 94 | + ], |
| 95 | + ); |
| 96 | + |
| 97 | + $secondEvent = new EventCandidate( |
| 98 | + 'https://www.eventsourcingdb.io', |
| 99 | + '/test', |
| 100 | + 'io.eventsourcingdb.test', |
| 101 | + [ |
| 102 | + 'value' => 42, |
| 103 | + ], |
| 104 | + ); |
| 105 | + |
| 106 | + $writtenEvents = $this->client->writeEvents([ |
| 107 | + $firstEvent, |
| 108 | + $secondEvent, |
| 109 | + ]); |
| 110 | + |
| 111 | + $this->assertCount(2, $writtenEvents); |
| 112 | + $this->assertSame('0', $writtenEvents[0]->id); |
| 113 | + $this->assertSame(23, $writtenEvents[0]->data['value']); |
| 114 | + $this->assertSame('1', $writtenEvents[1]->id); |
| 115 | + $this->assertSame(42, $writtenEvents[1]->data['value']); |
142 | 116 | } |
143 | 117 |
|
144 | 118 | public function testWriteEventsSupportsTheIsSubjectPristinePrecondition(): void |
145 | 119 | { |
146 | | - $imageVersion = getImageVersionFromDockerfile(); |
147 | | - $container = new Container()->withImageTag($imageVersion); |
148 | | - $container->start(); |
149 | | - try { |
150 | | - $client = $container->getClient(); |
151 | | - |
152 | | - $firstEvent = new EventCandidate( |
153 | | - 'https://www.eventsourcingdb.io', |
154 | | - '/test', |
155 | | - 'io.eventsourcingdb.test', |
156 | | - [ |
157 | | - 'value' => 23, |
158 | | - ], |
159 | | - ); |
160 | | - |
161 | | - $client->writeEvents([ |
162 | | - $firstEvent, |
163 | | - ]); |
164 | | - |
165 | | - $secondEvent = new EventCandidate( |
166 | | - 'https://www.eventsourcingdb.io', |
167 | | - '/test', |
168 | | - 'io.eventsourcingdb.test', |
169 | | - [ |
170 | | - 'value' => 42, |
171 | | - ], |
172 | | - ); |
173 | | - |
174 | | - $this->expectExceptionMessage("Failed to write events, got HTTP status code '409', expected '200'"); |
175 | | - $client->writeEvents([ |
176 | | - $secondEvent, |
177 | | - ], [ |
178 | | - new IsSubjectPristine('/test') |
179 | | - ]); |
180 | | - } finally { |
181 | | - $container->stop(); |
182 | | - } |
| 120 | + $firstEvent = new EventCandidate( |
| 121 | + 'https://www.eventsourcingdb.io', |
| 122 | + '/test', |
| 123 | + 'io.eventsourcingdb.test', |
| 124 | + [ |
| 125 | + 'value' => 23, |
| 126 | + ], |
| 127 | + ); |
| 128 | + |
| 129 | + $this->client->writeEvents([ |
| 130 | + $firstEvent, |
| 131 | + ]); |
| 132 | + |
| 133 | + $secondEvent = new EventCandidate( |
| 134 | + 'https://www.eventsourcingdb.io', |
| 135 | + '/test', |
| 136 | + 'io.eventsourcingdb.test', |
| 137 | + [ |
| 138 | + 'value' => 42, |
| 139 | + ], |
| 140 | + ); |
| 141 | + |
| 142 | + $this->expectExceptionMessage("Failed to write events, got HTTP status code '409', expected '200'"); |
| 143 | + |
| 144 | + $this->client->writeEvents([ |
| 145 | + $secondEvent, |
| 146 | + ], [ |
| 147 | + new IsSubjectPristine('/test') |
| 148 | + ]); |
183 | 149 | } |
184 | 150 |
|
185 | 151 | public function testWriteEventsSupportsTheIsSubjectOnEventIdPrecondition(): void |
186 | 152 | { |
187 | | - $imageVersion = getImageVersionFromDockerfile(); |
188 | | - $container = new Container()->withImageTag($imageVersion); |
189 | | - $container->start(); |
190 | | - try { |
191 | | - $client = $container->getClient(); |
192 | | - |
193 | | - $firstEvent = new EventCandidate( |
194 | | - 'https://www.eventsourcingdb.io', |
195 | | - '/test', |
196 | | - 'io.eventsourcingdb.test', |
197 | | - [ |
198 | | - 'value' => 23, |
199 | | - ], |
200 | | - ); |
201 | | - |
202 | | - $client->writeEvents([ |
203 | | - $firstEvent, |
204 | | - ]); |
205 | | - |
206 | | - $secondEvent = new EventCandidate( |
207 | | - 'https://www.eventsourcingdb.io', |
208 | | - '/test', |
209 | | - 'io.eventsourcingdb.test', |
210 | | - [ |
211 | | - 'value' => 42, |
212 | | - ], |
213 | | - ); |
214 | | - |
215 | | - $this->expectExceptionMessage("Failed to write events, got HTTP status code '409', expected '200'"); |
216 | | - $client->writeEvents([ |
217 | | - $secondEvent, |
218 | | - ], [ |
219 | | - new IsSubjectOnEventId('/test', '1') |
220 | | - ]); |
221 | | - } finally { |
222 | | - $container->stop(); |
223 | | - } |
| 153 | + $firstEvent = new EventCandidate( |
| 154 | + 'https://www.eventsourcingdb.io', |
| 155 | + '/test', |
| 156 | + 'io.eventsourcingdb.test', |
| 157 | + [ |
| 158 | + 'value' => 23, |
| 159 | + ], |
| 160 | + ); |
| 161 | + |
| 162 | + $this->client->writeEvents([ |
| 163 | + $firstEvent, |
| 164 | + ]); |
| 165 | + |
| 166 | + $secondEvent = new EventCandidate( |
| 167 | + 'https://www.eventsourcingdb.io', |
| 168 | + '/test', |
| 169 | + 'io.eventsourcingdb.test', |
| 170 | + [ |
| 171 | + 'value' => 42, |
| 172 | + ], |
| 173 | + ); |
| 174 | + |
| 175 | + $this->expectExceptionMessage("Failed to write events, got HTTP status code '409', expected '200'"); |
| 176 | + $this->client->writeEvents([ |
| 177 | + $secondEvent, |
| 178 | + ], [ |
| 179 | + new IsSubjectOnEventId('/test', '1') |
| 180 | + ]); |
224 | 181 | } |
225 | 182 | } |
0 commit comments