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