-
-
Notifications
You must be signed in to change notification settings - Fork 972
Expand file tree
/
Copy pathFeatureContext.php
More file actions
446 lines (371 loc) · 13.6 KB
/
Copy pathFeatureContext.php
File metadata and controls
446 lines (371 loc) · 13.6 KB
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeLabel;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeRelation;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCarColor;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\FileConfigDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedToDummyFriend;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelationEmbedder;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\User;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UuidIdentifierDummy;
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Behat\Hook\Scope\AfterStepScope;
use Behatch\HttpCall\Request;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
/**
* Defines application features from the specific context.
*/
final class FeatureContext implements Context, SnippetAcceptingContext
{
/**
* @var EntityManagerInterface
*/
private $manager;
private $doctrine;
private $schemaTool;
private $classes;
private $request;
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct(ManagerRegistry $doctrine, Request $request)
{
$this->doctrine = $doctrine;
$this->manager = $doctrine->getManager();
$this->schemaTool = new SchemaTool($this->manager);
$this->classes = $this->manager->getMetadataFactory()->getAllMetadata();
$this->request = $request;
}
/**
* Sets the default Accept HTTP header to null (workaround to artificially remove it).
*
* @AfterStep
*/
public function removeAcceptHeaderAfterRequest(AfterStepScope $event)
{
if (preg_match('/^I send a "[A-Z]+" request to ".+"/', $event->getStep()->getText())) {
$this->request->setHttpHeader('Accept', null);
}
}
/**
* Sets the default Accept HTTP header to null (workaround to artificially remove it).
*
* @BeforeScenario
*/
public function removeAcceptHeaderBeforeScenario()
{
$this->request->setHttpHeader('Accept', null);
}
/**
* @BeforeScenario @createSchema
*/
public function createDatabase()
{
$this->schemaTool->createSchema($this->classes);
}
/**
* @AfterScenario @dropSchema
*/
public function dropDatabase()
{
$this->schemaTool->dropSchema($this->classes);
$this->doctrine->getManager()->clear();
}
/**
* @Given there are :nb dummy objects
*/
public function thereIsDummyObjects(int $nb)
{
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
for ($i = 1; $i <= $nb; ++$i) {
$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->setDummy('SomeDummyTest'.$i);
$dummy->setDescription($descriptions[($i - 1) % 2]);
$this->manager->persist($dummy);
}
$this->manager->flush();
}
/**
* @Given there are :nb dummy objects with relatedDummy
*/
public function thereIsDummyObjectsWithRelatedDummy(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$relatedDummy = new RelatedDummy();
$relatedDummy->setName('RelatedDummy #'.$i);
$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->setRelatedDummy($relatedDummy);
$this->manager->persist($relatedDummy);
$this->manager->persist($dummy);
}
$this->manager->flush();
}
/**
* @Given there are :nb dummy objects having each :nbrelated relatedDummies
*/
public function thereIsDummyObjectsWithRelatedDummies(int $nb, int $nbrelated)
{
for ($i = 1; $i <= $nb; ++$i) {
$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
for ($j = 1; $j <= $nbrelated; ++$j) {
$relatedDummy = new RelatedDummy();
$relatedDummy->setName('RelatedDummy'.$j.$i);
$this->manager->persist($relatedDummy);
$dummy->addRelatedDummy($relatedDummy);
}
$this->manager->persist($dummy);
}
$this->manager->flush();
}
/**
* @Given there are :nb dummy objects with dummyDate
*/
public function thereIsDummyObjectsWithDummyDate(int $nb)
{
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
for ($i = 1; $i <= $nb; ++$i) {
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->setDescription($descriptions[($i - 1) % 2]);
// Last Dummy has a null date
if ($nb !== $i) {
$dummy->setDummyDate($date);
}
$this->manager->persist($dummy);
}
$this->manager->flush();
}
/**
* @Given there are :nb dummy objects with dummyDate and dummyBoolean :bool
*/
public function thereIsDummyObjectsWithDummyDateAndDummyBoolean(int $nb, string $bool)
{
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
if (in_array($bool, ['true', '1', 1], true)) {
$bool = true;
} elseif (in_array($bool, ['false', '0', 0], true)) {
$bool = false;
} else {
$expected = ['true', 'false', '1', '0'];
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
}
for ($i = 1; $i <= $nb; ++$i) {
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->setDescription($descriptions[($i - 1) % 2]);
$dummy->setDummyBoolean($bool);
// Last Dummy has a null date
if ($nb !== $i) {
$dummy->setDummyDate($date);
}
$this->manager->persist($dummy);
}
$this->manager->flush();
}
/**
* @Given there are :nb dummy objects with dummyDate and relatedDummy
*/
public function thereIsDummyObjectsWithDummyDateAndRelatedDummy(int $nb)
{
for ($i = 1; $i <= $nb; ++$i) {
$date = new \DateTime(sprintf('2015-04-%d', $i), new \DateTimeZone('UTC'));
$relatedDummy = new RelatedDummy();
$relatedDummy->setName('RelatedDummy #'.$i);
$relatedDummy->setDummyDate($date);
$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->setRelatedDummy($relatedDummy);
// Last Dummy has a null date
if ($nb !== $i) {
$dummy->setDummyDate($date);
}
$this->manager->persist($relatedDummy);
$this->manager->persist($dummy);
}
$this->manager->flush();
}
/**
* @Given there are :nb dummy objects with dummyPrice
*/
public function thereIsDummyObjectsWithDummyPrice(int $nb)
{
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
$prices = ['9.99', '12.99', '15.99', '19.99'];
for ($i = 1; $i <= $nb; ++$i) {
$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->setDescription($descriptions[($i - 1) % 2]);
$dummy->setDummyPrice($prices[($i - 1) % 4]);
$this->manager->persist($dummy);
}
$this->manager->flush();
}
/**
* @Given there are :nb dummy objects with dummyBoolean :bool
*/
public function thereIsDummyObjectsWithDummyBoolean(int $nb, string $bool)
{
if (in_array($bool, ['true', '1', 1], true)) {
$bool = true;
} elseif (in_array($bool, ['false', '0', 0], true)) {
$bool = false;
} else {
$expected = ['true', 'false', '1', '0'];
throw new InvalidArgumentException(sprintf('Invalid boolean value for "%s" property, expected one of ( "%s" )', $bool, implode('" | "', $expected)));
}
$descriptions = ['Smart dummy.', 'Not so smart dummy.'];
for ($i = 1; $i <= $nb; ++$i) {
$dummy = new Dummy();
$dummy->setName('Dummy #'.$i);
$dummy->setAlias('Alias #'.($nb - $i));
$dummy->setDescription($descriptions[($i - 1) % 2]);
$dummy->setDummyBoolean($bool);
$this->manager->persist($dummy);
}
$this->manager->flush();
}
/**
* @Given there is a RelationEmbedder object
*/
public function thereIsARelationEmbedderObject()
{
$relationEmbedder = new RelationEmbedder();
$this->manager->persist($relationEmbedder);
$this->manager->flush();
}
/**
* @Given there is a Dummy Object mapped by UUID
*/
public function thereIsADummyObjectMappedByUUID()
{
$dummy = new UuidIdentifierDummy();
$dummy->setName('My Dummy');
$dummy->setUuid('41B29566-144B-11E6-A148-3E1D05DEFE78');
$this->manager->persist($dummy);
$this->manager->flush();
}
/**
* @Given there are Composite identifier objects
*/
public function thereIsACompositeIdentifierObject()
{
$item = new CompositeItem();
$item->setField1('foobar');
$this->manager->persist($item);
$this->manager->flush();
for ($i = 0; $i < 4; ++$i) {
$label = new CompositeLabel();
$label->setValue('foo-'.$i);
$rel = new CompositeRelation();
$rel->setCompositeLabel($label);
$rel->setCompositeItem($item);
$rel->setValue('somefoobardummy');
$this->manager->persist($label);
// since doctrine 2.6 we need existing identifiers on relations
$this->manager->flush();
$this->manager->persist($rel);
}
$this->manager->flush();
$this->manager->clear();
}
/**
* @Given there is a FileConfigDummy object
*/
public function thereIsAFileConfigDummyObject()
{
$fileConfigDummy = new FileConfigDummy();
$fileConfigDummy->setName('ConfigDummy');
$fileConfigDummy->setFoo('Foo');
$this->manager->persist($fileConfigDummy);
$this->manager->flush();
}
/**
* @Given there is a DummyCar entity with related colors
*/
public function thereIsAFooEntityWithRelatedBars()
{
$foo = new DummyCar();
$this->manager->persist($foo);
$bar1 = new DummyCarColor();
$bar1->setProp('red');
$bar1->setCar($foo);
$this->manager->persist($bar1);
$bar2 = new DummyCarColor();
$bar2->setProp('blue');
$bar2->setCar($foo);
$this->manager->persist($bar2);
$foo->setColors([$bar1, $bar2]);
$this->manager->persist($foo);
$this->manager->flush();
}
/**
* @Given there is a RelatedDummy with :nb friends
*/
public function thereIsARelatedDummyWithFriends(int $nb)
{
$relatedDummy = new RelatedDummy();
$relatedDummy->setName('RelatedDummy with friends');
$this->manager->persist($relatedDummy);
$this->manager->flush();
for ($i = 1; $i <= $nb; ++$i) {
$friend = new DummyFriend();
$friend->setName('Friend-'.$i);
$this->manager->persist($friend);
// since doctrine 2.6 we need existing identifiers on relations
// See https://github.com/doctrine/doctrine2/pull/6701
$this->manager->flush();
$relation = new RelatedToDummyFriend();
$relation->setName('Relation-'.$i);
$relation->setDummyFriend($friend);
$relation->setRelatedDummy($relatedDummy);
$relatedDummy->addRelatedToDummyFriend($relation);
$this->manager->persist($relation);
}
$relatedDummy2 = new RelatedDummy();
$relatedDummy2->setName('RelatedDummy without friends');
$this->manager->persist($relatedDummy2);
$this->manager->flush();
}
/**
* @Then the password :password for user :user should be hashed
*/
public function thePasswordForUserShouldBeHashed(string $password, string $user)
{
$user = $this->doctrine->getRepository(User::class)->find($user);
if (!password_verify($password, $user->getPassword())) {
throw new \Exception('User password mismatch');
}
}
}