Passed
Push — develop ( c404d0...7a63fa )
by Jens
08:21 queued 01:02
created

ApiContext::theBodyShouldBe()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/**
3
 * @author @jenschude <[email protected]>
4
 */
5
6
namespace Commercetools\Core;
7
8
use Behat\Behat\Tester\Exception\PendingException;
9
use Behat\Gherkin\Node\PyStringNode;
10
use Commercetools\Core\Model\Common\AbstractJsonDeserializeObject;
11
use Commercetools\Core\Model\Common\Collection;
12
use Commercetools\Core\Model\Common\JsonObject;
13
use Commercetools\Core\Model\Product\Search\Filter;
14
use Commercetools\Core\Request\Products\ProductProjectionSearchRequest;
15
16
trait ApiContext
17
{
18
    protected $request;
19
20
    protected $objects = [];
21
22
    protected $context;
23
24
    protected $actions = [];
25
26
    protected function getModuleName($context)
27
    {
28
        if (substr($context, -1) == 's' || strtolower($context) == 'inventory') {
29
            $module = $context;
30
        } elseif (substr($context, -1) == 'y') {
31
            $module = substr($context, 0, -1) . 'ies';
32
        } else {
33
            $module = $context . 's';
34
        }
35
36
        return ucfirst($module);
37
    }
38
39
    protected function getQueryContext($context)
40
    {
41
        if (substr($context, -3) == 'ies') {
42
            $context = substr($context, 0, -3) . 'y';
43
        } elseif (substr($context, -1) == 's') {
44
            $context = substr($context, 0, -1);
45
        }
46
        return ucfirst($context);
47
    }
48
49
    protected function getContext($context)
50
    {
51
        return ucfirst($context);
52
    }
53
54
    /**
55
     * @Given i have a :context draft with values
56
     */
57
    public function iHaveAContextDraftWithValuesJson($context, PyStringNode $json)
58
    {
59
        $context = $this->getContext($context);
60
        $class = '\Commercetools\Core\Model\\' . $context . '\\' . $context . 'Draft';
61
62
        $rawData = json_decode((string)$json, true);
63
        $object = call_user_func_array($class.'::fromArray', [$rawData]);
64
        $this->forceTyping($object, $rawData);
0 ignored issues
show
Unused Code introduced by
The call to Commercetools\Core\ApiContext::forceTyping() has too many arguments starting with $rawData. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        $this->/** @scrutinizer ignore-call */ 
65
               forceTyping($object, $rawData);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
65
66
        $this->objects[$context] = $object;
67
        $this->context = $context;
68
    }
69
70
    protected function forceTyping($object)
71
    {
72
        if ($object instanceof JsonObject) {
73
            $fields = $object->fieldDefinitions();
74
            foreach ($fields as $field => $definition) {
75
                $dummy = $object->get($field);
76
                if ($dummy instanceof AbstractJsonDeserializeObject) {
77
                    $this->forceTyping($dummy);
78
                }
79
            }
80
        } elseif ($object instanceof Collection) {
81
            foreach ($object as $index => $element) {
82
                $this->forceTyping($element);
83
            }
84
        }
85
    }
86
87
    /**
88
     * @Given add the :actionName action to :context with values
89
     */
90
    public function addTheActionToWithValues($actionName, $context, PyStringNode $json)
91
    {
92
        $module = $this->getModuleName($context);
93
        $context = $this->getContext($context);
94
        $actionName = $this->getContext($actionName);
95
        $class = '\Commercetools\Core\Request\\' . $module . '\\Command\\' . $context . $actionName . 'Action';
96
        $json = (string)$json;
97
        assertJson($json);
98
        $rawData = json_decode($json, true);
99
        $object = call_user_func_array($class.'::fromArray', [$rawData]);
100
        $this->forceTyping($object, $rawData);
0 ignored issues
show
Unused Code introduced by
The call to Commercetools\Core\ApiContext::forceTyping() has too many arguments starting with $rawData. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        $this->/** @scrutinizer ignore-call */ 
101
               forceTyping($object, $rawData);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
101
102
        $this->request->addAction($object);
103
    }
104
105
    /**
106
     * @Given i want to create a :context
107
     */
108
    public function iWantToCreateAContext($context)
109
    {
110
        $context = $this->getContext($context);
111
        $module = $this->getModuleName($context);
112
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'CreateRequest';
113
        if ($context == 'CustomObject') {
114
            $this->request = call_user_func_array($request. '::ofObject', [$this->objects[$context]]);
115
        } else {
116
            $this->request = call_user_func_array($request. '::ofDraft', [$this->objects[$context]]);
117
        }
118
    }
119
120
    /**
121
     * @Given a :context is identified by key :key and version :version
122
     */
123
    public function aContextIsIdentifiedByKeyAndVersionNr($context, $key, $version)
124
    {
125
        $context = $this->getContext($context);
126
        $requestContext = $context . 'Request';
127
        $this->objects[$requestContext] = ['key' => $key, 'version' => (int)$version, 'params' => []];
128
        $this->context = $requestContext;
129
    }
130
131
    /**
132
     * @Given a :context is identified by :id and version :version
133
     */
134
    public function aContextIsIdentifiedByIdAndVersionNr($context, $id, $version)
135
    {
136
        $context = $this->getContext($context);
137
        $requestContext = $context . 'Request';
138
        $this->objects[$requestContext] = ['id' => $id, 'version' => (int)$version, 'params' => []];
139
        $this->context = $requestContext;
140
    }
141
142
    /**
143
     * @Given a :context is identified by :container and key :key
144
     */
145
    public function aContextIsIdentifiedByContainerAndKey($context, $container, $key)
146
    {
147
        $context = $this->getContext($context);
148
        $requestContext = $context . 'Request';
149
        $this->objects[$requestContext] = ['container' => $container, 'key' => $key, 'params' => []];
150
        $this->context = $requestContext;
151
    }
152
153
154
    /**
155
     * @Given a :context is identified by :id
156
     */
157
    public function aContextIsIdentifiedById($context, $id)
158
    {
159
        $context = $this->getContext($context);
160
        $requestContext = $context . 'Request';
161
        $this->objects[$requestContext] = ['id' => $id];
162
    }
163
164
    /**
165
     * @Given i want to delete a :context
166
     */
167
    public function iWantToDeleteAContext($context)
168
    {
169
        $context = $this->getContext($context);
170
        $module = $this->getModuleName($context);
171
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'DeleteRequest';
172
        $requestContext = $context . 'Request';
173
        $id = $this->objects[$requestContext]['id'];
174
        $version = $this->objects[$requestContext]['version'];
175
        $this->request = call_user_func_array($request. '::ofIdAndVersion', [$id, $version]);
176
    }
177
178
    /**
179
     * @Given i want to fetch a :context
180
     */
181
    public function iWantToFetchAContext($context)
182
    {
183
        $context = $this->getContext($context);
184
        $module = $this->getModuleName($context);
185
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'ByIdGetRequest';
186
        $requestContext = $context . 'Request';
187
        $id = $this->objects[$requestContext]['id'];
188
        $this->request = call_user_func_array($request. '::ofId', [$id]);
189
    }
190
191
    /**
192
     * @Given i want to fetch a :context by key
193
     */
194
    public function iWantToFetchAContextByKey($context)
195
    {
196
        $context = $this->getContext($context);
197
        $module = $this->getModuleName($context);
198
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'ByKeyGetRequest';
199
        $requestContext = $context . 'Request';
200
        $key = $this->objects[$requestContext]['id'];
201
        $this->request = call_user_func_array($request. '::ofKey', [$key]);
202
    }
203
204
    /**
205
     * @Given i want to fetch a :context by container and key
206
     */
207
    public function iWantToFetchAContextByContainerAndKey($context)
208
    {
209
        $context = $this->getContext($context);
210
        $module = $this->getModuleName($context);
211
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'ByKeyGetRequest';
212
        $requestContext = $context . 'Request';
213
        $container = $this->objects[$requestContext]['container'];
214
        $key = $this->objects[$requestContext]['key'];
215
        $this->request = call_user_func_array($request. '::ofContainerAndKey', [$container, $key]);
216
    }
217
218
    /**
219
     * @Given i want to fetch a :context by customerId
220
     */
221
    public function iWantToFetchAContextByCustomerId($context)
222
    {
223
        $context = $this->getContext($context);
224
        $module = $this->getModuleName($context);
225
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'ByCustomerIdGetRequest';
226
        $requestContext = $context . 'Request';
227
        $id = $this->objects[$requestContext]['id'];
228
        $this->request = call_user_func_array($request. '::ofCustomerId', [$id]);
229
    }
230
231
    /**
232
     * @Given i want to fetch a :context by cartId
233
     */
234
    public function iWantToFetchAContextByCartId($context)
235
    {
236
        $context = $this->getContext($context);
237
        $module = $this->getModuleName($context);
238
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'ByCartIdGetRequest';
239
        $requestContext = $context . 'Request';
240
        $id = $this->objects[$requestContext]['id'];
241
        $this->request = call_user_func_array($request. '::ofCartId', [$id]);
242
    }
243
244
    /**
245
     * @Given i want to fetch a :context by location
246
     */
247
    public function iWantToFetchAContextByLocation($context)
248
    {
249
        $context = $this->getContext($context);
250
        $module = $this->getModuleName($context);
251
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'ByLocationGetRequest';
252
        $requestContext = $context . 'Request';
253
        $id = $this->objects[$requestContext]['id'];
254
        $this->request = call_user_func_array($request. '::ofCountry', [$id]);
255
    }
256
257
    /**
258
     * @Given with :field :value
259
     */
260
    public function withFieldValue($field, $value)
261
    {
262
        $method = 'with' . ucfirst($field);
263
        $this->request->$method($value);
264
    }
265
266
    /**
267
     * @Given i want to query :context
268
     */
269
    public function iWantToQuery($context)
270
    {
271
        $module = $this->getModuleName($context);
272
        $context = $this->getQueryContext($context);
273
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'QueryRequest';
274
        $this->request = call_user_func($request. '::of');
275
    }
276
277
    /**
278
     * @Given i want to search products
279
     */
280
    public function iWantToSearchProducts()
281
    {
282
        $request = ProductProjectionSearchRequest::class;
283
        $this->request = call_user_func($request. '::of');
284
    }
285
286
    /**
287
     * @Given i want to update a :context
288
     */
289
    public function iWantToUpdateAContext($context)
290
    {
291
        $context = $this->getContext($context);
292
        $module = $this->getModuleName($context);
293
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'UpdateRequest';
294
295
        $requestContext = $context . 'Request';
296
        if (isset($this->objects[$requestContext]['key'])) {
297
            $id = $this->objects[$requestContext]['key'];
298
            $method = 'ofKeyAndVersion';
299
        } else {
300
            $id = $this->objects[$requestContext]['id'];
301
            $method = 'ofIdAndVersion';
302
        }
303
        $version = $this->objects[$requestContext]['version'];
304
        $this->request = call_user_func_array($request . '::' . $method, [$id, $version]);
305
    }
306
307
    /**
308
     * @Given i want to update a :context by key
309
     */
310
    public function iWantToUpdateAContextByKey($context)
311
    {
312
        $context = $this->getContext($context);
313
        $module = $this->getModuleName($context);
314
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'UpdateByKeyRequest';
315
316
        $requestContext = $context . 'Request';
317
        $id = $this->objects[$requestContext]['id'];
318
        $version = $this->objects[$requestContext]['version'];
319
        $this->request = call_user_func_array($request. '::ofKeyAndVersion', [$id, $version]);
320
    }
321
322
323
    /**
324
     * @Given i want to delete a :context by key
325
     */
326
    public function iWantToDeleteAContextByKey($context)
327
    {
328
        $context = $this->getContext($context);
329
        $module = $this->getModuleName($context);
330
        $request = '\Commercetools\Core\Request\\' . ucfirst($module) . '\\' . ucfirst($context) . 'DeleteByKeyRequest';
331
        $requestContext = $context . 'Request';
332
        $id = $this->objects[$requestContext]['id'];
333
        $version = $this->objects[$requestContext]['version'];
334
        $this->request = call_user_func_array($request. '::ofKeyAndVersion', [$id, $version]);
335
    }
336
337
    /**
338
     * @Given i want to delete a :context by container and key
339
     */
340
    public function iWantToDeleteAContextByContainerAndKey($context)
341
    {
342
        $context = $this->getContext($context);
343
        $module = $this->getModuleName($context);
344
        $request = '\Commercetools\Core\Request\\' . ucfirst($module) . '\\' . ucfirst($context) . 'DeleteByKeyRequest';
345
        $requestContext = $context . 'Request';
346
        $container = $this->objects[$requestContext]['container'];
347
        $key = $this->objects[$requestContext]['key'];
348
        $this->request = call_user_func_array($request. '::ofContainerAndKey', [$container, $key]);
349
    }
350
351
    /**
352
     * @Given query by customers id :customerId
353
     */
354
    public function queryByCustomersId($customerId)
355
    {
356
        $this->request->byCustomerId($customerId);
357
    }
358
359
    /**
360
     * @Then the path should be :expectedPath
361
     */
362
    public function thePathShouldBe($expectedPath)
363
    {
364
        $httpRequest = $this->request->httpRequest();
365
366
        assertSame($expectedPath, (string)$httpRequest->getUri());
367
    }
368
369
    /**
370
     * @Then the request should be
371
     */
372
    public function theRequestShouldBe(PyStringNode $result)
373
    {
374
        $expectedResult = (string)$result;
375
        $httpRequest = $this->request->httpRequest();
376
        $request = (string)$httpRequest->getBody();
377
378
        assertJsonStringEqualsJsonString($expectedResult, $request);
379
    }
380
381
    /**
382
     * @Then the body should be
383
     */
384
    public function theBodyShouldBe(PyStringNode $result)
385
    {
386
        $expectedResult = (string)$result;
387
        $httpRequest = $this->request->httpRequest();
388
        $request = (string)$httpRequest->getBody();
389
390
        assertEquals($expectedResult, $request);
391
    }
392
393
    /**
394
     * @Then the method should be :method
395
     */
396
    public function theMethodShouldBe($expectedMethod)
397
    {
398
        $httpRequest = $this->request->httpRequest();
399
400
        assertSame(strtoupper($expectedMethod), $httpRequest->getMethod());
401
    }
402
403
    /**
404
     * @Given filter them with criteria :where
405
     */
406
    public function filterThemWithCriteriaName($where)
407
    {
408
        /**
409
         * @var \Commercetools\Core\Request\AbstractQueryRequest $request
410
         */
411
        $this->request->where($where);
412
    }
413
414
    /**
415
     * @Given filter :field with value :value
416
     */
417
    public function filterFieldWithValue($field, $value)
418
    {
419
        /**
420
         * @var \Commercetools\Core\Request\AbstractQueryRequest $request
421
         */
422
        $filter = Filter::ofName($field)->setValue($value);
423
        $this->request->addFilter($filter);
424
    }
425
426
    /**
427
     * @Given limit the result to :limit
428
     */
429
    public function limitTheResultTo($limit)
430
    {
431
        $this->request->limit($limit);
432
    }
433
434
    /**
435
     * @Given offset the result with :offset
436
     */
437
    public function offsetTheResultWith($offset)
438
    {
439
        $this->request->offset($offset);
440
    }
441
442
    /**
443
     * @Given sort them by :sort
444
     */
445
    public function sortThemBy($sort)
446
    {
447
        $this->request->sort($sort);
448
    }
449
450
    /**
451
     * @Given i want to create a :context token with :minutes minutes ttl
452
     */
453
    public function iWantToCreateATokenWithMinutesTtl($context, $minutes)
454
    {
455
        $context = $this->getContext($context);
456
        $module = $this->getModuleName($context);
457
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'EmailTokenRequest';
458
        $requestContext = $context . 'Request';
459
        $id = $this->objects[$requestContext]['id'];
460
        $version = $this->objects[$requestContext]['version'];
461
        $this->request = call_user_func_array(
462
            $request. '::ofIdVersionAndTtl',
463
            [$id, $version, $minutes]
464
        );
465
    }
466
467
    /**
468
     * @When i want to signin a :context with email :email, password :password and anonymousCartId :cartId
469
     */
470
    public function iWantToSignInAWithEmailPasswordAndAnonymousCartId($context, $email, $password, $cartId)
471
    {
472
        $context = $this->getContext($context);
473
        $module = $this->getModuleName($context);
474
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'LoginRequest';
475
        $this->request = call_user_func_array(
476
            $request. '::ofEmailAndPassword',
477
            [$email, $password, $cartId]
478
        );
479
    }
480
481
    /**
482
     * @Given a :context is identified by the email :email
483
     */
484
    public function aIsIdentifiedByTheEmail($context, $email)
485
    {
486
        $context = $this->getContext($context);
487
        $requestContext = $context . 'Request';
488
        $this->objects[$requestContext] = ['email' => $email];
489
    }
490
491
    /**
492
     * @Given i want to create a password token for :context
493
     */
494
    public function iWantToCreateAPasswordTokenFor($context)
495
    {
496
        $context = $this->getContext($context);
497
        $module = $this->getModuleName($context);
498
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'PasswordTokenRequest';
499
        $requestContext = $context . 'Request';
500
        $email = $this->objects[$requestContext]['email'];
501
        $this->request = call_user_func_array(
502
            $request. '::ofEmail',
503
            [$email]
504
        );
505
    }
506
507
    /**
508
     * @Given a :context is identified by the token :token
509
     */
510
    public function aContextIsIdentifiedByTheToken($context, $token)
511
    {
512
        $context = $this->getContext($context);
513
        $requestContext = $context . 'Request';
514
        $this->objects[$requestContext] = ['token' => $token];
515
    }
516
517
    /**
518
     * @Given i want to fetch a :context by token
519
     */
520
    public function iWantToFetchAContextByToken($context)
521
    {
522
        $context = $this->getContext($context);
523
        $module = $this->getModuleName($context);
524
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'ByTokenGetRequest';
525
        $requestContext = $context . 'Request';
526
        $token = $this->objects[$requestContext]['token'];
527
        $this->request = call_user_func_array($request. '::ofToken', [$token]);
528
    }
529
530
    /**
531
     * @Given i want to reset the :context password to :newPassword with token :token
532
     */
533
    public function iWantToResetTheContextPasswordToNewPasswordWithToken($context, $newPassword, $token)
534
    {
535
        $context = $this->getContext($context);
536
        $module = $this->getModuleName($context);
537
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'PasswordResetRequest';
538
        $this->request = call_user_func_array(
539
            $request. '::ofTokenAndPassword',
540
            [$token, $newPassword]
541
        );
542
    }
543
544
    /**
545
     * @Given i want to confirm the :context email with token :token
546
     */
547
    public function iWantToConfirmTheContextEmailWithToken($context, $token)
548
    {
549
        $context = $this->getContext($context);
550
        $module = $this->getModuleName($context);
551
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'EmailConfirmRequest';
552
        $this->request = call_user_func_array(
553
            $request. '::ofToken',
554
            [$token]
555
        );
556
    }
557
558
    /**
559
     * @Given i want to change the :context password from :currentPassword to :newPassword
560
     */
561
    public function iWantToChangeTheContextPasswordFromCurrentToNewPassword($context, $currentPassword, $newPassword)
562
    {
563
        $context = $this->getContext($context);
564
        $module = $this->getModuleName($context);
565
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'PasswordChangeRequest';
566
        $requestContext = $context . 'Request';
567
        $id = $this->objects[$requestContext]['id'];
568
        $version = $this->objects[$requestContext]['version'];
569
        $this->request = call_user_func_array(
570
            $request. '::ofIdVersionAndPasswords',
571
            [$id, $version, $currentPassword, $newPassword]
572
        );
573
    }
574
575
    /**
576
     * @Given i want to create a :context from :context2
577
     */
578
    public function iWantToCreateAContextFromContext2($context, $context2)
579
    {
580
        $context = $this->getContext($context);
581
        $context2 = $this->getContext($context2);
582
        $module = $this->getModuleName($context);
583
        $request = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'CreateFrom' . $context2 . 'Request';
584
        $requestContext = $context2 . 'Request';
585
        $id = $this->objects[$requestContext]['id'];
586
        $version = $this->objects[$requestContext]['version'];
587
        $this->request = call_user_func_array(
588
            $request. '::ofCartIdAndVersion',
589
            [$id, $version]
590
        );
591
    }
592
593
    /**
594
     * @Given set the orderNumber to :orderNumber and the paymentState to :paymentState
595
     */
596
    public function setTheOrderNumberToAndThePaymentStateTo($orderNumber, $paymentState)
597
    {
598
        $this->request->setOrderNumber($orderNumber)->setPaymentState($paymentState);
599
    }
600
601
    /**
602
     * @Given i want to import a :context with values
603
     */
604
    public function iWantToImportAWithValues($context, PyStringNode $json)
605
    {
606
        $module = $this->getModuleName($context);
607
        $context = $this->getContext($context);
608
609
        $importClass = '\Commercetools\Core\Model\\' . $context . '\\Import' . $context;
610
        $requestClass = '\Commercetools\Core\Request\\' . $module . '\\' . $context . 'ImportRequest';
611
612
        $rawData = json_decode((string)$json, true);
613
        $object = call_user_func_array($importClass.'::fromArray', [$rawData]);
614
        $this->forceTyping($object, $rawData);
0 ignored issues
show
Unused Code introduced by
The call to Commercetools\Core\ApiContext::forceTyping() has too many arguments starting with $rawData. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

614
        $this->/** @scrutinizer ignore-call */ 
615
               forceTyping($object, $rawData);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
615
616
        $this->request = call_user_func_array(
617
            $requestClass . '::ofImport'.$context,
618
            [$object]
619
        );
620
    }
621
}
622