1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\ConfigValidation\Test\Unit\Rules; |
4
|
|
|
|
5
|
|
|
use Graze\ConfigValidation\Exceptions\AttributeSetException; |
6
|
|
|
use Graze\ConfigValidation\Rules\AttributeSet; |
7
|
|
|
use PHPUnit_Framework_TestCase as TestCase; |
8
|
|
|
use Respect\Validation\Rules\AllOf; |
9
|
|
|
use Respect\Validation\Rules\AlwaysValid; |
10
|
|
|
use Respect\Validation\Rules\Attribute; |
11
|
|
|
|
12
|
|
|
class AttributeSetTest extends TestCase |
13
|
|
|
{ |
14
|
|
View Code Duplication |
public function testShouldAcceptAttributeRule() |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
$attribute = new Attribute('foo', new AlwaysValid(), false); |
17
|
|
|
$attributeSet = new AttributeSet($attribute); |
18
|
|
|
|
19
|
|
|
$rules = $attributeSet->getRules(); |
20
|
|
|
|
21
|
|
|
$this->assertSame(current($rules), $attribute); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
public function testShouldAcceptAllOfWithOneAttributeRule() |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
$attribute = new Attribute('foo', new AlwaysValid(), false); |
27
|
|
|
$allOf = new AllOf($attribute); |
28
|
|
|
$attributeSet = new AttributeSet($allOf); |
29
|
|
|
|
30
|
|
|
$rules = $attributeSet->getRules(); |
31
|
|
|
|
32
|
|
|
$this->assertSame(current($rules), $attribute); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @expectedException \Respect\Validation\Exceptions\ComponentException |
37
|
|
|
* @expectedExceptionMessage AllOf rule must have only one Attribute rule |
38
|
|
|
*/ |
39
|
|
|
public function testShouldNotAcceptAllOfWithMoreThanOneAttributeRule() |
40
|
|
|
{ |
41
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), false); |
42
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), false); |
43
|
|
|
$allOf = new AllOf($attribute1, $attribute2); |
44
|
|
|
|
45
|
|
|
new AttributeSet($allOf); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @expectedException \Respect\Validation\Exceptions\ComponentException |
50
|
|
|
* @expectedExceptionMessage AttributeSet rule accepts only Attribute rules |
51
|
|
|
*/ |
52
|
|
|
public function testShouldNotAcceptAllOfWithANonAttributeRule() |
53
|
|
|
{ |
54
|
|
|
$alwaysValid = new AlwaysValid(); |
55
|
|
|
$allOf = new AllOf($alwaysValid); |
56
|
|
|
|
57
|
|
|
new AttributeSet($allOf); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @expectedException \Respect\Validation\Exceptions\ComponentException |
62
|
|
|
* @expectedExceptionMessage AttributeSet rule accepts only Attribute rules |
63
|
|
|
*/ |
64
|
|
|
public function testShouldNotAcceptANonAttributeRule() |
65
|
|
|
{ |
66
|
|
|
$alwaysValid = new AlwaysValid(); |
67
|
|
|
|
68
|
|
|
new AttributeSet($alwaysValid); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testShouldReturnAttributes() |
72
|
|
|
{ |
73
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), true); |
74
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), false); |
75
|
|
|
|
76
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
77
|
|
|
|
78
|
|
|
$this->assertEquals(['foo', 'bar'], $attributeSet->getAttributes()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
View Code Duplication |
public function testShouldValidateAttributesWhenThereAreMissingRequiredAttributes() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$input = (object)[ |
84
|
|
|
'foo' => 42, |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), true); |
88
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), true); |
89
|
|
|
|
90
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
91
|
|
|
|
92
|
|
|
$this->assertFalse($attributeSet->validate($input)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
View Code Duplication |
public function testShouldValidateAttributesWhenThereAreMissingNonRequiredAttributes() |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$input = (object)[ |
98
|
|
|
'foo' => 42, |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), true); |
102
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), false); |
103
|
|
|
|
104
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
105
|
|
|
|
106
|
|
|
$this->assertTrue($attributeSet->validate($input)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testShouldValidateAttributesWhenThereAreMoreAttributes() |
110
|
|
|
{ |
111
|
|
|
$input = (object)[ |
112
|
|
|
'foo' => 42, |
113
|
|
|
'bar' => 'String', |
114
|
|
|
'baz' => false, |
115
|
|
|
]; |
116
|
|
|
|
117
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), false); |
118
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), false); |
119
|
|
|
|
120
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
121
|
|
|
|
122
|
|
|
$this->assertFalse($attributeSet->validate($input)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
View Code Duplication |
public function testShouldValidateAttributesWhenEmpty() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$input = (object)[]; |
128
|
|
|
|
129
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), true); |
130
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), true); |
131
|
|
|
|
132
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
133
|
|
|
|
134
|
|
|
$this->assertFalse($attributeSet->validate($input)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @expectedException \Respect\Validation\Exceptions\AttributeException |
139
|
|
|
* @expectedExceptionMessage Attribute foo must be present |
140
|
|
|
*/ |
141
|
|
View Code Duplication |
public function testShouldCheckAttributesAndUseChildValidators() |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$input = (object)[]; |
144
|
|
|
|
145
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), true); |
146
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), true); |
147
|
|
|
|
148
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
149
|
|
|
$attributeSet->check($input); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testShouldAssertGetAttributesInException() |
153
|
|
|
{ |
154
|
|
|
$input = (object)[ |
155
|
|
|
'foo' => 42, |
156
|
|
|
'bar' => 'String', |
157
|
|
|
'baz' => false, |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), false); |
161
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), false); |
162
|
|
|
|
163
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
164
|
|
|
|
165
|
|
|
$raised = false; |
166
|
|
|
try { |
167
|
|
|
$attributeSet->assert($input); |
168
|
|
|
} catch (AttributeSetException $e) { |
169
|
|
|
$this->assertEquals( |
170
|
|
|
<<<ERR |
171
|
|
|
- Must not have unknown attributes { "baz" } |
172
|
|
|
ERR |
173
|
|
|
, |
|
|
|
|
174
|
|
|
$e->getFullMessage() |
175
|
|
|
); |
176
|
|
|
$raised = true; |
177
|
|
|
} |
178
|
|
|
$this->assertTrue($raised); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testShouldAssertGetRequiredMissingInException() |
182
|
|
|
{ |
183
|
|
|
$input = (object)[]; |
184
|
|
|
|
185
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), true); |
186
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), false); |
187
|
|
|
|
188
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
189
|
|
|
|
190
|
|
|
$raised = false; |
191
|
|
|
try { |
192
|
|
|
$attributeSet->assert($input); |
193
|
|
|
} catch (AttributeSetException $e) { |
194
|
|
|
$this->assertEquals( |
195
|
|
|
<<<ERR |
196
|
|
|
- Attribute foo must be present |
197
|
|
|
ERR |
198
|
|
|
, |
|
|
|
|
199
|
|
|
$e->getFullMessage() |
200
|
|
|
); |
201
|
|
|
$raised = true; |
202
|
|
|
} |
203
|
|
|
$this->assertTrue($raised); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testShouldAcceptArrayOfAttributes() |
207
|
|
|
{ |
208
|
|
|
$input = (object)[ |
209
|
|
|
'foo' => 42, |
210
|
|
|
]; |
211
|
|
|
|
212
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), true); |
213
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), false); |
214
|
|
|
|
215
|
|
|
$attributeSet = new AttributeSet([[$attribute1, $attribute2]]); |
216
|
|
|
|
217
|
|
|
$this->assertTrue($attributeSet->validate($input)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
View Code Duplication |
public function testShouldByAbleToAssert() |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
$input = (object)[ |
223
|
|
|
'foo' => 42, |
224
|
|
|
]; |
225
|
|
|
|
226
|
|
|
$attribute1 = new Attribute('foo', new AlwaysValid(), true); |
227
|
|
|
$attribute2 = new Attribute('bar', new AlwaysValid(), false); |
228
|
|
|
|
229
|
|
|
$attributeSet = new AttributeSet($attribute1, $attribute2); |
230
|
|
|
|
231
|
|
|
$attributeSet->assert($input); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
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.