PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/api/vendor/guzzle/guzzle/tests/Guzzle/Tests/Service/Description/ParameterTest.php

https://gitlab.com/x33n/respond
PHP | 416 lines | 362 code | 40 blank | 14 comment | 0 complexity | 3d0dc26044634cb17f18496f39563cb3 MD5 | raw file
  1. <?php
  2. namespace Guzzle\Tests\Service\Description;
  3. use Guzzle\Service\Description\Parameter;
  4. use Guzzle\Service\Description\ServiceDescription;
  5. /**
  6. * @covers Guzzle\Service\Description\Parameter
  7. */
  8. class ParameterTest extends \Guzzle\Tests\GuzzleTestCase
  9. {
  10. protected $data = array(
  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' => 'static!',
  20. 'filters' => array('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->assertEquals(true, $p->getRequired());
  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->assertEquals('static!', $p->getStatic());
  34. $this->assertEquals(array('trim', 'json_encode'), $p->getFilters());
  35. }
  36. public function testCanConvertToArray()
  37. {
  38. $p = new Parameter($this->data);
  39. unset($this->data['name']);
  40. $this->assertEquals($this->data, $p->toArray());
  41. }
  42. public function testUsesStatic()
  43. {
  44. $d = $this->data;
  45. $d['default'] = 'booboo';
  46. $d['static'] = true;
  47. $p = new Parameter($d);
  48. $this->assertEquals('booboo', $p->getValue('bar'));
  49. }
  50. public function testUsesDefault()
  51. {
  52. $d = $this->data;
  53. $d['default'] = 'foo';
  54. $d['static'] = null;
  55. $p = new Parameter($d);
  56. $this->assertEquals('foo', $p->getValue(null));
  57. }
  58. public function testReturnsYourValue()
  59. {
  60. $d = $this->data;
  61. $d['static'] = null;
  62. $p = new Parameter($d);
  63. $this->assertEquals('foo', $p->getValue('foo'));
  64. }
  65. public function testZeroValueDoesNotCauseDefaultToBeReturned()
  66. {
  67. $d = $this->data;
  68. $d['default'] = '1';
  69. $d['static'] = null;
  70. $p = new Parameter($d);
  71. $this->assertEquals('0', $p->getValue('0'));
  72. }
  73. public function testFiltersValues()
  74. {
  75. $d = $this->data;
  76. $d['static'] = null;
  77. $d['filters'] = 'strtoupper';
  78. $p = new Parameter($d);
  79. $this->assertEquals('FOO', $p->filter('foo'));
  80. }
  81. public function testConvertsBooleans()
  82. {
  83. $p = new Parameter(array('type' => 'boolean'));
  84. $this->assertEquals(true, $p->filter('true'));
  85. $this->assertEquals(false, $p->filter('false'));
  86. }
  87. public function testUsesArrayByDefaultForFilters()
  88. {
  89. $d = $this->data;
  90. $d['filters'] = null;
  91. $p = new Parameter($d);
  92. $this->assertEquals(array(), $p->getFilters());
  93. }
  94. public function testAllowsSimpleLocationValue()
  95. {
  96. $p = new Parameter(array('name' => 'myname', 'location' => 'foo', 'sentAs' => 'Hello'));
  97. $this->assertEquals('foo', $p->getLocation());
  98. $this->assertEquals('Hello', $p->getSentAs());
  99. }
  100. public function testParsesTypeValues()
  101. {
  102. $p = new Parameter(array('type' => 'foo'));
  103. $this->assertEquals('foo', $p->getType());
  104. }
  105. /**
  106. * @expectedException InvalidArgumentException
  107. * @expectedExceptionMessage A [method] value must be specified for each complex filter
  108. */
  109. public function testValidatesComplexFilters()
  110. {
  111. $p = new Parameter(array('filters' => array(array('args' => 'foo'))));
  112. }
  113. public function testCanBuildUpParams()
  114. {
  115. $p = new Parameter(array());
  116. $p->setName('foo')
  117. ->setDescription('c')
  118. ->setFilters(array('d'))
  119. ->setLocation('e')
  120. ->setSentAs('f')
  121. ->setMaxLength(1)
  122. ->setMinLength(1)
  123. ->setMinimum(2)
  124. ->setMaximum(2)
  125. ->setMinItems(3)
  126. ->setMaxItems(3)
  127. ->setRequired(true)
  128. ->setStatic(true)
  129. ->setDefault('h')
  130. ->setType('i');
  131. $p->addFilter('foo');
  132. $this->assertEquals('foo', $p->getName());
  133. $this->assertEquals('h', $p->getDefault());
  134. $this->assertEquals('c', $p->getDescription());
  135. $this->assertEquals(array('d', 'foo'), $p->getFilters());
  136. $this->assertEquals('e', $p->getLocation());
  137. $this->assertEquals('f', $p->getSentAs());
  138. $this->assertEquals(1, $p->getMaxLength());
  139. $this->assertEquals(1, $p->getMinLength());
  140. $this->assertEquals(2, $p->getMaximum());
  141. $this->assertEquals(2, $p->getMinimum());
  142. $this->assertEquals(3, $p->getMaxItems());
  143. $this->assertEquals(3, $p->getMinItems());
  144. $this->assertEquals(true, $p->getRequired());
  145. $this->assertEquals(true, $p->getStatic());
  146. $this->assertEquals('i', $p->getType());
  147. }
  148. public function testAllowsNestedShape()
  149. {
  150. $command = $this->getServiceBuilder()->get('mock')->getCommand('mock_command')->getOperation();
  151. $param = new Parameter(array(
  152. 'parent' => $command,
  153. 'name' => 'foo',
  154. 'type' => 'object',
  155. 'location' => 'query',
  156. 'properties' => array(
  157. 'foo' => array(
  158. 'type' => 'object',
  159. 'required' => true,
  160. 'properties' => array(
  161. 'baz' => array(
  162. 'name' => 'baz',
  163. 'type' => 'bool',
  164. )
  165. )
  166. ),
  167. 'bar' => array(
  168. 'name' => 'bar',
  169. 'default' => '123'
  170. )
  171. )
  172. ));
  173. $this->assertSame($command, $param->getParent());
  174. $this->assertNotEmpty($param->getProperties());
  175. $this->assertInstanceOf('Guzzle\Service\Description\Parameter', $param->getProperty('foo'));
  176. $this->assertSame($param, $param->getProperty('foo')->getParent());
  177. $this->assertSame($param->getProperty('foo'), $param->getProperty('foo')->getProperty('baz')->getParent());
  178. $this->assertInstanceOf('Guzzle\Service\Description\Parameter', $param->getProperty('bar'));
  179. $this->assertSame($param, $param->getProperty('bar')->getParent());
  180. $array = $param->toArray();
  181. $this->assertInternalType('array', $array['properties']);
  182. $this->assertArrayHasKey('foo', $array['properties']);
  183. $this->assertArrayHasKey('bar', $array['properties']);
  184. }
  185. public function testAllowsComplexFilters()
  186. {
  187. $that = $this;
  188. $param = new Parameter(array());
  189. $param->setFilters(array(array('method' => function ($a, $b, $c, $d) use ($that, $param) {
  190. $that->assertEquals('test', $a);
  191. $that->assertEquals('my_value!', $b);
  192. $that->assertEquals('bar', $c);
  193. $that->assertSame($param, $d);
  194. return 'abc' . $b;
  195. }, 'args' => array('test', '@value', 'bar', '@api'))));
  196. $this->assertEquals('abcmy_value!', $param->filter('my_value!'));
  197. }
  198. public function testCanChangeParentOfNestedParameter()
  199. {
  200. $param1 = new Parameter(array('name' => 'parent'));
  201. $param2 = new Parameter(array('name' => 'child'));
  202. $param2->setParent($param1);
  203. $this->assertSame($param1, $param2->getParent());
  204. }
  205. public function testCanRemoveFromNestedStructure()
  206. {
  207. $param1 = new Parameter(array('name' => 'parent'));
  208. $param2 = new Parameter(array('name' => 'child'));
  209. $param1->addProperty($param2);
  210. $this->assertSame($param1, $param2->getParent());
  211. $this->assertSame($param2, $param1->getProperty('child'));
  212. // Remove a single child from the structure
  213. $param1->removeProperty('child');
  214. $this->assertNull($param1->getProperty('child'));
  215. // Remove the entire structure
  216. $param1->addProperty($param2);
  217. $param1->removeProperty('child');
  218. $this->assertNull($param1->getProperty('child'));
  219. }
  220. public function testAddsAdditionalProperties()
  221. {
  222. $p = new Parameter(array(
  223. 'type' => 'object',
  224. 'additionalProperties' => array('type' => 'string')
  225. ));
  226. $this->assertInstanceOf('Guzzle\Service\Description\Parameter', $p->getAdditionalProperties());
  227. $this->assertNull($p->getAdditionalProperties()->getAdditionalProperties());
  228. $p = new Parameter(array('type' => 'object'));
  229. $this->assertTrue($p->getAdditionalProperties());
  230. }
  231. public function testAddsItems()
  232. {
  233. $p = new Parameter(array(
  234. 'type' => 'array',
  235. 'items' => array('type' => 'string')
  236. ));
  237. $this->assertInstanceOf('Guzzle\Service\Description\Parameter', $p->getItems());
  238. $out = $p->toArray();
  239. $this->assertEquals('array', $out['type']);
  240. $this->assertInternalType('array', $out['items']);
  241. }
  242. public function testHasExtraProperties()
  243. {
  244. $p = new Parameter();
  245. $this->assertEquals(array(), $p->getData());
  246. $p->setData(array('foo' => 'bar'));
  247. $this->assertEquals('bar', $p->getData('foo'));
  248. $p->setData('baz', 'boo');
  249. $this->assertEquals(array('foo' => 'bar', 'baz' => 'boo'), $p->getData());
  250. }
  251. public function testCanRetrieveKnownPropertiesUsingDataMethod()
  252. {
  253. $p = new Parameter();
  254. $this->assertEquals(null, $p->getData('foo'));
  255. $p->setName('test');
  256. $this->assertEquals('test', $p->getData('name'));
  257. }
  258. public function testHasInstanceOf()
  259. {
  260. $p = new Parameter();
  261. $this->assertNull($p->getInstanceOf());
  262. $p->setInstanceOf('Foo');
  263. $this->assertEquals('Foo', $p->getInstanceOf());
  264. }
  265. public function testHasPattern()
  266. {
  267. $p = new Parameter();
  268. $this->assertNull($p->getPattern());
  269. $p->setPattern('/[0-9]+/');
  270. $this->assertEquals('/[0-9]+/', $p->getPattern());
  271. }
  272. public function testHasEnum()
  273. {
  274. $p = new Parameter();
  275. $this->assertNull($p->getEnum());
  276. $p->setEnum(array('foo', 'bar'));
  277. $this->assertEquals(array('foo', 'bar'), $p->getEnum());
  278. }
  279. public function testSerializesItems()
  280. {
  281. $p = new Parameter(array(
  282. 'type' => 'object',
  283. 'additionalProperties' => array('type' => 'string')
  284. ));
  285. $this->assertEquals(array(
  286. 'type' => 'object',
  287. 'additionalProperties' => array('type' => 'string')
  288. ), $p->toArray());
  289. }
  290. public function testResolvesRefKeysRecursively()
  291. {
  292. $jarJar = array('type' => 'string', 'default' => 'Mesa address tha senate!');
  293. $anakin = array('type' => 'array', 'items' => array('$ref' => 'JarJar'));
  294. $description = new ServiceDescription(array(
  295. 'models' => array(
  296. 'JarJar' => $jarJar,
  297. 'Anakin' => $anakin
  298. )
  299. ));
  300. // description attribute will be removed
  301. $p = new Parameter(array('$ref' => 'Anakin', 'description' => 'missing'), $description);
  302. $this->assertEquals(array(
  303. 'type' => 'array',
  304. 'items' => array(
  305. 'type' => 'string',
  306. 'default' => 'Mesa address tha senate!'
  307. )
  308. ), $p->toArray());
  309. }
  310. public function testResolvesExtendsRecursively()
  311. {
  312. $jarJar = array('type' => 'string', 'default' => 'Mesa address tha senate!', 'description' => 'a');
  313. $anakin = array('type' => 'array', 'items' => array('extends' => 'JarJar', 'description' => 'b'));
  314. $description = new ServiceDescription(array(
  315. 'models' => array('JarJar' => $jarJar, 'Anakin' => $anakin)
  316. ));
  317. // Description attribute will be updated, and format added
  318. $p = new Parameter(array('extends' => 'Anakin', 'format' => 'date'), $description);
  319. $this->assertEquals(array(
  320. 'type' => 'array',
  321. 'format' => 'date',
  322. 'items' => array(
  323. 'type' => 'string',
  324. 'default' => 'Mesa address tha senate!',
  325. 'description' => 'b'
  326. )
  327. ), $p->toArray());
  328. }
  329. public function testHasKeyMethod()
  330. {
  331. $p = new Parameter(array('name' => 'foo', 'sentAs' => 'bar'));
  332. $this->assertEquals('bar', $p->getWireName());
  333. $p->setSentAs(null);
  334. $this->assertEquals('foo', $p->getWireName());
  335. }
  336. public function testIncludesNameInToArrayWhenItemsAttributeHasName()
  337. {
  338. $p = new Parameter(array(
  339. 'type' => 'array',
  340. 'name' => 'Abc',
  341. 'items' => array(
  342. 'name' => 'Foo',
  343. 'type' => 'object'
  344. )
  345. ));
  346. $result = $p->toArray();
  347. $this->assertEquals(array(
  348. 'type' => 'array',
  349. 'items' => array(
  350. 'name' => 'Foo',
  351. 'type' => 'object',
  352. 'additionalProperties' => true
  353. )
  354. ), $result);
  355. }
  356. public function dateTimeProvider()
  357. {
  358. $d = 'October 13, 2012 16:15:46 UTC';
  359. return array(
  360. array($d, 'date-time', '2012-10-13T16:15:46Z'),
  361. array($d, 'date', '2012-10-13'),
  362. array($d, 'timestamp', strtotime($d)),
  363. array(new \DateTime($d), 'timestamp', strtotime($d))
  364. );
  365. }
  366. /**
  367. * @dataProvider dateTimeProvider
  368. */
  369. public function testAppliesFormat($d, $format, $result)
  370. {
  371. $p = new Parameter();
  372. $p->setFormat($format);
  373. $this->assertEquals($format, $p->getFormat());
  374. $this->assertEquals($result, $p->filter($d));
  375. }
  376. }