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: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327:
<?php
namespace Guzzle\Tests\Service\Description;
use Guzzle\Service\Description\Parameter;
use Guzzle\Service\Description\SchemaValidator;
class SchemaValidatorTest extends \Guzzle\Tests\GuzzleTestCase
{
protected $validator;
public function setUp()
{
$this->validator = new SchemaValidator();
}
public function testValidatesArrayListsAreNumericallyIndexed()
{
$value = array(array(1));
$this->assertFalse($this->validator->validate($this->getComplexParam(), $value));
$this->assertEquals(
array('[Foo][0] must be an array of properties. Got a numerically indexed array.'),
$this->validator->getErrors()
);
}
public function testValidatesArrayListsContainProperItems()
{
$value = array(true);
$this->assertFalse($this->validator->validate($this->getComplexParam(), $value));
$this->assertEquals(
array('[Foo][0] must be of type object'),
$this->validator->getErrors()
);
}
public function testAddsDefaultValuesInLists()
{
$value = array(array());
$this->assertTrue($this->validator->validate($this->getComplexParam(), $value));
$this->assertEquals(array(array('Bar' => true)), $value);
}
public function testMergesDefaultValuesInLists()
{
$value = array(
array('Baz' => 'hello!'),
array('Bar' => false)
);
$this->assertTrue($this->validator->validate($this->getComplexParam(), $value));
$this->assertEquals(array(
array(
'Baz' => 'hello!',
'Bar' => true
),
array('Bar' => false)
), $value);
}
public function testCorrectlyConvertsParametersToArrayWhenArraysArePresent()
{
$param = $this->getComplexParam();
$result = $param->toArray();
$this->assertInternalType('array', $result['items']);
$this->assertEquals('array', $result['type']);
$this->assertInstanceOf('Guzzle\Service\Description\Parameter', $param->getItems());
}
public function testAllowsInstanceOf()
{
$p = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'instanceOf' => get_class($this)
));
$this->assertTrue($this->validator->validate($p, $this));
$this->assertFalse($this->validator->validate($p, $p));
$this->assertEquals(array('[foo] must be an instance of ' . __CLASS__), $this->validator->getErrors());
}
public function testEnforcesInstanceOfOnlyWhenObject()
{
$p = new Parameter(array(
'name' => 'foo',
'type' => array('object', 'string'),
'instanceOf' => get_class($this)
));
$this->assertTrue($this->validator->validate($p, $this));
$s = 'test';
$this->assertTrue($this->validator->validate($p, $s));
}
public function testConvertsObjectsToArraysWhenToArrayInterface()
{
$o = $this->getMockBuilder('Guzzle\Common\ToArrayInterface')
->setMethods(array('toArray'))
->getMockForAbstractClass();
$o->expects($this->once())
->method('toArray')
->will($this->returnValue(array(
'foo' => 'bar'
)));
$p = new Parameter(array(
'name' => 'test',
'type' => 'object',
'properties' => array(
'foo' => array('required' => 'true')
)
));
$this->assertTrue($this->validator->validate($p, $o));
}
public function testMergesValidationErrorsInPropertiesWithParent()
{
$p = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'properties' => array(
'bar' => array('type' => 'string', 'required' => true, 'description' => 'This is what it does'),
'test' => array('type' => 'string', 'minLength' => 2, 'maxLength' => 5),
'test2' => array('type' => 'string', 'minLength' => 2, 'maxLength' => 2),
'test3' => array('type' => 'integer', 'minimum' => 100),
'test4' => array('type' => 'integer', 'maximum' => 10),
'test5' => array('type' => 'array', 'maxItems' => 2),
'test6' => array('type' => 'string', 'enum' => array('a', 'bc')),
'test7' => array('type' => 'string', 'pattern' => '/[0-9]+/'),
'test8' => array('type' => 'number'),
'baz' => array(
'type' => 'array',
'minItems' => 2,
'required' => true,
"items" => array("type" => "string")
)
)
));
$value = array(
'test' => 'a',
'test2' => 'abc',
'baz' => array(false),
'test3' => 10,
'test4' => 100,
'test5' => array(1, 3, 4),
'test6' => 'Foo',
'test7' => 'abc',
'test8' => 'abc'
);
$this->assertFalse($this->validator->validate($p, $value));
$this->assertEquals(array (
'[foo][bar] is a required string: This is what it does',
'[foo][baz] must contain 2 or more elements',
'[foo][baz][0] must be of type string',
'[foo][test2] length must be less than or equal to 2',
'[foo][test3] must be greater than or equal to 100',
'[foo][test4] must be less than or equal to 10',
'[foo][test5] must contain 2 or fewer elements',
'[foo][test6] must be one of "a" or "bc"',
'[foo][test7] must match the following regular expression: /[0-9]+/',
'[foo][test8] must be of type number',
'[foo][test] length must be greater than or equal to 2',
), $this->validator->getErrors());
}
public function testHandlesNullValuesInArraysWithDefaults()
{
$p = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'required' => true,
'properties' => array(
'bar' => array(
'type' => 'object',
'required' => true,
'properties' => array(
'foo' => array('default' => 'hi')
)
)
)
));
$value = array();
$this->assertTrue($this->validator->validate($p, $value));
$this->assertEquals(array('bar' => array('foo' => 'hi')), $value);
}
public function testFailsWhenNullValuesInArraysWithNoDefaults()
{
$p = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'required' => true,
'properties' => array(
'bar' => array(
'type' => 'object',
'required' => true,
'properties' => array('foo' => array('type' => 'string'))
)
)
));
$value = array();
$this->assertFalse($this->validator->validate($p, $value));
$this->assertEquals(array('[foo][bar] is a required object'), $this->validator->getErrors());
}
public function testChecksTypes()
{
$p = new SchemaValidator();
$r = new \ReflectionMethod($p, 'determineType');
$r->setAccessible(true);
$this->assertEquals('any', $r->invoke($p, 'any', 'hello'));
$this->assertEquals(false, $r->invoke($p, 'foo', 'foo'));
$this->assertEquals('string', $r->invoke($p, 'string', 'hello'));
$this->assertEquals(false, $r->invoke($p, 'string', false));
$this->assertEquals('integer', $r->invoke($p, 'integer', 1));
$this->assertEquals(false, $r->invoke($p, 'integer', 'abc'));
$this->assertEquals('numeric', $r->invoke($p, 'numeric', 1));
$this->assertEquals('numeric', $r->invoke($p, 'numeric', '1'));
$this->assertEquals('number', $r->invoke($p, 'number', 1));
$this->assertEquals('number', $r->invoke($p, 'number', '1'));
$this->assertEquals(false, $r->invoke($p, 'numeric', 'a'));
$this->assertEquals('boolean', $r->invoke($p, 'boolean', true));
$this->assertEquals('boolean', $r->invoke($p, 'boolean', false));
$this->assertEquals(false, $r->invoke($p, 'boolean', 'false'));
$this->assertEquals('null', $r->invoke($p, 'null', null));
$this->assertEquals(false, $r->invoke($p, 'null', 'abc'));
$this->assertEquals('array', $r->invoke($p, 'array', array()));
$this->assertEquals(false, $r->invoke($p, 'array', 'foo'));
}
public function testValidatesFalseAdditionalProperties()
{
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'properties' => array('bar' => array('type' => 'string')),
'additionalProperties' => false
));
$value = array('test' => '123');
$this->assertFalse($this->validator->validate($param, $value));
$this->assertEquals(array('[foo][test] is not an allowed property'), $this->validator->getErrors());
$value = array('bar' => '123');
$this->assertTrue($this->validator->validate($param, $value));
}
public function testAllowsUndefinedAdditionalProperties()
{
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'properties' => array('bar' => array('type' => 'string'))
));
$value = array('test' => '123');
$this->assertTrue($this->validator->validate($param, $value));
}
public function testValidatesAdditionalProperties()
{
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'properties' => array('bar' => array('type' => 'string')),
'additionalProperties' => array('type' => 'integer')
));
$value = array('test' => 'foo');
$this->assertFalse($this->validator->validate($param, $value));
$this->assertEquals(array('[foo][test] must be of type integer'), $this->validator->getErrors());
}
public function testValidatesAdditionalPropertiesThatArrayArrays()
{
$param = new Parameter(array(
'name' => 'foo',
'type' => 'object',
'additionalProperties' => array(
'type' => 'array',
'items' => array('type' => 'string')
)
));
$value = array('test' => array(true));
$this->assertFalse($this->validator->validate($param, $value));
$this->assertEquals(array('[foo][test][0] must be of type string'), $this->validator->getErrors());
}
public function testIntegersCastToStringWhenTypeMismatch()
{
$param = new Parameter(array('name' => 'test', 'type' => 'string'));
$value = 12;
$this->assertTrue($this->validator->validate($param, $value));
$this->assertEquals('12', $value);
}
public function testRequiredMessageIncludesType()
{
$param = new Parameter(array('name' => 'test', 'type' => array('string', 'boolean'), 'required' => true));
$value = null;
$this->assertFalse($this->validator->validate($param, $value));
$this->assertEquals(array('[test] is a required string or boolean'), $this->validator->getErrors());
}
protected function getComplexParam()
{
return new Parameter(array(
'name' => 'Foo',
'type' => 'array',
'required' => true,
'min' => 1,
'items' => array(
'type' => 'object',
'properties' => array(
'Baz' => array(
'type' => 'string',
),
'Bar' => array(
'required' => true,
'type' => 'boolean',
'default' => true
)
)
)
));
}
}