Passed
Push — master ( 8381cb...5b292a )
by Peter
02:38
created

invalidDataWithDotNotationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Pixelpeter\IsoCodesValidation;
4
5
use Validator;
6
7
class IsoCodesValidatorTest extends TestCase
8
{
9
10
    /**
11
     * Test the correct error messages ist returned
12
     * with all pace holder replaced
13
     *
14
     * @test
15
     * @dataProvider invalidDataProvider
16
     */
17
    public function validator_returns_correct_error_message($payload, $rules, $expected)
18
    {
19
        $validator = Validator::make($payload, $rules);
20
21
        $this->assertEquals($expected, $validator->errors()->first());
22
    }
23
24
    /**
25
     * Test each validator returns true for valid data
26
     *
27
     * @test
28
     * @dataProvider validDataProvider
29
     * @param $payload
30
     * @param $rules
31
     */
32
    public function it_validates_correctly($field, $value)
33
    {
34
        $validator = Validator::make([$field => $value], [$field => $field]);
35
36
        $this->assertTrue($validator->passes());
37
    }
38
39
    /**
40
     * Test each validator with a reference value returns true for valid data
41
     *
42
     * @test
43
     * @dataProvider validDataWithReferencesProvider
44
     * @param $payload
45
     * @param $rules
46
     */
47
    public function it_validates_correctly_with_references($field, $value, $referenceField='', $referenceValue='')
48
    {
49
        $payload = [
50
            $field => $value,
51
            $referenceField => $referenceValue
52
        ];
53
54
        $rules = [
55
            $field => "{$field}:{$referenceField}"
56
        ];
57
58
        $validator = Validator::make($payload, $rules);
59
60
        $this->assertTrue($validator->passes());
61
    }
62
63
    /**
64
     * DataProvider for simple rules
65
     *
66
     * @return array
67
     */
68
    public function validDataProvider()
69
    {
70
        return include_once(__DIR__.'/fixtures/valid.php');
71
    }
72
73
    /**
74
     * DataProvider for failing tests
75
     *
76
     * @return array
77
     */
78
    public function invalidDataProvider()
79
    {
80
        return include_once(__DIR__.'/fixtures/invalid.php');
81
    }
82
83
    /**
84
     * DataProvider for rules with references
85
     *
86
     * @return array
87
     */
88
    public function validDataWithReferencesProvider()
89
    {
90
        return include_once(__DIR__.'/fixtures/valid_with_references.php');
91
    }
92
93
    /**
94
     * DataProvider for arrays with dot notation
95
     *
96
     * @return array
97
     */
98
    public function validDataWithDotNotationProvider()
99
    {
100
        return include_once(__DIR__.'/fixtures/valid_wit_dot_notation.php');
101
    }
102
103
    /**
104
     * DataProvider for arrays with dot notation
105
     *
106
     * @return array
107
     */
108
    public function invalidDataWithDotNotationProvider()
109
    {
110
        return include_once(__DIR__.'/fixtures/invalid_wit_dot_notation.php');
111
    }
112
113
    /**
114
     * Test validator can work on arrays with dot notation
115
     * and returns true for valid data
116
     *
117
     * @test
118
     * @dataProvider validDataWithDotNotationProvider
119
     */
120
    public function it_validates_correctly_with_dot_notation($data, $rule)
121
    {
122
        $payload = [
123
            'data' => $data
124
        ];
125
126
        $validator = Validator::make($payload, $rule);
127
128
        $this->assertTrue($validator->passes());
129
        $this->assertEmpty($validator->errors()->all());
130
    }
131
132
    /**
133
     * Test the correct error messages ist returned
134
     * on arrays with dot notation
135
     *
136
     * @test
137
     * @dataProvider invalidDataWithDotNotationProvider
138
     */
139
    public function validator_returns_correct_error_message_with_dot_notation($data, $rule, $error_message)
140
    {
141
        $payload = [
142
            'data' => $data
143
        ];
144
145
        $validator = Validator::make($payload, $rule);
146
147
        $this->assertTrue($validator->fails());
148
        $this->assertCount(2, $validator->errors()->all());
149
        $this->assertEquals($error_message[0], $validator->errors()->first());
150
    }
151
}
152