requestPostCustomerFailsValidation()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 22
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
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
declare(strict_types = 1);
9
10
namespace PyzTest\Glue\Customer\RestApi;
11
12
use Codeception\Example;
13
use Generated\Shared\DataBuilder\RestCustomersAttributesBuilder;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\DataBui...tomersAttributesBuilder 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\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...
15
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...
16
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...
17
use PyzTest\Glue\Customer\CustomerApiTester;
18
use Spryker\Glue\CustomersRestApi\CustomersRestApiConfig;
19
use Spryker\Glue\RestRequestValidator\RestRequestValidatorConfig;
20
use Symfony\Component\HttpFoundation\Response;
21
22
/**
23
 * Auto-generated group annotations
24
 *
25
 * @group PyzTest
26
 * @group Glue
27
 * @group Customer
28
 * @group RestApi
29
 * @group CustomerRegistrationCest
30
 * Add your own group annotations below this line
31
 * @group EndToEnd
32
 */
33
class CustomerRegistrationCest
34
{
35
    protected CustomerRestApiFixtures $fixtures;
36
37
    public function _before(CustomerApiTester $i): void
38
    {
39
        /** @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures $fixtures */
40
        $fixtures = $i->loadFixtures(CustomerRestApiFixtures::class);
41
42
        $this->fixtures = $fixtures;
43
    }
44
45
    public function requestPostCustomerFailsOnExistingEmailUsage(CustomerApiTester $I): void
46
    {
47
        // Arrange
48
        /** @var \Generated\Shared\Transfer\RestCustomersAttributesTransfer $restCustomersAttributesTransfer */
49
        $restCustomersAttributesTransfer = (new RestCustomersAttributesBuilder([
50
            RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
51
            RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
52
            RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
53
        ]))->build();
54
        $customerTransfer = $I->haveCustomer(
55
            [
56
                CustomerTransfer::NEW_PASSWORD => 'Change!23456',
57
                CustomerTransfer::PASSWORD => 'Change!23456',
58
            ],
59
        );
60
        $I->confirmCustomer($customerTransfer);
61
62
        $restCustomersAttributesTransfer->setEmail($customerTransfer->getEmail());
63
64
        // Act
65
        $I->sendPOST(
66
            CustomersRestApiConfig::RESOURCE_CUSTOMERS,
67
            [
68
                'data' => [
69
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
70
                    'attributes' => $restCustomersAttributesTransfer->toArray(true, true),
71
                ],
72
            ],
73
        );
74
75
        // Assert
76
        $I->seeResponseCodeIs(Response::HTTP_UNPROCESSABLE_ENTITY);
77
        $I->seeResponseIsJson();
78
        $I->seeResponseMatchesOpenApiSchema();
79
80
        $I->seeResponseErrorsHaveCode(CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_ALREADY_EXISTS);
81
        $I->seeResponseErrorsHaveStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
82
        $I->seeResponseErrorsHaveDetail(CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_ALREADY_EXISTS);
83
    }
84
85
    /**
86
     * @dataProvider requestPostCustomerFailsValidationDataProvider
87
     *
88
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
89
     * @param \Codeception\Example $example
90
     */
91
    public function requestPostCustomerFailsValidation(CustomerApiTester $I, Example $example): void
92
    {
93
        // Act
94
        $I->sendPOST(
95
            CustomersRestApiConfig::RESOURCE_CUSTOMERS,
96
            [
97
                'data' => [
98
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
99
                    'attributes' => $example['attributes']->toArray(true, true),
100
                ],
101
            ],
102
        );
103
104
        // Assert
105
        $I->seeResponseCodeIs($example[RestErrorMessageTransfer::STATUS]);
106
        $I->seeResponseIsJson();
107
        $I->seeResponseMatchesOpenApiSchema();
108
109
        foreach ($example['errors'] as $index => $error) {
110
            $I->seeResponseErrorsHaveCode($error[RestErrorMessageTransfer::CODE], (string)$index);
111
            $I->seeResponseErrorsHaveStatus($error[RestErrorMessageTransfer::STATUS], (string)$index);
112
            $I->seeResponseErrorsHaveDetail($error[RestErrorMessageTransfer::DETAIL], (string)$index);
113
        }
114
    }
115
116
    /**
117
     * @return array<array<string, mixed>>
118
     */
119
    protected function requestPostCustomerFailsValidationDataProvider(): array
120
    {
121
        return [
122
            [
123
                'attributes' => (new RestCustomersAttributesBuilder([
124
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
125
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
126
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => false,
127
                ]))->build(),
128
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
129
                'errors' => [
130
                    [
131
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
132
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
133
                        RestErrorMessageTransfer::DETAIL => 'acceptedTerms => This value should be true.',
134
                    ],
135
                ],
136
            ],
137
            [
138
                'attributes' => (new RestCustomersAttributesBuilder([
139
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
140
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
141
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
142
                    RestCustomersAttributesTransfer::SALUTATION => 'xyz',
143
                ]))->build(),
144
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
145
                'errors' => [
146
                    [
147
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
148
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
149
                        RestErrorMessageTransfer::DETAIL => 'salutation => The value you selected is not a valid choice.',
150
                    ],
151
                ],
152
            ],
153
            [
154
                'attributes' => (new RestCustomersAttributesBuilder([
155
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
156
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
157
                ]))->build(),
158
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
159
                'errors' => [
160
                    [
161
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_NOT_ACCEPTED_TERMS,
162
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
163
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_DETAILS_NOT_ACCEPTED_TERMS,
164
                    ],
165
                ],
166
            ],
167
            [
168
                'attributes' => (new RestCustomersAttributesBuilder([
169
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
170
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!234564',
171
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
172
                ]))->build(),
173
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
174
                'errors' => [
175
                    [
176
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_PASSWORDS_DONT_MATCH,
177
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
178
                        RestErrorMessageTransfer::DETAIL => sprintf(
179
                            CustomersRestApiConfig::RESPONSE_DETAILS_PASSWORDS_DONT_MATCH,
180
                            RestCustomersAttributesTransfer::PASSWORD,
181
                            RestCustomersAttributesTransfer::CONFIRM_PASSWORD,
182
                        ),
183
                    ],
184
                ],
185
            ],
186
            [
187
                'attributes' => (new RestCustomersAttributesBuilder([
188
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
189
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
190
                ]))->build(),
191
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
192
                'errors' => [
193
                    [
194
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
195
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
196
                        RestErrorMessageTransfer::DETAIL => 'password => This value should not be blank.',
197
                    ],
198
                ],
199
            ],
200
            [
201
                'attributes' => (new RestCustomersAttributesBuilder([
202
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
203
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
204
                ]))->build(),
205
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
206
                'errors' => [
207
                    [
208
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
209
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
210
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value should not be blank.',
211
                    ],
212
                ],
213
            ],
214
            [
215
                'attributes' => (new RestCustomersAttributesBuilder([
216
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456pqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuioppqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwert',
217
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456pqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuioppqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwert',
218
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
219
                ]))->build(),
220
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
221
                'errors' => [
222
                    [
223
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
224
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
225
                        RestErrorMessageTransfer::DETAIL => 'password => This value is too long. It should have 128 characters or less.',
226
                    ],
227
                    [
228
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
229
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
230
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too long. It should have 128 characters or less.',
231
                    ],
232
                ],
233
            ],
234
            [
235
                'attributes' => (new RestCustomersAttributesBuilder([
236
                    RestCustomersAttributesTransfer::PASSWORD => 'qwe',
237
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwe',
238
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
239
                ]))->build(),
240
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
241
                'errors' => [
242
                    [
243
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
244
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
245
                        RestErrorMessageTransfer::DETAIL => 'password => This value is too short. It should have 12 characters or more.',
246
                    ],
247
                    [
248
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
249
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
250
                        RestErrorMessageTransfer::DETAIL => 'password => Your password must include at least one uppercase letter, one lowercase letter, one number, and one special character from the following list: !@#$%^&*()_-+=[]{}|;:<>.,/?\~. Non-Latin and other special characters are not allowed.',
251
                    ],
252
                    [
253
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
254
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
255
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too short. It should have 12 characters or more.',
256
                    ],
257
                ],
258
            ],
259
            [
260
                'attributes' => (new RestCustomersAttributesBuilder([
261
                    RestCustomersAttributesTransfer::PASSWORD => 'qwertyuI1!eee',
262
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwertyuI1!eee',
263
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
264
                ]))->build(),
265
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
266
                'errors' => [
267
                    [
268
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
269
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
270
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
271
                    ],
272
                ],
273
            ],
274
            [
275
                'attributes' => (new RestCustomersAttributesBuilder([
276
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
277
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
278
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
279
                    RestCustomersAttributesTransfer::GENDER => 'xyz',
280
                ]))->build(),
281
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
282
                'errors' => [
283
                    [
284
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
285
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
286
                        RestErrorMessageTransfer::DETAIL => 'gender => The value you selected is not a valid choice.',
287
                    ],
288
                ],
289
            ],
290
            [
291
                'attributes' => new RestCustomersAttributesTransfer(),
292
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
293
                'errors' => [
294
                    [
295
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
296
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
297
                        RestErrorMessageTransfer::DETAIL => 'email => This value should not be blank.',
298
                    ],
299
                    [
300
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
301
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
302
                        RestErrorMessageTransfer::DETAIL => 'gender => This value should not be blank.',
303
                    ],
304
                    [
305
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
306
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
307
                        RestErrorMessageTransfer::DETAIL => 'salutation => This value should not be blank.',
308
                    ],
309
                    [
310
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
311
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
312
                        RestErrorMessageTransfer::DETAIL => 'firstName => This value should not be blank.',
313
                    ],
314
                    [
315
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
316
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
317
                        RestErrorMessageTransfer::DETAIL => 'lastName => This value should not be blank.',
318
                    ],
319
                    [
320
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
321
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
322
                        RestErrorMessageTransfer::DETAIL => 'password => This value should not be blank.',
323
                    ],
324
                    [
325
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
326
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
327
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value should not be blank.',
328
                    ],
329
                ],
330
            ],
331
        ];
332
    }
333
}
334