requestPostCustomerFailsValidationDataProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 209
Code Lines 136

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 136
dl 0
loc 209
rs 8
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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;
14
use Generated\Shared\Transfer\CustomerTransfer;
15
use Generated\Shared\Transfer\RestCustomersAttributesTransfer;
16
use Generated\Shared\Transfer\RestErrorMessageTransfer;
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
    /**
36
     * @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures
37
     */
38
    protected CustomerRestApiFixtures $fixtures;
39
40
    /**
41
     * @param \PyzTest\Glue\Customer\CustomerApiTester $i
42
     *
43
     * @return void
44
     */
45
    public function _before(CustomerApiTester $i): void
46
    {
47
        /** @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures $fixtures */
48
        $fixtures = $i->loadFixtures(CustomerRestApiFixtures::class);
49
50
        $this->fixtures = $fixtures;
51
    }
52
53
    /**
54
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
55
     *
56
     * @return void
57
     */
58
    public function requestPostCustomerFailsOnExistingEmailUsage(CustomerApiTester $I): void
59
    {
60
        // Arrange
61
        /** @var \Generated\Shared\Transfer\RestCustomersAttributesTransfer $restCustomersAttributesTransfer */
62
        $restCustomersAttributesTransfer = (new RestCustomersAttributesBuilder([
63
            RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
64
            RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
65
            RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
66
        ]))->build();
67
        $customerTransfer = $I->haveCustomer(
68
            [
69
                CustomerTransfer::NEW_PASSWORD => 'Change!23456',
70
                CustomerTransfer::PASSWORD => 'Change!23456',
71
            ],
72
        );
73
        $I->confirmCustomer($customerTransfer);
74
75
        $restCustomersAttributesTransfer->setEmail($customerTransfer->getEmail());
76
77
        // Act
78
        $I->sendPOST(
79
            CustomersRestApiConfig::RESOURCE_CUSTOMERS,
80
            [
81
                'data' => [
82
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
83
                    'attributes' => $restCustomersAttributesTransfer->toArray(true, true),
84
                ],
85
            ],
86
        );
87
88
        // Assert
89
        $I->seeResponseCodeIs(Response::HTTP_UNPROCESSABLE_ENTITY);
90
        $I->seeResponseIsJson();
91
        $I->seeResponseMatchesOpenApiSchema();
92
93
        $I->seeResponseErrorsHaveCode(CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_ALREADY_EXISTS);
94
        $I->seeResponseErrorsHaveStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
95
        $I->seeResponseErrorsHaveDetail(CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_ALREADY_EXISTS);
96
    }
97
98
    /**
99
     * @dataProvider requestPostCustomerFailsValidationDataProvider
100
     *
101
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
102
     * @param \Codeception\Example $example
103
     *
104
     * @return void
105
     */
106
    public function requestPostCustomerFailsValidation(CustomerApiTester $I, Example $example): void
107
    {
108
        // Act
109
        $I->sendPOST(
110
            CustomersRestApiConfig::RESOURCE_CUSTOMERS,
111
            [
112
                'data' => [
113
                    'type' => CustomersRestApiConfig::RESOURCE_CUSTOMERS,
114
                    'attributes' => $example['attributes']->toArray(true, true),
115
                ],
116
            ],
117
        );
118
119
        // Assert
120
        $I->seeResponseCodeIs($example[RestErrorMessageTransfer::STATUS]);
121
        $I->seeResponseIsJson();
122
        $I->seeResponseMatchesOpenApiSchema();
123
124
        foreach ($example['errors'] as $index => $error) {
125
            $I->seeResponseErrorsHaveCode($error[RestErrorMessageTransfer::CODE], (string)$index);
126
            $I->seeResponseErrorsHaveStatus($error[RestErrorMessageTransfer::STATUS], (string)$index);
127
            $I->seeResponseErrorsHaveDetail($error[RestErrorMessageTransfer::DETAIL], (string)$index);
128
        }
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    protected function requestPostCustomerFailsValidationDataProvider(): array
135
    {
136
        return [
137
            [
138
                'attributes' => (new RestCustomersAttributesBuilder([
139
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
140
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
141
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => false,
142
                ]))->build(),
143
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
144
                'errors' => [
145
                    [
146
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
147
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
148
                        RestErrorMessageTransfer::DETAIL => 'acceptedTerms => This value should be true.',
149
                    ],
150
                ],
151
            ],
152
            [
153
                'attributes' => (new RestCustomersAttributesBuilder([
154
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
155
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
156
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
157
                    RestCustomersAttributesTransfer::SALUTATION => 'xyz',
158
                ]))->build(),
159
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
160
                'errors' => [
161
                    [
162
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
163
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
164
                        RestErrorMessageTransfer::DETAIL => 'salutation => The value you selected is not a valid choice.',
165
                    ],
166
                ],
167
            ],
168
            [
169
                'attributes' => (new RestCustomersAttributesBuilder([
170
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
171
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
172
                ]))->build(),
173
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
174
                'errors' => [
175
                    [
176
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_NOT_ACCEPTED_TERMS,
177
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
178
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_DETAILS_NOT_ACCEPTED_TERMS,
179
                    ],
180
                ],
181
            ],
182
            [
183
                'attributes' => (new RestCustomersAttributesBuilder([
184
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
185
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!234564',
186
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
187
                ]))->build(),
188
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
189
                'errors' => [
190
                    [
191
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_PASSWORDS_DONT_MATCH,
192
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
193
                        RestErrorMessageTransfer::DETAIL => sprintf(
194
                            CustomersRestApiConfig::RESPONSE_DETAILS_PASSWORDS_DONT_MATCH,
195
                            RestCustomersAttributesTransfer::PASSWORD,
196
                            RestCustomersAttributesTransfer::CONFIRM_PASSWORD,
197
                        ),
198
                    ],
199
                ],
200
            ],
201
            [
202
                'attributes' => (new RestCustomersAttributesBuilder([
203
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
204
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
205
                ]))->build(),
206
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
207
                'errors' => [
208
                    [
209
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
210
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
211
                        RestErrorMessageTransfer::DETAIL => 'password => This value should not be blank.',
212
                    ],
213
                ],
214
            ],
215
            [
216
                'attributes' => (new RestCustomersAttributesBuilder([
217
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
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 => 'confirmPassword => This value should not be blank.',
226
                    ],
227
                ],
228
            ],
229
            [
230
                'attributes' => (new RestCustomersAttributesBuilder([
231
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456pqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuioppqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwert',
232
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456pqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuioppqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwert',
233
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
234
                ]))->build(),
235
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
236
                'errors' => [
237
                    [
238
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
239
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
240
                        RestErrorMessageTransfer::DETAIL => 'password => This value is too long. It should have 128 characters or less.',
241
                    ],
242
                    [
243
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
244
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
245
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too long. It should have 128 characters or less.',
246
                    ],
247
                ],
248
            ],
249
            [
250
                'attributes' => (new RestCustomersAttributesBuilder([
251
                    RestCustomersAttributesTransfer::PASSWORD => 'qwe',
252
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwe',
253
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
254
                ]))->build(),
255
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
256
                'errors' => [
257
                    [
258
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
259
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
260
                        RestErrorMessageTransfer::DETAIL => 'password => This value is too short. It should have 12 characters or more.',
261
                    ],
262
                    [
263
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
264
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
265
                        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.',
266
                    ],
267
                    [
268
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
269
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
270
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too short. It should have 12 characters or more.',
271
                    ],
272
                ],
273
            ],
274
            [
275
                'attributes' => (new RestCustomersAttributesBuilder([
276
                    RestCustomersAttributesTransfer::PASSWORD => 'qwertyuI1!eee',
277
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwertyuI1!eee',
278
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
279
                ]))->build(),
280
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
281
                'errors' => [
282
                    [
283
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
284
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
285
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
286
                    ],
287
                ],
288
            ],
289
            [
290
                'attributes' => (new RestCustomersAttributesBuilder([
291
                    RestCustomersAttributesTransfer::PASSWORD => 'Change!23456',
292
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'Change!23456',
293
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
294
                    RestCustomersAttributesTransfer::GENDER => 'xyz',
295
                ]))->build(),
296
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
297
                'errors' => [
298
                    [
299
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
300
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
301
                        RestErrorMessageTransfer::DETAIL => 'gender => The value you selected is not a valid choice.',
302
                    ],
303
                ],
304
            ],
305
            [
306
                'attributes' => new RestCustomersAttributesTransfer(),
307
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
308
                'errors' => [
309
                    [
310
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
311
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
312
                        RestErrorMessageTransfer::DETAIL => 'email => 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 => 'gender => 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 => 'salutation => 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 => 'firstName => This value should not be blank.',
328
                    ],
329
                    [
330
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
331
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
332
                        RestErrorMessageTransfer::DETAIL => 'lastName => This value should not be blank.',
333
                    ],
334
                    [
335
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
336
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
337
                        RestErrorMessageTransfer::DETAIL => 'password => This value should not be blank.',
338
                    ],
339
                    [
340
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
341
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
342
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value should not be blank.',
343
                    ],
344
                ],
345
            ],
346
        ];
347
    }
348
}
349