Completed
Push — master ( ebff66...d47983 )
by Markus
06:38 queued 02:43
created

CValidateTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 105
Duplicated Lines 17.14 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 18
loc 105
rs 10
wmc 6
lcom 0
cbo 1

6 Methods

Rating   Name   Duplication   Size   Complexity  
A providerCheck() 9 9 1
A testCheck() 0 7 1
A testValidationRuleMissing() 0 5 1
A providerCheckFail() 9 9 1
A testCheckFailException() 0 5 1
A testCheckFail() 0 6 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 Anax\Validate;
4
5
/**
6
 * A helper to validate variables.
7
 *
8
 */
9
class CValidateTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * Provider for test values
13
     *
14
     * @return array
15
     */
16 View Code Duplication
    public function providerCheck()
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...
17
    {
18
        return [
19
            [ null, ['pass'] ],
20
            [ 0, ['int'] ],
21
            [ 1, ['range' => [1, 100]] ],
22
            [ 100, ['range' => [1, 100]] ],
23
      ];
24
    }
25
26
27
28
    /**
29
     * Test 
30
     *
31
     * @return void
32
     *
33
     * @dataProvider providerCheck
34
     *
35
     */
36
    public function testCheck($value, $rules) 
37
    {
38
        $validate = new \Anax\Validate\CValidate();
39
40
        $res = $validate->check($value, $rules);
41
        $this->assertTrue($res, "Should return the tested value.");
42
    }
43
44
45
46
    /**
47
     * Test 
48
     *
49
     * @dataProvider providerCheckFail
50
     *
51
     * @expectedException Exception
52
     *
53
     * @return void
54
     *
55
     */
56
    public function testValidationRuleMissing() 
57
    {
58
        $validate = new \Anax\Validate\CValidate();
59
        $validate->check(0, ['not_exists']);
0 ignored issues
show
Documentation introduced by
array('not_exists') is of type array<integer,string,{"0":"string"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
    }
61
62
63
64
    /**
65
     * Provider for test values
66
     *
67
     * @return array
68
     */
69 View Code Duplication
    public function providerCheckFail()
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...
70
    {
71
        return [
72
            [ null, ['fail'] ],
73
            [ "moped", ['int'] ],
74
            [ 0, ['range' => [1, 100]] ],
75
            [ 101, ['range' => [1, 100]] ],
76
        ];
77
    }
78
79
80
81
    /**
82
     * Test 
83
     *
84
     * @dataProvider providerCheckFail
85
     *
86
     * @expectedException Exception
87
     *
88
     * @return void
89
     *
90
     */
91
    public function testCheckFailException($value, $rules) 
92
    {
93
        $validate = new \Anax\Validate\CValidate();
94
        $validate->check($value, $rules, true);
95
    }
96
97
98
99
    /**
100
     * Test 
101
     *
102
     * @dataProvider providerCheckFail
103
     *
104
     * @return void
105
     *
106
     */
107
    public function testCheckFail($value, $rules) 
108
    {
109
        $validate = new \Anax\Validate\CValidate();
110
        $res = $validate->check($value, $rules);
111
        $this->assertFalse($res, "Should return the tested value.");
112
    }
113
}