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