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

requestPostCustomerFailsValidationDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 219
Code Lines 143

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 143
dl 0
loc 219
c 1
b 0
f 0
rs 8
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
namespace PyzTest\Glue\Customer\RestApi;
9
10
use Codeception\Example;
11
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...
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 CustomerRegistrationCest
28
 * Add your own group annotations below this line
29
 * @group EndToEnd
30
 */
31
class CustomerRegistrationCest
32
{
33
    /**
34
     * @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures
35
     */
36
    protected CustomerRestApiFixtures $fixtures;
37
38
    /**
39
     * @param \PyzTest\Glue\Customer\CustomerApiTester $i
40
     *
41
     * @return void
42
     */
43
    public function _before(CustomerApiTester $i): void
44
    {
45
        /** @var \PyzTest\Glue\Customer\RestApi\CustomerRestApiFixtures $fixtures */
46
        $fixtures = $i->loadFixtures(CustomerRestApiFixtures::class);
47
48
        $this->fixtures = $fixtures;
49
    }
50
51
    /**
52
     * @param \PyzTest\Glue\Customer\CustomerApiTester $I
53
     *
54
     * @return void
55
     */
56
    public function requestPostCustomerFailsOnExistingEmailUsage(CustomerApiTester $I): void
57
    {
58
        // Arrange
59
        /** @var \Generated\Shared\Transfer\RestCustomersAttributesTransfer $restCustomersAttributesTransfer */
60
        $restCustomersAttributesTransfer = (new RestCustomersAttributesBuilder([
61
            RestCustomersAttributesTransfer::PASSWORD => 'change123',
62
            RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'change123',
63
            RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
64
        ]))->build();
65
        $customerTransfer = $I->haveCustomer(
66
            [
67
                CustomerTransfer::NEW_PASSWORD => 'change123',
68
                CustomerTransfer::PASSWORD => 'change123',
69
            ],
70
        );
71
        $I->confirmCustomer($customerTransfer);
72
73
        $restCustomersAttributesTransfer->setEmail($customerTransfer->getEmail());
74
75
        // Act
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], $index);
124
            $I->seeResponseErrorsHaveStatus($error[RestErrorMessageTransfer::STATUS], $index);
125
            $I->seeResponseErrorsHaveDetail($error[RestErrorMessageTransfer::DETAIL], $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 => 'change123',
138
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'change123',
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 => 'change123',
153
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'change123',
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 => 'change123',
169
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'change123',
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 => 'change123',
183
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'change1234',
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 => 'change123',
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 => 'change123',
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 => 'qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiop',
230
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiopqwertyuiop',
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 64 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 64 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 8 characters or more.',
259
                    ],
260
                    [
261
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
262
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
263
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value is too short. It should have 8 characters or more.',
264
                    ],
265
                ],
266
            ],
267
            [
268
                'attributes' => (new RestCustomersAttributesBuilder([
269
                    RestCustomersAttributesTransfer::PASSWORD => 'qwertyui',
270
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwertyui',
271
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
272
                ]))->build(),
273
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
274
                'errors' => [
275
                    [
276
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_PASSWORD_INVALID_CHARACTER_SET,
277
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
278
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_PASSWORD_INVALID_CHARACTER_SET,
279
                    ],
280
                ],
281
            ],
282
            [
283
                'attributes' => (new RestCustomersAttributesBuilder([
284
                    RestCustomersAttributesTransfer::PASSWORD => 'qwertyuI1!eee',
285
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'qwertyuI1!eee',
286
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
287
                ]))->build(),
288
                RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
289
                'errors' => [
290
                    [
291
                        RestErrorMessageTransfer::CODE => CustomersRestApiConfig::RESPONSE_CODE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
292
                        RestErrorMessageTransfer::STATUS => Response::HTTP_BAD_REQUEST,
293
                        RestErrorMessageTransfer::DETAIL => CustomersRestApiConfig::RESPONSE_MESSAGE_CUSTOMER_PASSWORD_SEQUENCE_NOT_ALLOWED,
294
                    ],
295
                ],
296
            ],
297
            [
298
                'attributes' => (new RestCustomersAttributesBuilder([
299
                    RestCustomersAttributesTransfer::PASSWORD => 'change123',
300
                    RestCustomersAttributesTransfer::CONFIRM_PASSWORD => 'change123',
301
                    RestCustomersAttributesTransfer::ACCEPTED_TERMS => true,
302
                    RestCustomersAttributesTransfer::GENDER => 'xyz',
303
                ]))->build(),
304
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
305
                'errors' => [
306
                    [
307
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
308
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
309
                        RestErrorMessageTransfer::DETAIL => 'gender => The value you selected is not a valid choice.',
310
                    ],
311
                ],
312
            ],
313
            [
314
                'attributes' => new RestCustomersAttributesTransfer(),
315
                RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
316
                'errors' => [
317
                    [
318
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
319
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
320
                        RestErrorMessageTransfer::DETAIL => 'email => 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 => 'gender => 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 => 'salutation => 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 => 'firstName => 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 => 'lastName => This value should not be blank.',
341
                    ],
342
                    [
343
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
344
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
345
                        RestErrorMessageTransfer::DETAIL => 'password => This value should not be blank.',
346
                    ],
347
                    [
348
                        RestErrorMessageTransfer::CODE => RestRequestValidatorConfig::RESPONSE_CODE_REQUEST_INVALID,
349
                        RestErrorMessageTransfer::STATUS => Response::HTTP_UNPROCESSABLE_ENTITY,
350
                        RestErrorMessageTransfer::DETAIL => 'confirmPassword => This value should not be blank.',
351
                    ],
352
                ],
353
            ],
354
        ];
355
    }
356
}
357