DeserializerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. namespace GuzzleHttp\Tests\Command\Guzzle;
  3. use GuzzleHttp\Client as HttpClient;
  4. use GuzzleHttp\Command\CommandInterface;
  5. use GuzzleHttp\Command\Guzzle\Description;
  6. use GuzzleHttp\Command\Guzzle\DescriptionInterface;
  7. use GuzzleHttp\Command\Guzzle\GuzzleClient;
  8. use GuzzleHttp\Command\Guzzle\Operation;
  9. use GuzzleHttp\Command\ServiceClientInterface;
  10. use GuzzleHttp\Handler\MockHandler;
  11. use GuzzleHttp\HandlerStack;
  12. use GuzzleHttp\Psr7\Response;
  13. use GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException;
  14. use GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\OtherCustomCommandException;
  15. use Predis\Response\ResponseInterface;
  16. /**
  17. * @covers \GuzzleHttp\Command\Guzzle\Deserializer
  18. */
  19. class DeserializerTest extends \PHPUnit_Framework_TestCase
  20. {
  21. /** @var ServiceClientInterface|\PHPUnit_Framework_MockObject_MockObject */
  22. private $serviceClient;
  23. /** @var CommandInterface|\PHPUnit_Framework_MockObject_MockObject */
  24. private $command;
  25. public function setUp()
  26. {
  27. $this->serviceClient = $this->getMockBuilder(GuzzleClient::class)
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->command = $this->getMockBuilder(CommandInterface::class)->getMock();
  31. }
  32. protected function prepareErrorResponses($commandName, array $errors = [])
  33. {
  34. $this->command->expects($this->once())->method('getName')->will($this->returnValue($commandName));
  35. $description = $this->getMockBuilder(DescriptionInterface::class)->getMock();
  36. $operation = new Operation(['errorResponses' => $errors], $description);
  37. $description->expects($this->once())
  38. ->method('getOperation')
  39. ->with($commandName)
  40. ->will($this->returnValue($operation));
  41. $this->serviceClient->expects($this->once())
  42. ->method('getDescription')
  43. ->will($this->returnValue($description));
  44. }
  45. public function testDoNothingIfNoException()
  46. {
  47. $mock = new MockHandler([new Response(200)]);
  48. $description = new Description([
  49. 'operations' => [
  50. 'foo' => [
  51. 'uri' => 'http://httpbin.org/{foo}',
  52. 'httpMethod' => 'GET',
  53. 'responseModel' => 'j',
  54. 'parameters' => [
  55. 'bar' => [
  56. 'type' => 'string',
  57. 'required' => true,
  58. 'location' => 'uri'
  59. ]
  60. ]
  61. ]
  62. ],
  63. 'models' => [
  64. 'j' => [
  65. 'type' => 'object'
  66. ]
  67. ]
  68. ]);
  69. $httpClient = new HttpClient(['handler' => $mock]);
  70. $client = new GuzzleClient($httpClient, $description);
  71. $client->foo(['bar' => 'baz']);
  72. }
  73. /**
  74. * @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException
  75. */
  76. public function testCreateExceptionWithCode()
  77. {
  78. $response = new Response(404);
  79. $mock = new MockHandler([$response]);
  80. $description = new Description([
  81. 'name' => 'Test API',
  82. 'baseUri' => 'http://httpbin.org',
  83. 'operations' => [
  84. 'foo' => [
  85. 'uri' => '/{foo}',
  86. 'httpMethod' => 'GET',
  87. 'responseClass' => 'Foo',
  88. 'parameters' => [
  89. 'bar' => [
  90. 'type' => 'string',
  91. 'required' => true,
  92. 'description' => 'Unique user name (alphanumeric)',
  93. 'location' => 'json'
  94. ],
  95. ],
  96. 'errorResponses' => [
  97. ['code' => 404, 'class' => CustomCommandException::class]
  98. ]
  99. ]
  100. ],
  101. 'models' => [
  102. 'Foo' => [
  103. 'type' => 'object',
  104. 'additionalProperties' => [
  105. 'location' => 'json'
  106. ]
  107. ]
  108. ]
  109. ]);
  110. $httpClient = new HttpClient(['handler' => $mock]);
  111. $client = new GuzzleClient($httpClient, $description);
  112. $client->foo(['bar' => 'baz']);
  113. }
  114. public function testNotCreateExceptionIfDoesNotMatchCode()
  115. {
  116. $response = new Response(401);
  117. $mock = new MockHandler([$response]);
  118. $description = new Description([
  119. 'name' => 'Test API',
  120. 'baseUri' => 'http://httpbin.org',
  121. 'operations' => [
  122. 'foo' => [
  123. 'uri' => '/{foo}',
  124. 'httpMethod' => 'GET',
  125. 'responseClass' => 'Foo',
  126. 'parameters' => [
  127. 'bar' => [
  128. 'type' => 'string',
  129. 'required' => true,
  130. 'description' => 'Unique user name (alphanumeric)',
  131. 'location' => 'json'
  132. ],
  133. ],
  134. 'errorResponses' => [
  135. ['code' => 404, 'class' => CustomCommandException::class]
  136. ]
  137. ]
  138. ],
  139. 'models' => [
  140. 'Foo' => [
  141. 'type' => 'object',
  142. 'additionalProperties' => [
  143. 'location' => 'json'
  144. ]
  145. ]
  146. ]
  147. ]);
  148. $httpClient = new HttpClient(['handler' => $mock]);
  149. $client = new GuzzleClient($httpClient, $description);
  150. $client->foo(['bar' => 'baz']);
  151. }
  152. /**
  153. * @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\CustomCommandException
  154. */
  155. public function testCreateExceptionWithExactMatchOfReasonPhrase()
  156. {
  157. $response = new Response(404, [], null, '1.1', 'Bar');
  158. $mock = new MockHandler([$response]);
  159. $description = new Description([
  160. 'name' => 'Test API',
  161. 'baseUri' => 'http://httpbin.org',
  162. 'operations' => [
  163. 'foo' => [
  164. 'uri' => '/{foo}',
  165. 'httpMethod' => 'GET',
  166. 'responseClass' => 'Foo',
  167. 'parameters' => [
  168. 'bar' => [
  169. 'type' => 'string',
  170. 'required' => true,
  171. 'description' => 'Unique user name (alphanumeric)',
  172. 'location' => 'json'
  173. ],
  174. ],
  175. 'errorResponses' => [
  176. ['code' => 404, 'phrase' => 'Bar', 'class' => CustomCommandException::class]
  177. ]
  178. ]
  179. ],
  180. 'models' => [
  181. 'Foo' => [
  182. 'type' => 'object',
  183. 'additionalProperties' => [
  184. 'location' => 'json'
  185. ]
  186. ]
  187. ]
  188. ]);
  189. $httpClient = new HttpClient(['handler' => $mock]);
  190. $client = new GuzzleClient($httpClient, $description);
  191. $client->foo(['bar' => 'baz']);
  192. }
  193. /**
  194. * @expectedException \GuzzleHttp\Tests\Command\Guzzle\Asset\Exception\OtherCustomCommandException
  195. */
  196. public function testFavourMostPreciseMatch()
  197. {
  198. $response = new Response(404, [], null, '1.1', 'Bar');
  199. $mock = new MockHandler([$response]);
  200. $description = new Description([
  201. 'name' => 'Test API',
  202. 'baseUri' => 'http://httpbin.org',
  203. 'operations' => [
  204. 'foo' => [
  205. 'uri' => '/{foo}',
  206. 'httpMethod' => 'GET',
  207. 'responseClass' => 'Foo',
  208. 'parameters' => [
  209. 'bar' => [
  210. 'type' => 'string',
  211. 'required' => true,
  212. 'description' => 'Unique user name (alphanumeric)',
  213. 'location' => 'json'
  214. ],
  215. ],
  216. 'errorResponses' => [
  217. ['code' => 404, 'class' => CustomCommandException::class],
  218. ['code' => 404, 'phrase' => 'Bar', 'class' => OtherCustomCommandException::class],
  219. ]
  220. ]
  221. ],
  222. 'models' => [
  223. 'Foo' => [
  224. 'type' => 'object',
  225. 'additionalProperties' => [
  226. 'location' => 'json'
  227. ]
  228. ]
  229. ]
  230. ]);
  231. $httpClient = new HttpClient(['handler' => $mock]);
  232. $client = new GuzzleClient($httpClient, $description);
  233. $client->foo(['bar' => 'baz']);
  234. }
  235. /**
  236. * @expectedException \GuzzleHttp\Command\Exception\CommandException
  237. * @expectedExceptionMessage 404
  238. */
  239. public function testDoesNotAddResultWhenExceptionIsPresent()
  240. {
  241. $description = new Description([
  242. 'operations' => [
  243. 'foo' => [
  244. 'uri' => 'http://httpbin.org/{foo}',
  245. 'httpMethod' => 'GET',
  246. 'responseModel' => 'j',
  247. 'parameters' => [
  248. 'bar' => [
  249. 'type' => 'string',
  250. 'required' => true,
  251. 'location' => 'uri'
  252. ]
  253. ]
  254. ]
  255. ],
  256. 'models' => [
  257. 'j' => [
  258. 'type' => 'object'
  259. ]
  260. ]
  261. ]);
  262. $mock = new MockHandler([new Response(404)]);
  263. $stack = HandlerStack::create($mock);
  264. $httpClient = new HttpClient(['handler' => $stack]);
  265. $client = new GuzzleClient($httpClient, $description);
  266. $client->foo(['bar' => 'baz']);
  267. }
  268. public function testReturnsExpectedResult()
  269. {
  270. $loginResponse = new Response(
  271. 200,
  272. [],
  273. '{
  274. "LoginResponse":{
  275. "result":{
  276. "type":4,
  277. "username":{
  278. "uid":38664492,
  279. "content":"skyfillers-api-test"
  280. },
  281. "token":"3FB1F21014D630481D35CBC30CBF4043"
  282. },
  283. "status":{
  284. "code":200,
  285. "content":"OK"
  286. }
  287. }
  288. }'
  289. );
  290. $mock = new MockHandler([$loginResponse]);
  291. $description = new Description([
  292. 'name' => 'Test API',
  293. 'baseUri' => 'http://httpbin.org',
  294. 'operations' => [
  295. 'Login' => [
  296. 'uri' => '/{foo}',
  297. 'httpMethod' => 'POST',
  298. 'responseClass' => 'LoginResponse',
  299. 'parameters' => [
  300. 'username' => [
  301. 'type' => 'string',
  302. 'required' => true,
  303. 'description' => 'Unique user name (alphanumeric)',
  304. 'location' => 'json'
  305. ],
  306. 'password' => [
  307. 'type' => 'string',
  308. 'required' => true,
  309. 'description' => 'User\'s password',
  310. 'location' => 'json'
  311. ],
  312. 'response' => [
  313. 'type' => 'string',
  314. 'required' => false,
  315. 'description' => 'Determines the response type: xml = result content will be xml formatted (default); plain = result content will be simple text, without structure; json = result content will be json formatted',
  316. 'location' => 'json'
  317. ],
  318. 'token' => [
  319. 'type' => 'string',
  320. 'required' => false,
  321. 'description' => 'Provides the authentication token',
  322. 'location' => 'json'
  323. ]
  324. ]
  325. ]
  326. ],
  327. 'models' => [
  328. 'LoginResponse' => [
  329. 'type' => 'object',
  330. 'additionalProperties' => [
  331. 'location' => 'json'
  332. ]
  333. ]
  334. ]
  335. ]);
  336. $httpClient = new HttpClient(['handler' => $mock]);
  337. $client = new GuzzleClient($httpClient, $description);
  338. $result = $client->Login([
  339. 'username' => 'test',
  340. 'password' => 'test',
  341. 'response' => 'json',
  342. ]);
  343. $expected = [
  344. 'result' => [
  345. 'type' => 4,
  346. 'username' => [
  347. 'uid' => 38664492,
  348. 'content' => 'skyfillers-api-test'
  349. ],
  350. 'token' => '3FB1F21014D630481D35CBC30CBF4043'
  351. ],
  352. 'status' => [
  353. 'code' => 200,
  354. 'content' => 'OK'
  355. ]
  356. ];
  357. $this->assertArraySubset($expected, $result['LoginResponse']);
  358. }
  359. }