Passed
Pull Request — master (#365)
by Dmitry
21:01
created

requestPatchCustomerFailsWithoutCustomerReference()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 34
c 1
b 0
f 0
rs 9.7333
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Spryker Commerce OS.
5
 * For full license information, please view the LICENSE file that was distributed with this source code.
6
 */
7
8
namespace PyzTest\Glue\Customer\RestApi;
9
10
use Codeception\Example;
11
use Codeception\Util\HttpCode;
12
use Generated\Shared\Transfer\CustomerTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\CustomerTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Generated\Shared\Transfer\RestCustomersAttributesTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...omersAttributesTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Generated\Shared\Transfer\RestErrorMessageTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\RestErrorMessageTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use PyzTest\Glue\Customer\CustomerApiTester;
16
use Spryker\Glue\CustomersRestApi\CustomersRestApiConfig;
17
use Spryker\Glue\RestRequestValidator\RestRequestValidatorConfig;
18
use Symfony\Component\HttpFoundation\Response;
19
20
/**
21
 * Auto-generated group annotations
22
 *
23
 * @group PyzTest
24
 * @group Glue
25
 * @group Customer
26
 * @group RestApi
27
 * @group CustomerUpdateCest
28
 * Add your own group annotations below this line
29
 * @group EndToEnd
30
 */
31
class CustomerUpdateCest
32
{
33
    /**
34
     * @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures
35
     */
36
    protected CustomerRestApiFixtures $fixtures;
37
38
    /**
39
     * @var \Generated\Shared\Transfer\CustomerTransfer
40
     */
41
    protected CustomerTransfer $customerTransfer;
42
43
    /**
44
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
45
     *
46
     * @return void
47
     */
48
    public function _before(CustomerApiTester $I): void
49
    {
50
        /** @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures $fixtures */
51
        $fixtures = $I->loadFixtures(CustomerRestApiFixtures::class);
52
53
        $this->fixtures = $fixtures;
54
55
        $this->customerTransfer = $I->haveCustomer(
56
            [
57
                CustomerTransfer::NEW_PASSWORD => 'change123',
58
                CustomerTransfer::PASSWORD => 'change123',
59
            ],
60
        );
61
        $I->confirmCustomer($this->customerTransfer);
62
63
        $oauthResponseTransfer = $I->haveAuthorizationToGlue($this->customerTransfer);
64
        $I->amBearerAuthenticated($oauthResponseTransfer->getAccessToken());
65
    }
66
67
    /**
68
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
69
     *
70
     * @return void
71
     */
72
    public function requestPatchCustomerUpdatesCustomerProfile(CustomerApiTester $I): void
73
    {
74
        // Arrange
75
        $firstName = uniqid('name');
76
        $restCustomersAttributesTransfer = (new RestCustomersAttributesTransfer())
77
            ->setFirstName($firstName)
78
            ->setGender($this->customerTransfer->getGender())
79
            ->setSalutation($this->customerTransfer->getSalutation());
80
81
        // Act
82
        $I->sendPatch(
83
            $I->formatUrl(
84
                '{resourceCustomers}/{customerReference}',
85
                [
86
                    'resourceCustomers' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
87
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
88
                ],
89
            ),
90
            [
91
                'data' => [
92
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
93
                    'id' => $this->customerTransfer->getCustomerReference(),
94
                    'attributes' => $restCustomersAttributesTransfer->modifiedToArray(true, true),
95
                ],
96
            ],
97
        );
98
99
        // Assert
100
        $I->seeResponseCodeIs(HttpCode::OK);
101
        $I->seeResponseIsJson();
102
        $I->seeResponseMatchesOpenApiSchema();
103
104
        $I->seeSingleResourceHasSelfLink(
105
            $I->formatFullUrl(
106
                '{resourceCustomers}/{customerReference}',
107
                [
108
                    'resourceCustomers' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
109
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
110
                ],
111
            ),
112
        );
113
114
        $I->amSure(sprintf('Returned resource is of type %s', CustomersRestApiConfig::RESOURCE_CUSTOMERS))
115
            ->whenI()
116
            ->seeResponseDataContainsSingleResourceOfType(CustomersRestApiConfig::RESOURCE_CUSTOMERS);
0 ignored issues
show
Bug introduced by
It seems like seeResponseDataContainsSingleResourceOfType() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

116
            ->/** @scrutinizer ignore-call */ seeResponseDataContainsSingleResourceOfType(CustomersRestApiConfig::RESOURCE_CUSTOMERS);
Loading history...
117
118
        $actualResponseAttributes = $I->getDataFromResponseByJsonPath('$.data.attributes');
119
        $I->amSure(sprintf('Attribute %s has been updated', 'firstName'))
120
            ->whenI()
121
            ->assertSame($firstName, $actualResponseAttributes['firstName']);
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

121
            ->/** @scrutinizer ignore-call */ assertSame($firstName, $actualResponseAttributes['firstName']);
Loading history...
122
123
        $I->amSure('Returned resource correct attributes')
124
            ->whenI()
125
            ->assertCustomersAttributes(
0 ignored issues
show
Bug introduced by
It seems like assertCustomersAttributes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

125
            ->/** @scrutinizer ignore-call */ assertCustomersAttributes(
Loading history...
126
                $this->customerTransfer->setFirstName($firstName),
127
                $I->getDataFromResponseByJsonPath('$.data.attributes'),
128
            );
129
    }
130
131
    /**
132
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
133
     *
134
     * @return void
135
     */
136
    public function requestPatchCustomerFailsToUseAnotherCustomersEmail(CustomerApiTester $I): void
137
    {
138
        // Arrange
139
        $firstCustomerTransfer = $I->haveCustomer(
140
            [
141
                CustomerTransfer::NEW_PASSWORD => 'change123',
142
                CustomerTransfer::PASSWORD => 'change123',
143
            ],
144
        );
145
146
        $restCustomersAttributesTransfer = (new RestCustomersAttributesTransfer())
147
            ->setEmail($firstCustomerTransfer->getEmail())
148
            ->setGender($this->customerTransfer->getGender())
149
            ->setSalutation($this->customerTransfer->getSalutation());
150
151
        // Act
152
        $I->sendPatch(
153
            $I->formatUrl(
154
                '{resourceCustomers}/{customerReference}',
155
                [
156
                    'resourceCustomers' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
157
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
158
                ],
159
            ),
160
            [
161
                'data' => [
162
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
163
                    'id' => $this->customerTransfer->getCustomerReference(),
164
                    'attributes' => $restCustomersAttributesTransfer->modifiedToArray(true, true),
165
                ],
166
            ],
167
        );
168
169
        // Assert
170
        $I->seeResponseCodeIs(Response::HTTP_UNPROCESSABLE_ENTITY);
171
        $I->seeResponseIsJson();
172
        $I->seeResponseMatchesOpenApiSchema();
173
174
        $I->seeResponseErrorsHaveCode(CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_ALREADY_EXISTS);
175
        $I->seeResponseErrorsHaveStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
176
        $I->seeResponseErrorsHaveDetail(CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_ALREADY_EXISTS);
177
    }
178
179
    /**
180
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
181
     *
182
     * @return void
183
     */
184
    public function requestPatchCustomerFailsToUseAnotherCustomersCustomerReference(CustomerApiTester $I): void
185
    {
186
        // Arrange
187
        $firstCustomerTransfer = $I->haveCustomer(
188
            [
189
                CustomerTransfer::NEW_PASSWORD => 'change123',
190
                CustomerTransfer::PASSWORD => 'change123',
191
            ],
192
        );
193
194
        $restCustomersAttributesTransfer = (new RestCustomersAttributesTransfer())
195
            ->setFirstName(uniqid())
196
            ->setGender($this->customerTransfer->getGender())
197
            ->setSalutation($this->customerTransfer->getSalutation());
198
199
        // Act
200
        $I->sendPatch(
201
            $I->formatUrl(
202
                '{resourceCustomers}/{customerReference}',
203
                [
204
                    'resourceCustomers' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
205
                    'customerReference' => $firstCustomerTransfer->getCustomerReference(),
206
                ],
207
            ),
208
            [
209
                'data' => [
210
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
211
                    'id' => $firstCustomerTransfer->getCustomerReference(),
212
                    'attributes' => $restCustomersAttributesTransfer->modifiedToArray(true, true),
213
                ],
214
            ],
215
        );
216
217
        // Assert
218
        $I->seeResponseCodeIs(Response::HTTP_FORBIDDEN);
219
        $I->seeResponseIsJson();
220
        $I->seeResponseMatchesOpenApiSchema();
221
222
        $I->seeResponseErrorsHaveCode(CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_UNAUTHORIZED);
223
        $I->seeResponseErrorsHaveStatus(Response::HTTP_FORBIDDEN);
224
        $I->seeResponseErrorsHaveDetail(CustomersRestApiConfig::RESPONSE_DETAILS_CUSTOMER_UNAUTHORIZED);
225
    }
226
227
    /**
228
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
229
     *
230
     * @return void
231
     */
232
    public function requestPatchCustomerFailsWithoutCustomerReference(CustomerApiTester $I): void
233
    {
234
        // Arrange
235
        $restCustomersAttributesTransfer = (new RestCustomersAttributesTransfer())
236
            ->setFirstName(uniqid())
237
            ->setGender($this->customerTransfer->getGender())
238
            ->setSalutation($this->customerTransfer->getSalutation());
239
240
        // Act
241
        $I->sendPatch(
242
            $I->formatUrl(
243
                '{resourceCustomers}/',
244
                [
245
                    'resourceCustomers' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
246
                ],
247
            ),
248
            [
249
                'data' => [
250
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
251
                    'id' => $this->customerTransfer->getCustomerReference(),
252
                    'attributes' => $restCustomersAttributesTransfer->modifiedToArray(true, true),
253
                ],
254
            ],
255
        );
256
257
        // Assert
258
        $I->seeResponseCodeIs(Response::HTTP_BAD_REQUEST);
259
        $I->seeResponseIsJson();
260
261
        $I->seeResponseErrorsHaveStatus(Response::HTTP_BAD_REQUEST);
262
        /**
263
         * @uses \Spryker\Glue\GlueApplication\Rest\Request\RestRequestValidator::EXCEPTION_MESSAGE_RESOURCE_ID_IS_NOT_SPECIFIED
264
         */
265
        $I->seeResponseErrorsHaveDetail('Resource id is not specified.');
266
    }
267
268
    /**
269
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
270
     *
271
     * @return void
272
     */
273
    public function requestPatchCustomerFailsWhenPasswordsDoNotMatch(CustomerApiTester $I): void
274
    {
275
        // Arrange
276
        $restCustomersAttributesTransfer = (new RestCustomersAttributesTransfer())
277
            ->setPassword('change123')
278
            ->setConfirmPassword('change1234')
279
            ->setGender($this->customerTransfer->getGender())
280
            ->setSalutation($this->customerTransfer->getSalutation());
281
282
        // Act
283
        $I->sendPatch(
284
            $I->formatUrl(
285
                '{resourceCustomers}/{customerReference}',
286
                [
287
                    'resourceCustomers' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
288
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
289
                ],
290
            ),
291
            [
292
                'data' => [
293
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
294
                    'id' => $this->customerTransfer->getCustomerReference(),
295
                    'attributes' => $restCustomersAttributesTransfer->modifiedToArray(true, true),
296
                ],
297
            ],
298
        );
299
300
        // Assert
301
        $I->seeResponseCodeIs(Response::HTTP_UNPROCESSABLE_ENTITY);
302
        $I->seeResponseIsJson();
303
        $I->seeResponseMatchesOpenApiSchema();
304
305
        $I->seeResponseErrorsHaveCode(CustomersRestApiConfig::RESPONSE_CODE_PASSWORDS_DONT_MATCH);
306
        $I->seeResponseErrorsHaveStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
307
        $I->seeResponseErrorsHaveDetail(sprintf(
308
            CustomersRestApiConfig::RESPONSE_DETAILS_PASSWORDS_DONT_MATCH,
309
            RestCustomersAttributesTransfer::PASSWORD,
310
            RestCustomersAttributesTransfer::CONFIRM_PASSWORD,
311
        ));
312
    }
313
314
    /**
315
     * @dataProvider requestPatchCustomerFailsValidationDataProvider
316
     *
317
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
318
     * @param \Codeception\Example $example
319
     *
320
     * @return void
321
     */
322
    public function requestPatchCustomerFailsValidation(CustomerApiTester $I, Example $example): void
323
    {
324
        // Act
325
        $I->sendPatch(
326
            $I->formatUrl(
327
                '{resourceCustomers}/{customerReference}',
328
                [
329
                    'resourceCustomers' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
330
                    'customerReference' => $this->customerTransfer->getCustomerReference(),
331
                ],
332
            ),
333
            [
334
                'data' => [
335
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
336
                    'id' => $this->customerTransfer->getCustomerReference(),
337
                    'attributes' => $example['attributes'],
338
                ],
339
            ],
340
        );
341
342
        // Assert
343
        $I->seeResponseCodeIs($example[RestErrorMessageTransfer::STATUS]);
344
        $I->seeResponseIsJson();
345
        $I->seeResponseMatchesOpenApiSchema();
346
347
        foreach ($example['errors'] as $index => $error) {
348
            $I->seeResponseErrorsHaveCode($error[RestErrorMessageTransfer::CODE], $index);
349
            $I->seeResponseErrorsHaveStatus($error[RestErrorMessageTransfer::STATUS], $index);
350
            $I->seeResponseErrorsHaveDetail($error[RestErrorMessageTransfer::DETAIL], $index);
351
        }
352
    }
353
354
    /**
355
     * @return array
356
     */
357
    protected function requestPatchCustomerFailsValidationDataProvider(): array
358
    {
359
        return [
360
            [
361
                'attributes' => [
362
                    RestCustomersAttributesTransfer::PASSWORD => 'qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiop',
363
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiop',
364
                    RestCustomersAttributesTransfer::GENDER => 'Male',
365
                    RestCustomersAttributesTransfer::SALUTATION => 'Mr',
366
                ],
367
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
368
                'errors' => [
369
                    [
370
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
371
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
372
                        RestErrorMessageTransfer::DETAIL => 'password => This value is too long. It should have 64 characters or less.',
373
                    ],
374
                    [
375
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
376
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
377
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too long. It should have 64 characters or less.',
378
                    ],
379
                ],
380
            ],
381
            [
382
                'attributes' => [
383
                    RestCustomersAttributesTransfer::PASSWORD => 'qwe',
384
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwe',
385
                    RestCustomersAttributesTransfer::GENDER => 'Male',
386
                    RestCustomersAttributesTransfer::SALUTATION => 'Mr',
387
                ],
388
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
389
                'errors' => [
390
                    [
391
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
392
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
393
                        RestErrorMessageTransfer::DETAIL => 'password => This value is too short. It should have 8 characters or more.',
394
                    ],
395
                    [
396
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
397
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
398
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too short. It should have 8 characters or more.',
399
                    ],
400
                ],
401
            ],
402
        ];
403
    }
404
}
405