1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
<?php
namespace Engelsystem\Test\Unit\Http\Validation;
use Engelsystem\Http\Validation\Validator;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
class ValidatorTest extends TestCase
{
/**
* @covers \Engelsystem\Http\Validation\Validator::validate
* @covers \Engelsystem\Http\Validation\Validator::getData
* @covers \Engelsystem\Http\Validation\Validator::getErrors
*/
public function testValidate()
{
$val = new Validator();
$this->assertTrue($val->validate(
['foo' => 'bar', 'lorem' => 'on', 'dolor' => 'bla'],
['lorem' => 'accepted']
));
$this->assertEquals(['lorem' => 'on'], $val->getData());
$this->assertFalse($val->validate(
[],
['lorem' => 'required|min:3']
));
$this->assertEquals(
['lorem' => ['validation.lorem.required', 'validation.lorem.min']],
$val->getErrors()
);
}
/**
* @covers \Engelsystem\Http\Validation\Validator::validate
*/
public function testValidateChaining()
{
$val = new Validator();
$this->assertTrue($val->validate(
['lorem' => 10],
['lorem' => 'required|min:3|max:10']
));
$this->assertTrue($val->validate(
['lorem' => 3],
['lorem' => 'required|min:3|max:10']
));
$this->assertFalse($val->validate(
['lorem' => 2],
['lorem' => 'required|min:3|max:10']
));
$this->assertFalse($val->validate(
['lorem' => 42],
['lorem' => 'required|min:3|max:10']
));
}
/**
* @covers \Engelsystem\Http\Validation\Validator::validate
*/
public function testValidateNotImplemented()
{
$val = new Validator();
$this->expectException(InvalidArgumentException::class);
$val->validate(
['lorem' => 'bar'],
['foo' => 'never_implemented']
);
}
/**
* @covers \Engelsystem\Http\Validation\Validator::map
* @covers \Engelsystem\Http\Validation\Validator::mapBack
*/
public function testValidateMapping()
{
$val = new Validator();
$this->assertTrue($val->validate(
['foo' => 'bar'],
['foo' => 'required']
));
$this->assertTrue($val->validate(
['foo' => '0'],
['foo' => 'int']
));
$this->assertTrue($val->validate(
['foo' => 'on'],
['foo' => 'accepted']
));
$this->assertFalse($val->validate(
[],
['lorem' => 'required']
));
$this->assertEquals(
['lorem' => ['validation.lorem.required']],
$val->getErrors()
);
}
/**
* @covers \Engelsystem\Http\Validation\Validator::validate
*/
public function testValidateNesting()
{
$val = new Validator();
$this->assertTrue($val->validate(
[],
['foo' => 'not|required']
));
$this->assertTrue($val->validate(
['foo' => 'foo'],
['foo' => 'not|int']
));
$this->assertFalse($val->validate(
['foo' => 1],
['foo' => 'not|int']
));
$this->assertTrue($val->validate(
[],
['foo' => 'optional|int']
));
$this->assertTrue($val->validate(
['foo' => '33'],
['foo' => 'optional|int']
));
$this->assertFalse($val->validate(
['foo' => 'T'],
['foo' => 'optional|int']
));
}
}
|