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

https://gitlab.com/x33n/respond · PHP · 308 lines · 269 code · 27 blank · 12 comment · 0 complexity · 8ff83ab037e47acb3e3af00fe0770b9a MD5 · raw file

  1. <?php
  2. namespace Guzzle\Tests\Service\Description;
  3. use Guzzle\Service\Description\Operation;
  4. use Guzzle\Service\Description\Parameter;
  5. use Guzzle\Service\Description\ServiceDescription;
  6. /**
  7. * @covers Guzzle\Service\Description\Operation
  8. */
  9. class OperationTest extends \Guzzle\Tests\GuzzleTestCase
  10. {
  11. public static function strtoupper($string)
  12. {
  13. return strtoupper($string);
  14. }
  15. public function testOperationIsDataObject()
  16. {
  17. $c = new Operation(array(
  18. 'name' => 'test',
  19. 'summary' => 'doc',
  20. 'notes' => 'notes',
  21. 'documentationUrl' => 'http://www.example.com',
  22. 'httpMethod' => 'POST',
  23. 'uri' => '/api/v1',
  24. 'responseClass' => 'array',
  25. 'responseNotes' => 'returns the json_decoded response',
  26. 'deprecated' => true,
  27. 'parameters' => array(
  28. 'key' => array(
  29. 'required' => true,
  30. 'type' => 'string',
  31. 'maxLength' => 10
  32. ),
  33. 'key_2' => array(
  34. 'required' => true,
  35. 'type' => 'integer',
  36. 'default' => 10
  37. )
  38. )
  39. ));
  40. $this->assertEquals('test', $c->getName());
  41. $this->assertEquals('doc', $c->getSummary());
  42. $this->assertEquals('http://www.example.com', $c->getDocumentationUrl());
  43. $this->assertEquals('POST', $c->getHttpMethod());
  44. $this->assertEquals('/api/v1', $c->getUri());
  45. $this->assertEquals('array', $c->getResponseClass());
  46. $this->assertEquals('returns the json_decoded response', $c->getResponseNotes());
  47. $this->assertTrue($c->getDeprecated());
  48. $this->assertEquals('Guzzle\\Service\\Command\\OperationCommand', $c->getClass());
  49. $this->assertEquals(array(
  50. 'key' => new Parameter(array(
  51. 'name' => 'key',
  52. 'required' => true,
  53. 'type' => 'string',
  54. 'maxLength' => 10,
  55. 'parent' => $c
  56. )),
  57. 'key_2' => new Parameter(array(
  58. 'name' => 'key_2',
  59. 'required' => true,
  60. 'type' => 'integer',
  61. 'default' => 10,
  62. 'parent' => $c
  63. ))
  64. ), $c->getParams());
  65. $this->assertEquals(new Parameter(array(
  66. 'name' => 'key_2',
  67. 'required' => true,
  68. 'type' => 'integer',
  69. 'default' => 10,
  70. 'parent' => $c
  71. )), $c->getParam('key_2'));
  72. $this->assertNull($c->getParam('afefwef'));
  73. $this->assertArrayNotHasKey('parent', $c->getParam('key_2')->toArray());
  74. }
  75. public function testAllowsConcreteCommands()
  76. {
  77. $c = new Operation(array(
  78. 'name' => 'test',
  79. 'class' => 'Guzzle\\Service\\Command\ClosureCommand',
  80. 'parameters' => array(
  81. 'p' => new Parameter(array(
  82. 'name' => 'foo'
  83. ))
  84. )
  85. ));
  86. $this->assertEquals('Guzzle\\Service\\Command\ClosureCommand', $c->getClass());
  87. }
  88. public function testConvertsToArray()
  89. {
  90. $data = array(
  91. 'name' => 'test',
  92. 'class' => 'Guzzle\\Service\\Command\ClosureCommand',
  93. 'summary' => 'test',
  94. 'documentationUrl' => 'http://www.example.com',
  95. 'httpMethod' => 'PUT',
  96. 'uri' => '/',
  97. 'parameters' => array('p' => array('name' => 'foo'))
  98. );
  99. $c = new Operation($data);
  100. $toArray = $c->toArray();
  101. unset($data['name']);
  102. $this->assertArrayHasKey('parameters', $toArray);
  103. $this->assertInternalType('array', $toArray['parameters']);
  104. // Normalize the array
  105. unset($data['parameters']);
  106. unset($toArray['parameters']);
  107. $data['responseType'] = 'primitive';
  108. $data['responseClass'] = 'array';
  109. $this->assertEquals($data, $toArray);
  110. }
  111. public function testDeterminesIfHasParam()
  112. {
  113. $command = $this->getTestCommand();
  114. $this->assertTrue($command->hasParam('data'));
  115. $this->assertFalse($command->hasParam('baz'));
  116. }
  117. public function testReturnsParamNames()
  118. {
  119. $command = $this->getTestCommand();
  120. $this->assertEquals(array('data'), $command->getParamNames());
  121. }
  122. protected function getTestCommand()
  123. {
  124. return new Operation(array(
  125. 'parameters' => array(
  126. 'data' => new Parameter(array(
  127. 'type' => 'string'
  128. ))
  129. )
  130. ));
  131. }
  132. public function testCanBuildUpCommands()
  133. {
  134. $c = new Operation(array());
  135. $c->setName('foo')
  136. ->setClass('Baz')
  137. ->setDeprecated(false)
  138. ->setSummary('summary')
  139. ->setDocumentationUrl('http://www.foo.com')
  140. ->setHttpMethod('PUT')
  141. ->setResponseNotes('oh')
  142. ->setResponseClass('string')
  143. ->setUri('/foo/bar')
  144. ->addParam(new Parameter(array(
  145. 'name' => 'test'
  146. )));
  147. $this->assertEquals('foo', $c->getName());
  148. $this->assertEquals('Baz', $c->getClass());
  149. $this->assertEquals(false, $c->getDeprecated());
  150. $this->assertEquals('summary', $c->getSummary());
  151. $this->assertEquals('http://www.foo.com', $c->getDocumentationUrl());
  152. $this->assertEquals('PUT', $c->getHttpMethod());
  153. $this->assertEquals('oh', $c->getResponseNotes());
  154. $this->assertEquals('string', $c->getResponseClass());
  155. $this->assertEquals('/foo/bar', $c->getUri());
  156. $this->assertEquals(array('test'), $c->getParamNames());
  157. }
  158. public function testCanRemoveParams()
  159. {
  160. $c = new Operation(array());
  161. $c->addParam(new Parameter(array('name' => 'foo')));
  162. $this->assertTrue($c->hasParam('foo'));
  163. $c->removeParam('foo');
  164. $this->assertFalse($c->hasParam('foo'));
  165. }
  166. public function testAddsNameToParametersIfNeeded()
  167. {
  168. $command = new Operation(array('parameters' => array('foo' => new Parameter(array()))));
  169. $this->assertEquals('foo', $command->getParam('foo')->getName());
  170. }
  171. public function testContainsApiErrorInformation()
  172. {
  173. $command = $this->getOperation();
  174. $this->assertEquals(1, count($command->getErrorResponses()));
  175. $arr = $command->toArray();
  176. $this->assertEquals(1, count($arr['errorResponses']));
  177. $command->addErrorResponse(400, 'Foo', 'Baz\\Bar');
  178. $this->assertEquals(2, count($command->getErrorResponses()));
  179. $command->setErrorResponses(array());
  180. $this->assertEquals(0, count($command->getErrorResponses()));
  181. }
  182. public function testHasNotes()
  183. {
  184. $o = new Operation(array('notes' => 'foo'));
  185. $this->assertEquals('foo', $o->getNotes());
  186. $o->setNotes('bar');
  187. $this->assertEquals('bar', $o->getNotes());
  188. }
  189. public function testHasData()
  190. {
  191. $o = new Operation(array('data' => array('foo' => 'baz', 'bar' => 123)));
  192. $o->setData('test', false);
  193. $this->assertEquals('baz', $o->getData('foo'));
  194. $this->assertEquals(123, $o->getData('bar'));
  195. $this->assertNull($o->getData('wfefwe'));
  196. $this->assertEquals(array(
  197. 'parameters' => array(),
  198. 'class' => 'Guzzle\Service\Command\OperationCommand',
  199. 'data' => array('foo' => 'baz', 'bar' => 123, 'test' => false),
  200. 'responseClass' => 'array',
  201. 'responseType' => 'primitive'
  202. ), $o->toArray());
  203. }
  204. public function testHasServiceDescription()
  205. {
  206. $s = new ServiceDescription();
  207. $o = new Operation(array(), $s);
  208. $this->assertSame($s, $o->getServiceDescription());
  209. }
  210. /**
  211. * @expectedException Guzzle\Common\Exception\InvalidArgumentException
  212. */
  213. public function testValidatesResponseType()
  214. {
  215. $o = new Operation(array('responseClass' => 'array', 'responseType' => 'foo'));
  216. }
  217. public function testInfersResponseType()
  218. {
  219. $o = $this->getOperation();
  220. $o->setServiceDescription(new ServiceDescription(array('models' => array('Foo' => array()))));
  221. $this->assertEquals('primitive', $o->getResponseType());
  222. $this->assertEquals('primitive', $o->setResponseClass('boolean')->getResponseType());
  223. $this->assertEquals('primitive', $o->setResponseClass('array')->getResponseType());
  224. $this->assertEquals('primitive', $o->setResponseClass('integer')->getResponseType());
  225. $this->assertEquals('primitive', $o->setResponseClass('string')->getResponseType());
  226. $this->assertEquals('class', $o->setResponseClass('foo')->getResponseType());
  227. $this->assertEquals('class', $o->setResponseClass(__CLASS__)->getResponseType());
  228. $this->assertEquals('model', $o->setResponseClass('Foo')->getResponseType());
  229. }
  230. public function testHasResponseType()
  231. {
  232. // infers in the constructor
  233. $o = new Operation(array('responseClass' => 'array'));
  234. $this->assertEquals('primitive', $o->getResponseType());
  235. // Infers when set
  236. $o = new Operation();
  237. $this->assertEquals('primitive', $o->getResponseType());
  238. $this->assertEquals('model', $o->setResponseType('model')->getResponseType());
  239. }
  240. public function testHasAdditionalParameters()
  241. {
  242. $o = new Operation(array(
  243. 'additionalParameters' => array(
  244. 'type' => 'string', 'name' => 'binks'
  245. ),
  246. 'parameters' => array(
  247. 'foo' => array('type' => 'integer')
  248. )
  249. ));
  250. $this->assertEquals('string', $o->getAdditionalParameters()->getType());
  251. $arr = $o->toArray();
  252. $this->assertEquals(array(
  253. 'type' => 'string'
  254. ), $arr['additionalParameters']);
  255. }
  256. /**
  257. * @return Operation
  258. */
  259. protected function getOperation()
  260. {
  261. return new Operation(array(
  262. 'name' => 'OperationTest',
  263. 'class' => get_class($this),
  264. 'parameters' => array(
  265. 'test' => array('type' => 'object'),
  266. 'bool_1' => array('default' => true, 'type' => 'boolean'),
  267. 'bool_2' => array('default' => false),
  268. 'float' => array('type' => 'numeric'),
  269. 'int' => array('type' => 'integer'),
  270. 'date' => array('type' => 'string'),
  271. 'timestamp' => array('type' => 'string'),
  272. 'string' => array('type' => 'string'),
  273. 'username' => array('type' => 'string', 'required' => true, 'filters' => 'strtolower'),
  274. 'test_function' => array('type' => 'string', 'filters' => __CLASS__ . '::strtoupper')
  275. ),
  276. 'errorResponses' => array(
  277. array('code' => 503, 'reason' => 'InsufficientCapacity', 'class' => 'Guzzle\\Exception\\RuntimeException')
  278. )
  279. ));
  280. }
  281. }