1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Awurth\SlimValidation\Tests; |
4
|
|
|
|
5
|
|
|
use Awurth\SlimValidation\Validator; |
6
|
|
|
use Awurth\SlimValidation\ValidatorExtension; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class ValidatorExtensionTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Validator |
13
|
|
|
*/ |
14
|
|
|
protected $validator; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ValidatorExtension |
18
|
|
|
*/ |
19
|
|
|
protected $validatorExtension; |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
$this->validator = new Validator(); |
24
|
|
|
|
25
|
|
|
$this->validatorExtension = new ValidatorExtension($this->validator); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
View Code Duplication |
public function testGetError() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this->assertEquals('', $this->validatorExtension->getError('username')); |
31
|
|
|
|
32
|
|
|
$this->validator->addError('username', 'Bad username'); |
33
|
|
|
$this->validator->addError('username', 'Too short!'); |
34
|
|
|
|
35
|
|
|
$this->assertEquals('Bad username', $this->validatorExtension->getError('username')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testGetErrors() |
39
|
|
|
{ |
40
|
|
|
$this->assertEquals([], $this->validatorExtension->getErrors()); |
41
|
|
|
$this->assertEquals([], $this->validatorExtension->getErrors('username')); |
42
|
|
|
|
43
|
|
|
$this->validator->addError('username', 'Bad username'); |
44
|
|
|
$this->validator->addError('password', 'Wrong password'); |
45
|
|
|
|
46
|
|
|
$this->assertEquals([ |
47
|
|
|
'username' => ['Bad username'], |
48
|
|
|
'password' => ['Wrong password'] |
49
|
|
|
], $this->validatorExtension->getErrors()); |
50
|
|
|
|
51
|
|
|
$this->assertEquals(['Bad username'], $this->validatorExtension->getErrors('username')); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testGetRuleError() |
55
|
|
|
{ |
56
|
|
|
$this->assertEquals('', $this->validatorExtension->getRuleError('username', 'notBlank')); |
57
|
|
|
|
58
|
|
|
$this->validator->setErrors([ |
59
|
|
|
'username' => [ |
60
|
|
|
'length' => 'Too short!', |
61
|
|
|
'notBlank' => 'Required' |
62
|
|
|
] |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$this->assertEquals('Required', $this->validatorExtension->getRuleError('username', 'notBlank')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
View Code Duplication |
public function testGetValue() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
$this->assertEquals('', $this->validatorExtension->getValue('username')); |
71
|
|
|
|
72
|
|
|
$this->validator->setValues(['username' => 'awurth']); |
73
|
|
|
|
74
|
|
|
$this->assertEquals('awurth', $this->validatorExtension->getValue('username')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testHasError() |
78
|
|
|
{ |
79
|
|
|
$this->assertFalse($this->validatorExtension->hasError('username')); |
80
|
|
|
|
81
|
|
|
$this->validator->addError('username', 'Bad username'); |
82
|
|
|
|
83
|
|
|
$this->assertTrue($this->validatorExtension->hasError('username')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testHasErrors() |
87
|
|
|
{ |
88
|
|
|
$this->assertFalse($this->validatorExtension->hasErrors()); |
89
|
|
|
|
90
|
|
|
$this->validator->addError('username', 'Bad username'); |
91
|
|
|
|
92
|
|
|
$this->assertTrue($this->validatorExtension->hasErrors()); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
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.