ParameterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. namespace Guzzle\Tests\Service\Description;
  3. use GuzzleHttp\Command\Guzzle\Description;
  4. use GuzzleHttp\Command\Guzzle\Parameter;
  5. /**
  6. * @covers \GuzzleHttp\Command\Guzzle\Parameter
  7. */
  8. class ParameterTest extends \PHPUnit_Framework_TestCase
  9. {
  10. protected $data = [
  11. 'name' => 'foo',
  12. 'type' => 'bar',
  13. 'required' => true,
  14. 'default' => '123',
  15. 'description' => '456',
  16. 'minLength' => 2,
  17. 'maxLength' => 5,
  18. 'location' => 'body',
  19. 'static' => true,
  20. 'filters' => ['trim', 'json_encode']
  21. ];
  22. public function testCreatesParamFromArray()
  23. {
  24. $p = new Parameter($this->data);
  25. $this->assertEquals('foo', $p->getName());
  26. $this->assertEquals('bar', $p->getType());
  27. $this->assertTrue($p->isRequired());
  28. $this->assertEquals('123', $p->getDefault());
  29. $this->assertEquals('456', $p->getDescription());
  30. $this->assertEquals(2, $p->getMinLength());
  31. $this->assertEquals(5, $p->getMaxLength());
  32. $this->assertEquals('body', $p->getLocation());
  33. $this->assertTrue($p->isStatic());
  34. $this->assertEquals(['trim', 'json_encode'], $p->getFilters());
  35. $p->setName('abc');
  36. $this->assertEquals('abc', $p->getName());
  37. }
  38. /**
  39. * @expectedException \InvalidArgumentException
  40. */
  41. public function testValidatesDescription()
  42. {
  43. new Parameter($this->data, ['description' => 'foo']);
  44. }
  45. public function testCanConvertToArray()
  46. {
  47. $p = new Parameter($this->data);
  48. $this->assertEquals($this->data, $p->toArray());
  49. }
  50. public function testUsesStatic()
  51. {
  52. $d = $this->data;
  53. $d['default'] = 'booboo';
  54. $d['static'] = true;
  55. $p = new Parameter($d);
  56. $this->assertEquals('booboo', $p->getValue('bar'));
  57. }
  58. public function testUsesDefault()
  59. {
  60. $d = $this->data;
  61. $d['default'] = 'foo';
  62. $d['static'] = null;
  63. $p = new Parameter($d);
  64. $this->assertEquals('foo', $p->getValue(null));
  65. }
  66. public function testReturnsYourValue()
  67. {
  68. $d = $this->data;
  69. $d['static'] = null;
  70. $p = new Parameter($d);
  71. $this->assertEquals('foo', $p->getValue('foo'));
  72. }
  73. public function testZeroValueDoesNotCauseDefaultToBeReturned()
  74. {
  75. $d = $this->data;
  76. $d['default'] = '1';
  77. $d['static'] = null;
  78. $p = new Parameter($d);
  79. $this->assertEquals('0', $p->getValue('0'));
  80. }
  81. public function testFiltersValues()
  82. {
  83. $d = $this->data;
  84. $d['static'] = null;
  85. $d['filters'] = 'strtoupper';
  86. $p = new Parameter($d);
  87. $this->assertEquals('FOO', $p->filter('foo'));
  88. }
  89. /**
  90. * @expectedException \RuntimeException
  91. * @expectedExceptionMessage No service description
  92. */
  93. public function testRequiresServiceDescriptionForFormatting()
  94. {
  95. $d = $this->data;
  96. $d['format'] = 'foo';
  97. $p = new Parameter($d);
  98. $p->filter('bar');
  99. }
  100. public function testConvertsBooleans()
  101. {
  102. $p = new Parameter(['type' => 'boolean']);
  103. $this->assertEquals(true, $p->filter('true'));
  104. $this->assertEquals(false, $p->filter('false'));
  105. }
  106. public function testUsesArrayByDefaultForFilters()
  107. {
  108. $d = $this->data;
  109. $d['filters'] = null;
  110. $p = new Parameter($d);
  111. $this->assertEquals([], $p->getFilters());
  112. }
  113. public function testAllowsSimpleLocationValue()
  114. {
  115. $p = new Parameter(['name' => 'myname', 'location' => 'foo', 'sentAs' => 'Hello']);
  116. $this->assertEquals('foo', $p->getLocation());
  117. $this->assertEquals('Hello', $p->getSentAs());
  118. }
  119. public function testParsesTypeValues()
  120. {
  121. $p = new Parameter(['type' => 'foo']);
  122. $this->assertEquals('foo', $p->getType());
  123. }
  124. /**
  125. * @expectedException \InvalidArgumentException
  126. * @expectedExceptionMessage A [method] value must be specified for each complex filter
  127. */
  128. public function testValidatesComplexFilters()
  129. {
  130. $p = new Parameter(['filters' => [['args' => 'foo']]]);
  131. }
  132. public function testAllowsComplexFilters()
  133. {
  134. $that = $this;
  135. $param = new Parameter([
  136. 'filters' => [
  137. [
  138. 'method' => function ($a, $b, $c, $d) use ($that, &$param) {
  139. $that->assertEquals('test', $a);
  140. $that->assertEquals('my_value!', $b);
  141. $that->assertEquals('bar', $c);
  142. $that->assertSame($param, $d);
  143. return 'abc' . $b;
  144. },
  145. 'args' => ['test', '@value', 'bar', '@api']
  146. ]
  147. ]
  148. ]);
  149. $this->assertEquals('abcmy_value!', $param->filter('my_value!'));
  150. }
  151. public function testAddsAdditionalProperties()
  152. {
  153. $p = new Parameter([
  154. 'type' => 'object',
  155. 'additionalProperties' => ['type' => 'string']
  156. ]);
  157. $this->assertInstanceOf('GuzzleHttp\Command\Guzzle\Parameter', $p->getAdditionalProperties());
  158. $this->assertNull($p->getAdditionalProperties()->getAdditionalProperties());
  159. $p = new Parameter(['type' => 'object']);
  160. $this->assertTrue($p->getAdditionalProperties());
  161. }
  162. public function testAddsItems()
  163. {
  164. $p = new Parameter([
  165. 'type' => 'array',
  166. 'items' => ['type' => 'string']
  167. ]);
  168. $this->assertInstanceOf('GuzzleHttp\Command\Guzzle\Parameter', $p->getItems());
  169. $out = $p->toArray();
  170. $this->assertEquals('array', $out['type']);
  171. $this->assertInternalType('array', $out['items']);
  172. }
  173. public function testCanRetrieveKnownPropertiesUsingDataMethod()
  174. {
  175. $p = new Parameter(['data' => ['name' => 'test'], 'extra' => 'hi!']);
  176. $this->assertEquals('test', $p->getData('name'));
  177. $this->assertEquals(['name' => 'test'], $p->getData());
  178. $this->assertNull($p->getData('fjnweefe'));
  179. $this->assertEquals('hi!', $p->getData('extra'));
  180. }
  181. public function testHasPattern()
  182. {
  183. $p = new Parameter(['pattern' => '/[0-9]+/']);
  184. $this->assertEquals('/[0-9]+/', $p->getPattern());
  185. }
  186. public function testHasEnum()
  187. {
  188. $p = new Parameter(['enum' => ['foo', 'bar']]);
  189. $this->assertEquals(['foo', 'bar'], $p->getEnum());
  190. }
  191. public function testSerializesItems()
  192. {
  193. $p = new Parameter([
  194. 'type' => 'object',
  195. 'additionalProperties' => ['type' => 'string']
  196. ]);
  197. $this->assertEquals([
  198. 'type' => 'object',
  199. 'additionalProperties' => ['type' => 'string']
  200. ], $p->toArray());
  201. }
  202. public function testResolvesRefKeysRecursively()
  203. {
  204. $description = new Description([
  205. 'models' => [
  206. 'JarJar' => ['type' => 'string', 'default' => 'Mesa address tha senate!'],
  207. 'Anakin' => ['type' => 'array', 'items' => ['$ref' => 'JarJar']]
  208. ],
  209. ]);
  210. $p = new Parameter(['$ref' => 'Anakin', 'description' => 'added'], ['description' => $description]);
  211. $this->assertEquals([
  212. 'description' => 'added',
  213. '$ref' => 'Anakin'
  214. ], $p->toArray());
  215. }
  216. public function testResolvesExtendsRecursively()
  217. {
  218. $jarJar = ['type' => 'string', 'default' => 'Mesa address tha senate!', 'description' => 'a'];
  219. $anakin = ['type' => 'array', 'items' => ['extends' => 'JarJar', 'description' => 'b']];
  220. $description = new Description([
  221. 'models' => ['JarJar' => $jarJar, 'Anakin' => $anakin]
  222. ]);
  223. // Description attribute will be updated, and format added
  224. $p = new Parameter(['extends' => 'Anakin', 'format' => 'date'], ['description' => $description]);
  225. $this->assertEquals([
  226. 'format' => 'date',
  227. 'extends' => 'Anakin'
  228. ], $p->toArray());
  229. }
  230. public function testHasKeyMethod()
  231. {
  232. $p = new Parameter(['name' => 'foo', 'sentAs' => 'bar']);
  233. $this->assertEquals('bar', $p->getWireName());
  234. }
  235. public function testIncludesNameInToArrayWhenItemsAttributeHasName()
  236. {
  237. $p = new Parameter([
  238. 'type' => 'array',
  239. 'name' => 'Abc',
  240. 'items' => [
  241. 'name' => 'Foo',
  242. 'type' => 'object'
  243. ]
  244. ]);
  245. $result = $p->toArray();
  246. $this->assertEquals([
  247. 'type' => 'array',
  248. 'name' => 'Abc',
  249. 'items' => [
  250. 'name' => 'Foo',
  251. 'type' => 'object'
  252. ]
  253. ], $result);
  254. }
  255. public function dateTimeProvider()
  256. {
  257. $d = 'October 13, 2012 16:15:46 UTC';
  258. return [
  259. [$d, 'date-time', '2012-10-13T16:15:46Z'],
  260. [$d, 'date', '2012-10-13'],
  261. [$d, 'timestamp', strtotime($d)],
  262. [new \DateTime($d), 'timestamp', strtotime($d)]
  263. ];
  264. }
  265. /**
  266. * @dataProvider dateTimeProvider
  267. */
  268. public function testAppliesFormat($d, $format, $result)
  269. {
  270. $p = new Parameter(['format' => $format], ['description' => new Description([])]);
  271. $this->assertEquals($format, $p->getFormat());
  272. $this->assertEquals($result, $p->filter($d));
  273. }
  274. public function testHasMinAndMax()
  275. {
  276. $p = new Parameter([
  277. 'minimum' => 2,
  278. 'maximum' => 3,
  279. 'minItems' => 4,
  280. 'maxItems' => 5,
  281. ]);
  282. $this->assertEquals(2, $p->getMinimum());
  283. $this->assertEquals(3, $p->getMaximum());
  284. $this->assertEquals(4, $p->getMinItems());
  285. $this->assertEquals(5, $p->getMaxItems());
  286. }
  287. public function testHasProperties()
  288. {
  289. $data = [
  290. 'type' => 'object',
  291. 'properties' => [
  292. 'foo' => ['type' => 'string'],
  293. 'bar' => ['type' => 'string'],
  294. ]
  295. ];
  296. $p = new Parameter($data);
  297. $this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Parameter', $p->getProperty('foo'));
  298. $this->assertSame($p->getProperty('foo'), $p->getProperty('foo'));
  299. $this->assertNull($p->getProperty('wefwe'));
  300. $properties = $p->getProperties();
  301. $this->assertInternalType('array', $properties);
  302. foreach ($properties as $prop) {
  303. $this->assertInstanceOf('GuzzleHttp\\Command\\Guzzle\\Parameter', $prop);
  304. }
  305. $this->assertEquals($data, $p->toArray());
  306. }
  307. /**
  308. * @expectedException \InvalidArgumentException
  309. * @expectedExceptionMessage Expected a string. Got: array
  310. */
  311. public function testThrowsWhenNotPassString()
  312. {
  313. $emptyParam = new Parameter();
  314. $this->assertFalse($emptyParam->has([]));
  315. $this->assertFalse($emptyParam->has(new \stdClass()));
  316. $this->assertFalse($emptyParam->has('1'));
  317. $this->assertFalse($emptyParam->has(1));
  318. }
  319. public function testHasReturnsFalseForWrongOrEmptyValues()
  320. {
  321. $emptyParam = new Parameter();
  322. $this->assertFalse($emptyParam->has(''));
  323. $this->assertFalse($emptyParam->has('description'));
  324. $this->assertFalse($emptyParam->has('noExisting'));
  325. }
  326. public function testHasReturnsTrueForCorrectValues()
  327. {
  328. $p = new Parameter([
  329. 'minimum' => 2,
  330. 'maximum' => 3,
  331. 'minItems' => 4,
  332. 'maxItems' => 5,
  333. ]);
  334. $this->assertTrue($p->has('minimum'));
  335. $this->assertTrue($p->has('maximum'));
  336. $this->assertTrue($p->has('minItems'));
  337. $this->assertTrue($p->has('maxItems'));
  338. }
  339. }