Completed
Push — master ( 952a61...25ea1a )
by Alexis
01:36
created

ValidatorExtensionTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 19.77 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 17
loc 86
rs 10
c 0
b 0
f 0

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 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
    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
    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