SchemaValidatorTest.php 12 KB

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