This repository was archived by the owner on Jun 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRaffleApiContext.php
277 lines (222 loc) · 7.02 KB
/
RaffleApiContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
declare(strict_types=1);
use App\Entity\JoindInUser;
use Behat\Behat\Context\Context;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\HttpKernel\KernelInterface;
use Webmozart\Assert\Assert;
class RaffleApiContext implements Context
{
use JoindInFixturesTrait;
use RaffleFixturesTrait;
use HelperTrait;
/** @var KernelInterface */
private $kernel;
/** @var string|null */
private $raffleId;
/** @var array|null */
private $picked;
/** @var string */
private $testApiUrl = 'http://test.raffler.loc:8000/api';
/** @var int */
private $responseCode;
public function __construct(KernelInterface $kernel)
{
$this->kernel = $kernel;
}
/**
* @When organizer picks to raffle meetups: :eventIdList
*/
public function organizerPicksToRaffleMeetups(string $eventIdList)
{
$options = [
'json' => ['events' => explode(',', $eventIdList)],
];
$data = $this->apiPostJson('/raffle/start', $options);
$this->raffleId = $data;
}
/**
* @Then we get an exception for a raffle with no meetups
*/
public function weGetAnExceptionForARaffleWithNoMeetups()
{
$options = [
'json' => ['events' => []],
];
$data = $this->apiPostJson('/raffle/start', $options);
Assert::contains($data, 'There were no events selected to raffle (RaffleID:');
}
/**
* @When I pick a winner
* @When I pick another winner
*/
public function wePick()
{
$this->picked = $this->apiPostJson('/raffle/'.$this->raffleId.'/pick');
}
/**
* @When I confirm him or her as a winner
*/
public function iConfirmHimOrHerAsAWinner()
{
$url = '/raffle/'.$this->raffleId.'/winner/'.$this->picked['id'];
Assert::eq('OK', $this->apiPostJson($url));
}
/**
* @When I mark him or her as a no show
*/
public function iMarkHimOrHerAsANoShow()
{
$url = '/raffle/'.$this->raffleId.'/no_show/'.$this->picked['id'];
Assert::eq('OK', $this->apiPostJson($url));
}
/**
* @When user :user wins
*/
public function userWins(JoindInUser $user)
{
$url = '/raffle/'.$this->raffleId.'/winner/'.$user->getId();
Assert::eq('OK', $this->apiPostJson($url));
}
/**
* @When user :user is no show
*/
public function userIsNoShow(JoindInUser $user)
{
$url = '/raffle/'.$this->raffleId.'/no_show/'.$user->getId();
Assert::eq('OK', $this->apiPostJson($url));
}
/**
* @Then there should be :count events on the raffle
*/
public function thereShouldBeEventsOnTheRaffle(int $count)
{
$data = $this->apiGetJson('/raffle/'.$this->raffleId);
Assert::count($data['events'], $count);
}
/**
* @Then there should be :count comments on the raffle
*/
public function thereShouldBeCommentsOnTheRaffle(int $count)
{
Assert::count($this->apiGetJson('/raffle/'.$this->raffleId.'/comments'), $count);
}
/**
* @Then we should get one of :userNames as a winner
*/
public function weShouldGetOneOfAsAWinner(string $userNames)
{
$users = $this->loadMultipleUsers($userNames);
foreach ($users as $user) {
if ($user->getId() === $this->picked['id']) {
return;
}
}
throw new Exception('Picked user is not in the expected list');
}
/**
* @Then we should get back one of the members that left feedback
*/
public function weShouldGetBackOneOfTheMembersThatLeftFeedback()
{
Assert::notNull($this->picked);
}
/**
* @Then :user user should be :count times in the list
*/
public function userShouldBeTimesInTheList(JoindInUser $user, int $count)
{
$data = $this->apiGetJson('/raffle/'.$this->raffleId.'/comments');
$found = 0;
foreach ($data as $comment) {
if ($comment['user']['id'] === $user->getId()) {
++$found;
}
}
Assert::eq($found, $count);
}
/**
* @Then we should get :user as a winner
*/
public function weShouldGetAsAWinner(JoindInUser $user)
{
Assert::eq($user->getId(), $this->picked['id']);
}
/**
* @Then we should have :count eligible comment for next prize
* @Then we should have :count eligible comments for next prize
*/
public function weShouldHaveEligibleCommentForNextPrize(int $count)
{
$data = $this->apiGetJson('/raffle/'.$this->raffleId.'/comments');
Assert::count($data, $count);
}
/**
* @Then winners comments should not be eligible for further raffling
*/
public function winnersCommentsShouldNotBeEligibleForFurtherRaffling()
{
$eligibleComments = $this->apiGetJson('/raffle/'.$this->raffleId.'/comments');
$noneligibleComments = $this->apiGetJson('/raffle/'.$this->raffleId.'/noneligible_comments');
$eligibleCommentsIds = [];
foreach ($eligibleComments as $comment) {
$eligibleCommentsIds[] = $comment['id'];
}
foreach ($noneligibleComments as $noneligibleComment) {
if (array_key_exists($noneligibleComment['id'], $eligibleCommentsIds)) {
throw new Exception('comment both eligible and non eligible');
}
}
}
/**
* @Then we cannot continue raffling
*/
public function weCannotContinueRaffling()
{
$response = $this->apiPostJson('/raffle/'.$this->raffleId.'/pick');
Assert::true($response['error']);
}
/**
* @Then we get an exception for a raffle with no comments
*/
public function weGetAnExceptionForARaffleWithNoComments()
{
Assert::eq($this->raffleId, '');
}
private function apiGetJson(string $url)
{
$response = $this->getGuzzle()->get($this->testApiUrl.$url);
return json_decode($response->getBody()->getContents(), true);
}
private function apiPostJson(string $url, array $options = [])
{
try {
$response = $this->getGuzzle()->post($this->testApiUrl.$url, $options);
$this->responseCode = $response->getStatusCode();
return json_decode($response->getBody()->getContents(), true);
} catch (ClientException $ex) {
$this->responseCode = $ex->getCode();
return;
}
}
private function getGuzzle(): Client
{
return $this->getService(Client::class);
}
protected function getService(string $name)
{
return $this->kernel->getContainer()->get($name);
}
/**
* @Then /^we get an http response with status code (\d+)$/
*/
public function weGetAnHttpResponseWithCode(int $statusCode)
{
$options = [
'json' => ['events' => []],
];
$this->apiPostJson('/raffle/start', $options);
Assert::eq($this->responseCode, $statusCode);
}
}