UserTest::createValidatorExistingProvider()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 144
Code Lines 100

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 100
c 1
b 0
f 0
dl 0
loc 144
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
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Validation\Factory;
6
7
use AbterPhp\Admin\TestDouble\Validation\StubRulesFactory;
8
use AbterPhp\Framework\Http\Service\Execute\IRepoService;
9
use AbterPhp\Framework\Validation\Rules\Forbidden;
10
use AbterPhp\Framework\Validation\Rules\Uuid;
11
use Opulence\Validation\IValidator;
12
use Opulence\Validation\Rules\Factories\RulesFactory;
13
use PHPUnit\Framework\MockObject\MockObject;
14
use PHPUnit\Framework\TestCase;
15
16
class UserTest extends TestCase
17
{
18
    /** @var User - System Under Test */
19
    protected $sut;
20
21
    /** @var RulesFactory|MockObject */
22
    protected $rulesFactoryMock;
23
24
    public function setUp(): void
25
    {
26
        parent::setUp();
27
28
        $this->rulesFactoryMock = StubRulesFactory::createRulesFactory(
29
            $this,
30
            ['uuid' => new Uuid(), 'forbidden' => new Forbidden()]
31
        );
32
33
        $this->sut = new User($this->rulesFactoryMock);
34
    }
35
36
    /**
37
     * @return array
38
     */
39
    public function createValidatorExistingProvider(): array
40
    {
41
        return [
42
            'empty-data'                          => [
43
                [],
44
                false,
45
            ],
46
            'valid-data'                          => [
47
                [
48
                    'username'           => 'foo',
49
                    'email'              => '[email protected]',
50
                    'user_group_ids'     => [
51
                        '5c032f90-bf10-4a77-81aa-b0b1254a8f66',
52
                        '96aaef56-0e11-4f1c-b407-a8b65ff8e647',
53
                    ],
54
                    'user_language_id'   => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
55
                    'password'           => 'foo',
56
                    'password_confirmed' => 'foo',
57
                ],
58
                true,
59
            ],
60
            'valid-data-missing-all-not-required' => [
61
                [
62
                    'username'         => 'foo',
63
                    'email'            => '[email protected]',
64
                    'user_language_id' => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
65
                ],
66
                true,
67
            ],
68
            'invalid-has-id'                      => [
69
                [
70
                    'id'               => '465c91df-9cc7-47e2-a2ef-8fe645753148',
71
                    'username'         => 'foo',
72
                    'email'            => '[email protected]',
73
                    'user_language_id' => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
74
                ],
75
                false,
76
            ],
77
            'invalid-username-missing'            => [
78
                [
79
                    'username'           => '',
80
                    'email'              => '[email protected]',
81
                    'user_group_ids'     => [
82
                        '5c032f90-bf10-4a77-81aa-b0b1254a8f66',
83
                        '96aaef56-0e11-4f1c-b407-a8b65ff8e647',
84
                    ],
85
                    'user_language_id'   => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
86
                    'password'           => 'foo',
87
                    'password_confirmed' => 'foo',
88
                ],
89
                false,
90
            ],
91
            'invalid-email-not-valid'             => [
92
                [
93
                    'username'           => 'foo',
94
                    'email'              => 'user@@example.com',
95
                    'user_group_ids'     => [
96
                        '5c032f90-bf10-4a77-81aa-b0b1254a8f66',
97
                        '96aaef56-0e11-4f1c-b407-a8b65ff8e647',
98
                    ],
99
                    'user_language_id'   => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
100
                    'password'           => 'foo',
101
                    'password_confirmed' => 'foo',
102
                ],
103
                false,
104
            ],
105
            'invalid-email-empty'                 => [
106
                [
107
                    'username'           => 'foo',
108
                    'email'              => '',
109
                    'user_group_ids'     => [
110
                        '5c032f90-bf10-4a77-81aa-b0b1254a8f66',
111
                        '96aaef56-0e11-4f1c-b407-a8b65ff8e647',
112
                    ],
113
                    'user_language_id'   => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
114
                    'password'           => 'foo',
115
                    'password_confirmed' => 'foo',
116
                ],
117
                false,
118
            ],
119
            'invalid-email-missing'               => [
120
                [
121
                    'username'           => 'foo',
122
                    'user_group_ids'     => [
123
                        '5c032f90-bf10-4a77-81aa-b0b1254a8f66',
124
                        '96aaef56-0e11-4f1c-b407-a8b65ff8e647',
125
                    ],
126
                    'user_language_id'   => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
127
                    'password'           => 'foo',
128
                    'password_confirmed' => 'foo',
129
                ],
130
                false,
131
            ],
132
            'invalid-user-language-id-not-uuid'   => [
133
                [
134
                    'username'           => 'foo',
135
                    'email'              => '[email protected]',
136
                    'user_group_ids'     => [],
137
                    'user_language_id'   => 'df99af41-82fd-4865-a3d1-6a2eebf0951',
138
                    'password'           => 'foo',
139
                    'password_confirmed' => 'foo',
140
                ],
141
                false,
142
            ],
143
            'invalid-user-language-id-empty'      => [
144
                [
145
                    'username'           => 'foo',
146
                    'email'              => '[email protected]',
147
                    'user_group_ids'     => [
148
                        '5c032f90-bf10-4a77-81aa-b0b1254a8f66',
149
                        '96aaef56-0e11-4f1c-b407-a8b65ff8e647',
150
                    ],
151
                    'user_language_id'   => '',
152
                    'password'           => 'foo',
153
                    'password_confirmed' => 'foo',
154
                ],
155
                false,
156
            ],
157
            'invalid-user-language-id-missing'    => [
158
                [
159
                    'username'           => 'foo',
160
                    'email'              => '[email protected]',
161
                    'user_group_ids'     => [
162
                        '5c032f90-bf10-4a77-81aa-b0b1254a8f66',
163
                        '96aaef56-0e11-4f1c-b407-a8b65ff8e647',
164
                    ],
165
                    'password'           => 'foo',
166
                    'password_confirmed' => 'foo',
167
                ],
168
                false,
169
            ],
170
            'invalid-passwords-dont-match'        => [
171
                [
172
                    'username'           => 'foo',
173
                    'email'              => '[email protected]',
174
                    'user_group_ids'     => [
175
                        '5c032f90-bf10-4a77-81aa-b0b1254a8f66',
176
                        '96aaef56-0e11-4f1c-b407-a8b65ff8e647',
177
                    ],
178
                    'user_language_id'   => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
179
                    'password'           => 'foo',
180
                    'password_confirmed' => 'bar',
181
                ],
182
                false,
183
            ],
184
        ];
185
    }
186
187
    /**
188
     * @dataProvider createValidatorExistingProvider
189
     *
190
     * @param array $data
191
     * @param bool  $expectedResult
192
     */
193
    public function testCreateValidatorExisting(array $data, bool $expectedResult)
194
    {
195
        $this->runTestCreateValidator(IRepoService::UPDATE, $data, $expectedResult);
196
    }
197
198
    /**
199
     * @return array
200
     */
201
    public function createValidatorNewProvider(): array
202
    {
203
        $data = $this->createValidatorExistingProvider();
204
205
        $data['valid-data-missing-all-not-required'] = [
206
            [
207
                'username'           => 'foo',
208
                'email'              => '[email protected]',
209
                'user_language_id'   => 'df99af41-82fd-4865-a3d1-6a2eebf0951c',
210
                'password'           => 'foo',
211
                'password_confirmed' => 'foo',
212
            ],
213
            true,
214
        ];
215
216
        return $data;
217
    }
218
219
    /**
220
     * @dataProvider createValidatorNewProvider
221
     *
222
     * @param array $data
223
     * @param bool  $expectedResult
224
     */
225
    public function testCreateValidatorNew(array $data, bool $expectedResult)
226
    {
227
        $this->runTestCreateValidator(IRepoService::CREATE, $data, $expectedResult);
228
    }
229
230
    /**
231
     * @dataProvider createValidatorExistingProvider
232
     *
233
     * @param array $additionalData
234
     * @param array $data
235
     * @param bool  $expectedResult
236
     */
237
    public function runTestCreateValidator(int $additionalData, array $data, bool $expectedResult)
238
    {
239
        $this->sut->setAdditionalData($additionalData);
240
241
        $validator = $this->sut->createValidator();
242
243
        $this->assertInstanceOf(IValidator::class, $validator);
244
245
        $actualResult = $validator->isValid($data);
246
247
        $this->assertSame($expectedResult, $actualResult);
248
    }
249
}
250