Open Monograph Press  3.3.0
SchemaValidatorTest.php
1 <?php
2 
4 
7 
12 {
14  protected $validator;
15 
16  public function setUp()
17  {
18  $this->validator = new SchemaValidator();
19  }
20 
22  {
23  $value = array(array(1));
24  $this->assertFalse($this->validator->validate($this->getComplexParam(), $value));
25  $this->assertEquals(
26  array('[Foo][0] must be an array of properties. Got a numerically indexed array.'),
27  $this->validator->getErrors()
28  );
29  }
30 
32  {
33  $value = array(true);
34  $this->assertFalse($this->validator->validate($this->getComplexParam(), $value));
35  $this->assertEquals(
36  array('[Foo][0] must be of type object'),
37  $this->validator->getErrors()
38  );
39  }
40 
41  public function testAddsDefaultValuesInLists()
42  {
43  $value = array(array());
44  $this->assertTrue($this->validator->validate($this->getComplexParam(), $value));
45  $this->assertEquals(array(array('Bar' => true)), $value);
46  }
47 
48  public function testMergesDefaultValuesInLists()
49  {
50  $value = array(
51  array('Baz' => 'hello!'),
52  array('Bar' => false)
53  );
54  $this->assertTrue($this->validator->validate($this->getComplexParam(), $value));
55  $this->assertEquals(array(
56  array(
57  'Baz' => 'hello!',
58  'Bar' => true
59  ),
60  array('Bar' => false)
61  ), $value);
62  }
63 
65  {
66  $param = $this->getComplexParam();
67  $result = $param->toArray();
68  $this->assertInternalType('array', $result['items']);
69  $this->assertEquals('array', $result['type']);
70  $this->assertInstanceOf('Guzzle\Service\Description\Parameter', $param->getItems());
71  }
72 
73  public function testAllowsInstanceOf()
74  {
75  $p = new Parameter(array(
76  'name' => 'foo',
77  'type' => 'object',
78  'instanceOf' => get_class($this)
79  ));
80  $this->assertTrue($this->validator->validate($p, $this));
81  $this->assertFalse($this->validator->validate($p, $p));
82  $this->assertEquals(array('[foo] must be an instance of ' . __CLASS__), $this->validator->getErrors());
83  }
84 
85  public function testEnforcesInstanceOfOnlyWhenObject()
86  {
87  $p = new Parameter(array(
88  'name' => 'foo',
89  'type' => array('object', 'string'),
90  'instanceOf' => get_class($this)
91  ));
92  $this->assertTrue($this->validator->validate($p, $this));
93  $s = 'test';
94  $this->assertTrue($this->validator->validate($p, $s));
95  }
96 
98  {
99  $o = $this->getMockBuilder('Guzzle\Common\ToArrayInterface')
100  ->setMethods(array('toArray'))
101  ->getMockForAbstractClass();
102  $o->expects($this->once())
103  ->method('toArray')
104  ->will($this->returnValue(array(
105  'foo' => 'bar'
106  )));
107  $p = new Parameter(array(
108  'name' => 'test',
109  'type' => 'object',
110  'properties' => array(
111  'foo' => array('required' => 'true')
112  )
113  ));
114  $this->assertTrue($this->validator->validate($p, $o));
115  }
116 
118  {
119  $p = new Parameter(array(
120  'name' => 'foo',
121  'type' => 'object',
122  'properties' => array(
123  'bar' => array('type' => 'string', 'required' => true, 'description' => 'This is what it does'),
124  'test' => array('type' => 'string', 'minLength' => 2, 'maxLength' => 5),
125  'test2' => array('type' => 'string', 'minLength' => 2, 'maxLength' => 2),
126  'test3' => array('type' => 'integer', 'minimum' => 100),
127  'test4' => array('type' => 'integer', 'maximum' => 10),
128  'test5' => array('type' => 'array', 'maxItems' => 2),
129  'test6' => array('type' => 'string', 'enum' => array('a', 'bc')),
130  'test7' => array('type' => 'string', 'pattern' => '/[0-9]+/'),
131  'test8' => array('type' => 'number'),
132  'baz' => array(
133  'type' => 'array',
134  'minItems' => 2,
135  'required' => true,
136  "items" => array("type" => "string")
137  )
138  )
139  ));
140 
141  $value = array(
142  'test' => 'a',
143  'test2' => 'abc',
144  'baz' => array(false),
145  'test3' => 10,
146  'test4' => 100,
147  'test5' => array(1, 3, 4),
148  'test6' => 'Foo',
149  'test7' => 'abc',
150  'test8' => 'abc'
151  );
152 
153  $this->assertFalse($this->validator->validate($p, $value));
154  $this->assertEquals(array (
155  '[foo][bar] is a required string: This is what it does',
156  '[foo][baz] must contain 2 or more elements',
157  '[foo][baz][0] must be of type string',
158  '[foo][test2] length must be less than or equal to 2',
159  '[foo][test3] must be greater than or equal to 100',
160  '[foo][test4] must be less than or equal to 10',
161  '[foo][test5] must contain 2 or fewer elements',
162  '[foo][test6] must be one of "a" or "bc"',
163  '[foo][test7] must match the following regular expression: /[0-9]+/',
164  '[foo][test8] must be of type number',
165  '[foo][test] length must be greater than or equal to 2',
166  ), $this->validator->getErrors());
167  }
168 
170  {
171  $p = new Parameter(array(
172  'name' => 'foo',
173  'type' => 'object',
174  'required' => true,
175  'properties' => array(
176  'bar' => array(
177  'type' => 'object',
178  'required' => true,
179  'properties' => array(
180  'foo' => array('default' => 'hi')
181  )
182  )
183  )
184  ));
185  $value = array();
186  $this->assertTrue($this->validator->validate($p, $value));
187  $this->assertEquals(array('bar' => array('foo' => 'hi')), $value);
188  }
189 
191  {
192  $p = new Parameter(array(
193  'name' => 'foo',
194  'type' => 'object',
195  'required' => true,
196  'properties' => array(
197  'bar' => array(
198  'type' => 'object',
199  'required' => true,
200  'properties' => array('foo' => array('type' => 'string'))
201  )
202  )
203  ));
204  $value = array();
205  $this->assertFalse($this->validator->validate($p, $value));
206  $this->assertEquals(array('[foo][bar] is a required object'), $this->validator->getErrors());
207  }
208 
209  public function testChecksTypes()
210  {
211  $p = new SchemaValidator();
212  $r = new \ReflectionMethod($p, 'determineType');
213  $r->setAccessible(true);
214  $this->assertEquals('any', $r->invoke($p, 'any', 'hello'));
215  $this->assertEquals(false, $r->invoke($p, 'foo', 'foo'));
216  $this->assertEquals('string', $r->invoke($p, 'string', 'hello'));
217  $this->assertEquals(false, $r->invoke($p, 'string', false));
218  $this->assertEquals('integer', $r->invoke($p, 'integer', 1));
219  $this->assertEquals(false, $r->invoke($p, 'integer', 'abc'));
220  $this->assertEquals('numeric', $r->invoke($p, 'numeric', 1));
221  $this->assertEquals('numeric', $r->invoke($p, 'numeric', '1'));
222  $this->assertEquals('number', $r->invoke($p, 'number', 1));
223  $this->assertEquals('number', $r->invoke($p, 'number', '1'));
224  $this->assertEquals(false, $r->invoke($p, 'numeric', 'a'));
225  $this->assertEquals('boolean', $r->invoke($p, 'boolean', true));
226  $this->assertEquals('boolean', $r->invoke($p, 'boolean', false));
227  $this->assertEquals(false, $r->invoke($p, 'boolean', 'false'));
228  $this->assertEquals('null', $r->invoke($p, 'null', null));
229  $this->assertEquals(false, $r->invoke($p, 'null', 'abc'));
230  $this->assertEquals('array', $r->invoke($p, 'array', array()));
231  $this->assertEquals(false, $r->invoke($p, 'array', 'foo'));
232  }
233 
235  {
236  $param = new Parameter(array(
237  'name' => 'foo',
238  'type' => 'object',
239  'properties' => array('bar' => array('type' => 'string')),
240  'additionalProperties' => false
241  ));
242  $value = array('test' => '123');
243  $this->assertFalse($this->validator->validate($param, $value));
244  $this->assertEquals(array('[foo][test] is not an allowed property'), $this->validator->getErrors());
245  $value = array('bar' => '123');
246  $this->assertTrue($this->validator->validate($param, $value));
247  }
248 
250  {
251  $param = new Parameter(array(
252  'name' => 'foo',
253  'type' => 'object',
254  'properties' => array('bar' => array('type' => 'string'))
255  ));
256  $value = array('test' => '123');
257  $this->assertTrue($this->validator->validate($param, $value));
258  }
259 
260  public function testValidatesAdditionalProperties()
261  {
262  $param = new Parameter(array(
263  'name' => 'foo',
264  'type' => 'object',
265  'properties' => array('bar' => array('type' => 'string')),
266  'additionalProperties' => array('type' => 'integer')
267  ));
268  $value = array('test' => 'foo');
269  $this->assertFalse($this->validator->validate($param, $value));
270  $this->assertEquals(array('[foo][test] must be of type integer'), $this->validator->getErrors());
271  }
272 
274  {
275  $param = new Parameter(array(
276  'name' => 'foo',
277  'type' => 'object',
278  'additionalProperties' => array(
279  'type' => 'array',
280  'items' => array('type' => 'string')
281  )
282  ));
283  $value = array('test' => array(true));
284  $this->assertFalse($this->validator->validate($param, $value));
285  $this->assertEquals(array('[foo][test][0] must be of type string'), $this->validator->getErrors());
286  }
287 
289  {
290  $param = new Parameter(array('name' => 'test', 'type' => 'string'));
291  $value = 12;
292  $this->assertTrue($this->validator->validate($param, $value));
293  $this->assertEquals('12', $value);
294  }
295 
296  public function testRequiredMessageIncludesType()
297  {
298  $param = new Parameter(array('name' => 'test', 'type' => array('string', 'boolean'), 'required' => true));
299  $value = null;
300  $this->assertFalse($this->validator->validate($param, $value));
301  $this->assertEquals(array('[test] is a required string or boolean'), $this->validator->getErrors());
302  }
303 
304  protected function getComplexParam()
305  {
306  return new Parameter(array(
307  'name' => 'Foo',
308  'type' => 'array',
309  'required' => true,
310  'min' => 1,
311  'items' => array(
312  'type' => 'object',
313  'properties' => array(
314  'Baz' => array(
315  'type' => 'string',
316  ),
317  'Bar' => array(
318  'required' => true,
319  'type' => 'boolean',
320  'default' => true
321  )
322  )
323  )
324  ));
325  }
326 }
Guzzle\Tests\Service\Description\SchemaValidatorTest\testFailsWhenNullValuesInArraysWithNoDefaults
testFailsWhenNullValuesInArraysWithNoDefaults()
Definition: SchemaValidatorTest.php:193
Guzzle\Tests\Service\Description\SchemaValidatorTest\testConvertsObjectsToArraysWhenToArrayInterface
testConvertsObjectsToArraysWhenToArrayInterface()
Definition: SchemaValidatorTest.php:100
Guzzle\Tests\Service\Description\SchemaValidatorTest
Definition: SchemaValidatorTest.php:11
Guzzle\Tests\Service\Description\SchemaValidatorTest\testValidatesAdditionalProperties
testValidatesAdditionalProperties()
Definition: SchemaValidatorTest.php:263
Guzzle\Tests\GuzzleTestCase
Definition: GuzzleTestCase.php:22
Guzzle\Tests\Service\Description\SchemaValidatorTest\testAllowsInstanceOf
testAllowsInstanceOf()
Definition: SchemaValidatorTest.php:76
Guzzle\Tests\Service\Description\SchemaValidatorTest\setUp
setUp()
Definition: SchemaValidatorTest.php:19
Guzzle\Tests\Service\Description\SchemaValidatorTest\testValidatesArrayListsAreNumericallyIndexed
testValidatesArrayListsAreNumericallyIndexed()
Definition: SchemaValidatorTest.php:24
Guzzle\Service\Description\Parameter
Definition: Parameter.php:10
Guzzle\Tests\Service\Description\SchemaValidatorTest\testHandlesNullValuesInArraysWithDefaults
testHandlesNullValuesInArraysWithDefaults()
Definition: SchemaValidatorTest.php:172
Guzzle\Tests\Service\Description\SchemaValidatorTest\getComplexParam
getComplexParam()
Definition: SchemaValidatorTest.php:307
Guzzle\Tests\Service\Description\SchemaValidatorTest\testAllowsUndefinedAdditionalProperties
testAllowsUndefinedAdditionalProperties()
Definition: SchemaValidatorTest.php:252
Guzzle\Tests\Service\Description
Definition: OperationTest.php:3
Guzzle\Tests\Service\Description\SchemaValidatorTest\$validator
$validator
Definition: SchemaValidatorTest.php:17
Guzzle\Tests\Service\Description\SchemaValidatorTest\testMergesValidationErrorsInPropertiesWithParent
testMergesValidationErrorsInPropertiesWithParent()
Definition: SchemaValidatorTest.php:120
Guzzle\Tests\Service\Description\SchemaValidatorTest\testIntegersCastToStringWhenTypeMismatch
testIntegersCastToStringWhenTypeMismatch()
Definition: SchemaValidatorTest.php:291
Guzzle\Tests\Service\Description\SchemaValidatorTest\testValidatesArrayListsContainProperItems
testValidatesArrayListsContainProperItems()
Definition: SchemaValidatorTest.php:34
Guzzle\Tests\Service\Description\SchemaValidatorTest\testValidatesAdditionalPropertiesThatArrayArrays
testValidatesAdditionalPropertiesThatArrayArrays()
Definition: SchemaValidatorTest.php:276
Guzzle\Service\Description\SchemaValidator
Definition: SchemaValidator.php:10
Guzzle\Tests\Service\Description\SchemaValidatorTest\testEnforcesInstanceOfOnlyWhenObject
testEnforcesInstanceOfOnlyWhenObject()
Definition: SchemaValidatorTest.php:88
Guzzle\Tests\Service\Description\SchemaValidatorTest\testCorrectlyConvertsParametersToArrayWhenArraysArePresent
testCorrectlyConvertsParametersToArrayWhenArraysArePresent()
Definition: SchemaValidatorTest.php:67
Guzzle\Tests\Service\Description\SchemaValidatorTest\testRequiredMessageIncludesType
testRequiredMessageIncludesType()
Definition: SchemaValidatorTest.php:299
Guzzle\Tests\Service\Description\SchemaValidatorTest\testChecksTypes
testChecksTypes()
Definition: SchemaValidatorTest.php:212
Guzzle\Tests\Service\Description\SchemaValidatorTest\testAddsDefaultValuesInLists
testAddsDefaultValuesInLists()
Definition: SchemaValidatorTest.php:44
Guzzle\Tests\Service\Description\SchemaValidatorTest\testValidatesFalseAdditionalProperties
testValidatesFalseAdditionalProperties()
Definition: SchemaValidatorTest.php:237
Guzzle\Tests\Service\Description\SchemaValidatorTest\testMergesDefaultValuesInLists
testMergesDefaultValuesInLists()
Definition: SchemaValidatorTest.php:51