Completed
Branch 2.x (0b1fa1)
by Alexis
03:21
created

ValidatorTest   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 283
Duplicated Lines 39.22 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 5
dl 111
loc 283
rs 10
c 0
b 0
f 0

18 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 8 1
A testValidateWithoutRules() 0 6 1
A testValidateWithWrongOptionsType() 0 6 1
A testValidateWithWrongRulesType() 0 8 1
A testValidate() 0 10 1
A testValidateWithErrors() 15 15 1
A testValidateWithIndexedErrors() 16 16 1
A testValidateWithCustomDefaultMessage() 19 19 1
A testValidateWithCustomGlobalMessages() 20 20 1
B testValidateWithCustomDefaultAndGlobalMessages() 0 25 1
A testValidateWithWrongCustomIndividualMessagesType() 9 9 1
A testValidateWithCustomIndividualMessage() 23 23 1
A testValidateWithWrongCustomSingleMessageType() 9 9 1
B testValidateWithCustomSingleMessage() 0 29 1
A testIsValidWithErrors() 0 6 1
A testIsValidWithoutErrors() 0 6 1
A testAddError() 0 10 1
A testAddErrors() 0 11 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Awurth\SlimValidation\Tests;
4
5
use Awurth\SlimValidation\Validator;
6
use InvalidArgumentException;
7
use PHPUnit\Framework\TestCase;
8
use Respect\Validation\Validator as V;
9
use Slim\Http\Environment;
10
use Slim\Http\Request;
11
12
class ValidatorTest extends TestCase
13
{
14
    /**
15
     * @var Validator
16
     */
17
    protected $validator;
18
19
    /**
20
     * @var Request
21
     */
22
    protected $request;
23
24
    public function setUp()
25
    {
26
        $this->request = Request::createFromEnvironment(Environment::mock([
27
            'QUERY_STRING' => 'username=a_wurth&password=1234'
28
        ]));
29
30
        $this->validator = new Validator();
31
    }
32
33
    /**
34
     * @expectedException InvalidArgumentException
35
     */
36
    public function testValidateWithoutRules()
37
    {
38
        $this->validator->validate($this->request, [
39
            'username'
40
        ]);
41
    }
42
43
    /**
44
     * @expectedException InvalidArgumentException
45
     */
46
    public function testValidateWithWrongOptionsType()
47
    {
48
        $this->validator->validate($this->request, [
49
            'username' => null
50
        ]);
51
    }
52
53
    /**
54
     * @expectedException InvalidArgumentException
55
     */
56
    public function testValidateWithWrongRulesType()
57
    {
58
        $this->validator->validate($this->request, [
59
            'username' => [
60
                'rules' => null
61
            ]
62
        ]);
63
    }
64
65
    public function testValidate()
66
    {
67
        $this->validator->validate($this->request, [
68
            'username' => V::length(6)
69
        ]);
70
71
        $this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues());
72
        $this->assertEquals('a_wurth', $this->validator->getValue('username'));
73
        $this->assertTrue($this->validator->isValid());
74
    }
75
76 View Code Duplication
    public function testValidateWithErrors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $this->validator->validate($this->request, [
79
            'username' => V::length(8)
80
        ]);
81
82
        $this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues());
83
        $this->assertEquals('a_wurth', $this->validator->getValue('username'));
84
        $this->assertFalse($this->validator->isValid());
85
        $this->assertEquals([
86
            'username' => [
87
                'length' => '"a_wurth" must have a length greater than 8'
88
            ]
89
        ], $this->validator->getErrors());
90
    }
91
92 View Code Duplication
    public function testValidateWithIndexedErrors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        $this->validator->setStoreErrorsWithRules(false);
95
        $this->validator->validate($this->request, [
96
            'username' => V::length(8)
97
        ]);
98
99
        $this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues());
100
        $this->assertEquals('a_wurth', $this->validator->getValue('username'));
101
        $this->assertFalse($this->validator->isValid());
102
        $this->assertEquals([
103
            'username' => [
104
                '"a_wurth" must have a length greater than 8'
105
            ]
106
        ], $this->validator->getErrors());
107
    }
108
109 View Code Duplication
    public function testValidateWithCustomDefaultMessage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $this->validator->setDefaultMessages([
112
            'length' => 'Too short!'
113
        ]);
114
115
        $this->validator->validate($this->request, [
116
            'username' => V::length(8)
117
        ]);
118
119
        $this->assertEquals(['username' => 'a_wurth'], $this->validator->getValues());
120
        $this->assertEquals('a_wurth', $this->validator->getValue('username'));
121
        $this->assertFalse($this->validator->isValid());
122
        $this->assertEquals([
123
            'username' => [
124
                'length' => 'Too short!'
125
            ]
126
        ], $this->validator->getErrors());
127
    }
128
129 View Code Duplication
    public function testValidateWithCustomGlobalMessages()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $this->validator->validate($this->request, [
132
            'username' => V::length(8),
133
            'password' => V::length(8)
134
        ], [
135
            'length' => 'Too short!'
136
        ]);
137
138
        $this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues());
139
        $this->assertFalse($this->validator->isValid());
140
        $this->assertEquals([
141
            'username' => [
142
                'length' => 'Too short!'
143
            ],
144
            'password' => [
145
                'length' => 'Too short!'
146
            ]
147
        ], $this->validator->getErrors());
148
    }
149
150
    public function testValidateWithCustomDefaultAndGlobalMessages()
151
    {
152
        $this->validator->setDefaultMessages([
153
            'length' => 'Too short!'
154
        ]);
155
156
        $this->validator->validate($this->request, [
157
            'username' => V::length(8),
158
            'password' => V::length(8)->alpha()
159
        ], [
160
            'alpha' => 'Only letters are allowed'
161
        ]);
162
163
        $this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues());
164
        $this->assertFalse($this->validator->isValid());
165
        $this->assertEquals([
166
            'username' => [
167
                'length' => 'Too short!'
168
            ],
169
            'password' => [
170
                'length' => 'Too short!',
171
                'alpha' => 'Only letters are allowed'
172
            ]
173
        ], $this->validator->getErrors());
174
    }
175
176
    /**
177
     * @expectedException InvalidArgumentException
178
     * @expectedExceptionMessage Expected custom individual messages to be of type array, string given
179
     */
180 View Code Duplication
    public function testValidateWithWrongCustomIndividualMessagesType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
181
    {
182
        $this->validator->validate($this->request, [
183
            'username' => [
184
                'rules' => V::length(8),
185
                'messages' => ''
186
            ]
187
        ]);
188
    }
189
190 View Code Duplication
    public function testValidateWithCustomIndividualMessage()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $this->validator->validate($this->request, [
193
            'username' => [
194
                'rules' => V::length(8),
195
                'messages' => [
196
                    'length' => 'Too short!'
197
                ]
198
            ],
199
            'password' => V::length(8)
200
        ]);
201
202
        $this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues());
203
        $this->assertFalse($this->validator->isValid());
204
        $this->assertEquals([
205
            'username' => [
206
                'length' => 'Too short!'
207
            ],
208
            'password' => [
209
                'length' => '"1234" must have a length greater than 8'
210
            ]
211
        ], $this->validator->getErrors());
212
    }
213
214
    /**
215
     * @expectedException InvalidArgumentException
216
     * @expectedExceptionMessage Expected custom message to be of type string, integer given
217
     */
218 View Code Duplication
    public function testValidateWithWrongCustomSingleMessageType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
219
    {
220
        $this->validator->validate($this->request, [
221
            'username' => [
222
                'rules' => V::length(8)->alnum(),
223
                'message' => 10
224
            ]
225
        ]);
226
    }
227
228
    public function testValidateWithCustomSingleMessage()
229
    {
230
        $this->validator->validate($this->request, [
231
            'username' => [
232
                'rules' => V::length(8)->alnum(),
233
                'message' => 'Bad username.',
234
                'messages' => [
235
                    'length' => 'Too short!'
236
                ]
237
            ],
238
            'password' => [
239
                'rules' => V::length(8),
240
                'messages' => [
241
                    'length' => 'Too short!'
242
                ]
243
            ]
244
        ]);
245
246
        $this->assertEquals(['username' => 'a_wurth', 'password' => '1234'], $this->validator->getValues());
247
        $this->assertFalse($this->validator->isValid());
248
        $this->assertEquals([
249
            'username' => [
250
                'Bad username.'
251
            ],
252
            'password' => [
253
                'length' => 'Too short!'
254
            ]
255
        ], $this->validator->getErrors());
256
    }
257
258
    public function testIsValidWithErrors()
259
    {
260
        $this->validator->setErrors(['error']);
261
262
        $this->assertFalse($this->validator->isValid());
263
    }
264
265
    public function testIsValidWithoutErrors()
266
    {
267
        $this->validator->setErrors([]);
268
269
        $this->assertTrue($this->validator->isValid());
270
    }
271
272
    public function testAddError()
273
    {
274
        $this->validator->addError('param', 'message');
275
276
        $this->assertEquals([
277
            'param' => [
278
                'message'
279
            ]
280
        ], $this->validator->getErrors());
281
    }
282
283
    public function testAddErrors()
284
    {
285
        $this->validator->addErrors('param', ['message1', 'message2']);
0 ignored issues
show
Deprecated Code introduced by
The method Awurth\SlimValidation\Validator::addErrors() has been deprecated with message: since version 2.1, will be removed in 3.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
286
287
        $this->assertEquals([
288
            'param' => [
289
                'message1',
290
                'message2'
291
            ]
292
        ], $this->validator->getErrors());
293
    }
294
}
295