JsonLocationTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. namespace GuzzleHttp\Tests\Command\Guzzle\ResponseLocation;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Command\Guzzle\Description;
  5. use GuzzleHttp\Command\Guzzle\GuzzleClient;
  6. use GuzzleHttp\Command\Guzzle\Parameter;
  7. use GuzzleHttp\Command\Guzzle\ResponseLocation\JsonLocation;
  8. use GuzzleHttp\Command\Result;
  9. use GuzzleHttp\Command\ResultInterface;
  10. use GuzzleHttp\Handler\MockHandler;
  11. use GuzzleHttp\Psr7\Response;
  12. /**
  13. * @covers \GuzzleHttp\Command\Guzzle\ResponseLocation\JsonLocation
  14. * @covers \GuzzleHttp\Command\Guzzle\Deserializer
  15. */
  16. class JsonLocationTest extends \PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * @group ResponseLocation
  20. */
  21. public function testVisitsLocation()
  22. {
  23. $location = new JsonLocation();
  24. $parameter = new Parameter([
  25. 'name' => 'val',
  26. 'sentAs' => 'vim',
  27. 'filters' => ['strtoupper']
  28. ]);
  29. $response = new Response(200, [], '{"vim":"bar"}');
  30. $result = new Result();
  31. $result = $location->before($result, $response, $parameter);
  32. $result = $location->visit($result, $response, $parameter);
  33. $this->assertEquals('BAR', $result['val']);
  34. }
  35. /**
  36. * @group ResponseLocation
  37. * @param $name
  38. * @param $expected
  39. */
  40. public function testVisitsWiredArray()
  41. {
  42. $json = ['car_models' => ['ferrari', 'aston martin']];
  43. $body = \GuzzleHttp\json_encode($json);
  44. $response = new Response(200, ['Content-Type' => 'application/json'], $body);
  45. $mock = new MockHandler([$response]);
  46. $guzzle = new Client(['handler' => $mock]);
  47. $description = new Description([
  48. 'operations' => [
  49. 'getCars' => [
  50. 'uri' => 'http://httpbin.org',
  51. 'httpMethod' => 'GET',
  52. 'responseModel' => 'Cars'
  53. ]
  54. ],
  55. 'models' => [
  56. 'Cars' => [
  57. 'type' => 'object',
  58. 'location' => 'json',
  59. 'properties' => [
  60. 'cars' => [
  61. 'type' => 'array',
  62. 'sentAs' => 'car_models',
  63. 'items' => [
  64. 'type' => 'object',
  65. ]
  66. ]
  67. ],
  68. ]
  69. ]
  70. ]);
  71. $guzzle = new GuzzleClient($guzzle, $description);
  72. $result = $guzzle->getCars();
  73. $this->assertEquals(['cars' => ['ferrari', 'aston martin']], $result->toArray());
  74. }
  75. /**
  76. * @group ResponseLocation
  77. */
  78. public function testVisitsAdditionalProperties()
  79. {
  80. $location = new JsonLocation();
  81. $parameter = new Parameter();
  82. $model = new Parameter(['additionalProperties' => ['location' => 'json']]);
  83. $response = new Response(200, [], '{"vim":"bar","qux":[1,2]}');
  84. $result = new Result();
  85. $result = $location->before($result, $response, $parameter);
  86. $result = $location->visit($result, $response, $parameter);
  87. $result = $location->after($result, $response, $model);
  88. $this->assertEquals('bar', $result['vim']);
  89. $this->assertEquals([1, 2], $result['qux']);
  90. }
  91. /**
  92. * @group ResponseLocation
  93. */
  94. public function testVisitsAdditionalPropertiesWithEmptyResponse()
  95. {
  96. $location = new JsonLocation();
  97. $parameter = new Parameter();
  98. $model = new Parameter(['additionalProperties' => ['location' => 'json']]);
  99. $response = new Response(204);
  100. $result = new Result();
  101. $result = $location->before($result, $response, $parameter);
  102. $result = $location->visit($result, $response, $parameter);
  103. $result = $location->after($result, $response, $model);
  104. $this->assertEquals([], $result->toArray());
  105. }
  106. public function jsonProvider()
  107. {
  108. return [
  109. [null, [['foo' => 'BAR'], ['baz' => 'BAM']]],
  110. ['under_me', ['under_me' => [['foo' => 'BAR'], ['baz' => 'BAM']]]],
  111. ];
  112. }
  113. /**
  114. * @dataProvider jsonProvider
  115. * @group ResponseLocation
  116. * @param $name
  117. * @param $expected
  118. */
  119. public function testVisitsTopLevelArrays($name, $expected)
  120. {
  121. $json = [
  122. ['foo' => 'bar'],
  123. ['baz' => 'bam'],
  124. ];
  125. $body = \GuzzleHttp\json_encode($json);
  126. $response = new Response(200, ['Content-Type' => 'application/json'], $body);
  127. $mock = new MockHandler([$response]);
  128. $guzzle = new Client(['handler' => $mock]);
  129. $description = new Description([
  130. 'operations' => [
  131. 'foo' => [
  132. 'uri' => 'http://httpbin.org',
  133. 'httpMethod' => 'GET',
  134. 'responseModel' => 'j'
  135. ]
  136. ],
  137. 'models' => [
  138. 'j' => [
  139. 'type' => 'array',
  140. 'location' => 'json',
  141. 'name' => $name,
  142. 'items' => [
  143. 'type' => 'object',
  144. 'additionalProperties' => [
  145. 'type' => 'string',
  146. 'filters' => ['strtoupper']
  147. ]
  148. ]
  149. ]
  150. ]
  151. ]);
  152. $guzzle = new GuzzleClient($guzzle, $description);
  153. /** @var ResultInterface $result */
  154. $result = $guzzle->foo();
  155. $this->assertEquals($expected, $result->toArray());
  156. }
  157. /**
  158. * @group ResponseLocation
  159. */
  160. public function testVisitsNestedArrays()
  161. {
  162. $json = [
  163. 'scalar' => 'foo',
  164. 'nested' => [
  165. 'bar',
  166. 'baz'
  167. ]
  168. ];
  169. $body = \GuzzleHttp\json_encode($json);
  170. $response = new Response(200, ['Content-Type' => 'application/json'], $body);
  171. $mock = new MockHandler([$response]);
  172. $httpClient = new Client(['handler' => $mock]);
  173. $description = new Description([
  174. 'operations' => [
  175. 'foo' => [
  176. 'uri' => 'http://httpbin.org',
  177. 'httpMethod' => 'GET',
  178. 'responseModel' => 'j'
  179. ]
  180. ],
  181. 'models' => [
  182. 'j' => [
  183. 'type' => 'object',
  184. 'location' => 'json',
  185. 'properties' => [
  186. 'scalar' => ['type' => 'string'],
  187. 'nested' => [
  188. 'type' => 'array',
  189. 'items' => ['type' => 'string']
  190. ]
  191. ]
  192. ]
  193. ]
  194. ]);
  195. $guzzle = new GuzzleClient($httpClient, $description);
  196. /** @var ResultInterface $result */
  197. $result = $guzzle->foo();
  198. $expected = [
  199. 'scalar' => 'foo',
  200. 'nested' => [
  201. 'bar',
  202. 'baz'
  203. ]
  204. ];
  205. $this->assertEquals($expected, $result->toArray());
  206. }
  207. public function nestedProvider()
  208. {
  209. return [
  210. [
  211. [
  212. 'operations' => [
  213. 'foo' => [
  214. 'uri' => 'http://httpbin.org',
  215. 'httpMethod' => 'GET',
  216. 'responseModel' => 'j'
  217. ]
  218. ],
  219. 'models' => [
  220. 'j' => [
  221. 'type' => 'object',
  222. 'properties' => [
  223. 'nested' => [
  224. 'location' => 'json',
  225. 'type' => 'object',
  226. 'properties' => [
  227. 'foo' => ['type' => 'string'],
  228. 'bar' => ['type' => 'number'],
  229. 'bam' => [
  230. 'type' => 'object',
  231. 'properties' => [
  232. 'abc' => [
  233. 'type' => 'number'
  234. ]
  235. ]
  236. ]
  237. ]
  238. ]
  239. ],
  240. 'additionalProperties' => [
  241. 'location' => 'json',
  242. 'type' => 'string',
  243. 'filters' => ['strtoupper']
  244. ]
  245. ]
  246. ]
  247. ]
  248. ],
  249. [
  250. [
  251. 'operations' => [
  252. 'foo' => [
  253. 'uri' => 'http://httpbin.org',
  254. 'httpMethod' => 'GET',
  255. 'responseModel' => 'j'
  256. ]
  257. ],
  258. 'models' => [
  259. 'j' => [
  260. 'type' => 'object',
  261. 'location' => 'json',
  262. 'properties' => [
  263. 'nested' => [
  264. 'type' => 'object',
  265. 'properties' => [
  266. 'foo' => ['type' => 'string'],
  267. 'bar' => ['type' => 'number'],
  268. 'bam' => [
  269. 'type' => 'object',
  270. 'properties' => [
  271. 'abc' => [
  272. 'type' => 'number'
  273. ]
  274. ]
  275. ]
  276. ]
  277. ]
  278. ],
  279. 'additionalProperties' => [
  280. 'type' => 'string',
  281. 'filters' => ['strtoupper']
  282. ]
  283. ]
  284. ]
  285. ]
  286. ]
  287. ];
  288. }
  289. /**
  290. * @dataProvider nestedProvider
  291. * @group ResponseLocation
  292. */
  293. public function testVisitsNestedProperties($desc)
  294. {
  295. $json = [
  296. 'nested' => [
  297. 'foo' => 'abc',
  298. 'bar' => 123,
  299. 'bam' => [
  300. 'abc' => 456
  301. ]
  302. ],
  303. 'baz' => 'boo'
  304. ];
  305. $body = \GuzzleHttp\json_encode($json);
  306. $response = new Response(200, ['Content-Type' => 'application/json'], $body);
  307. $mock = new MockHandler([$response]);
  308. $httpClient = new Client(['handler' => $mock]);
  309. $description = new Description($desc);
  310. $guzzle = new GuzzleClient($httpClient, $description);
  311. /** @var ResultInterface $result */
  312. $result = $guzzle->foo();
  313. $expected = [
  314. 'nested' => [
  315. 'foo' => 'abc',
  316. 'bar' => 123,
  317. 'bam' => [
  318. 'abc' => 456
  319. ]
  320. ],
  321. 'baz' => 'BOO'
  322. ];
  323. $this->assertEquals($expected, $result->toArray());
  324. }
  325. /**
  326. * @group ResponseLocation
  327. */
  328. public function testVisitsNullResponseProperties()
  329. {
  330. $json = [
  331. 'data' => [
  332. 'link' => null
  333. ]
  334. ];
  335. $body = \GuzzleHttp\json_encode($json);
  336. $response = new Response(200, ['Content-Type' => 'application/json'], $body);
  337. $mock = new MockHandler([$response]);
  338. $httpClient = new Client(['handler' => $mock]);
  339. $description = new Description(
  340. [
  341. 'operations' => [
  342. 'foo' => [
  343. 'uri' => 'http://httpbin.org',
  344. 'httpMethod' => 'GET',
  345. 'responseModel' => 'j'
  346. ]
  347. ],
  348. 'models' => [
  349. 'j' => [
  350. 'type' => 'object',
  351. 'location' => 'json',
  352. 'properties' => [
  353. 'scalar' => ['type' => 'string'],
  354. 'data' => [
  355. 'type' => 'object',
  356. 'location' => 'json',
  357. 'properties' => [
  358. 'link' => [
  359. 'name' => 'val',
  360. 'type' => 'string',
  361. 'location' => 'json'
  362. ],
  363. ],
  364. 'additionalProperties' => false
  365. ]
  366. ]
  367. ]
  368. ]
  369. ]
  370. );
  371. $guzzle = new GuzzleClient($httpClient, $description);
  372. /** @var ResultInterface $result */
  373. $result = $guzzle->foo();
  374. $expected = [
  375. 'data' => [
  376. 'link' => null
  377. ]
  378. ];
  379. $this->assertEquals($expected, $result->toArray());
  380. }
  381. /**
  382. * @group ResponseLocation
  383. */
  384. public function testVisitsNestedArrayOfArrays()
  385. {
  386. $json = [
  387. 'scalar' => 'foo',
  388. 'nested' => [
  389. [
  390. 'bar' => 123,
  391. 'baz' => false,
  392. ],
  393. [
  394. 'bar' => 345,
  395. 'baz' => true,
  396. ],
  397. [
  398. 'bar' => 678,
  399. 'baz' => true,
  400. ],
  401. ]
  402. ];
  403. $body = \GuzzleHttp\json_encode($json);
  404. $response = new Response(200, ['Content-Type' => 'application/json'], $body);
  405. $mock = new MockHandler([$response]);
  406. $httpClient = new Client(['handler' => $mock]);
  407. $description = new Description([
  408. 'operations' => [
  409. 'foo' => [
  410. 'uri' => 'http://httpbin.org',
  411. 'httpMethod' => 'GET',
  412. 'responseModel' => 'j'
  413. ]
  414. ],
  415. 'models' => [
  416. 'j' => [
  417. 'type' => 'object',
  418. 'properties' => [
  419. 'scalar' => [
  420. // for some reason (probably because location is also set on array of arrays)
  421. // array of arrays sibling elements must have location set to `json`
  422. // otherwise JsonLocation ignores them
  423. 'location' => 'json',
  424. 'type' => 'string'
  425. ],
  426. 'nested' => [
  427. // array of arrays type must be set to `array`
  428. // without that JsonLocation throws an exception
  429. 'type' => 'array',
  430. // for array of arrays `location` must be set to `json`
  431. // otherwise JsonLocation returns an empty array
  432. 'location' => 'json',
  433. 'items' => [
  434. // although this is array of arrays, array items type
  435. // must be set as `object`
  436. 'type' => 'object',
  437. 'properties' => [
  438. 'bar' => [
  439. 'type' => 'integer',
  440. ],
  441. 'baz' => [
  442. 'type' => 'boolean',
  443. ],
  444. ],
  445. ]
  446. ]
  447. ]
  448. ]
  449. ]
  450. ]);
  451. $guzzle = new GuzzleClient($httpClient, $description);
  452. /** @var ResultInterface $result */
  453. $result = $guzzle->foo();
  454. $expected = [
  455. 'scalar' => 'foo',
  456. 'nested' => [
  457. [
  458. 'bar' => 123,
  459. 'baz' => false,
  460. ],
  461. [
  462. 'bar' => 345,
  463. 'baz' => true,
  464. ],
  465. [
  466. 'bar' => 678,
  467. 'baz' => true,
  468. ],
  469. ]
  470. ];
  471. $this->assertEquals($expected, $result->toArray());
  472. }
  473. /**
  474. * @group ResponseLocation
  475. */
  476. public function testVisitsNestedArrayOfObjects()
  477. {
  478. $json = json_decode('{"scalar":"foo","nested":[{"bar":123,"baz":false},{"bar":345,"baz":true},{"bar":678,"baz":true}]}');
  479. $body = \GuzzleHttp\json_encode($json);
  480. $response = new Response(200, ['Content-Type' => 'application/json'], $body);
  481. $mock = new MockHandler([$response]);
  482. $httpClient = new Client(['handler' => $mock]);
  483. $description = new Description([
  484. 'operations' => [
  485. 'foo' => [
  486. 'uri' => 'http://httpbin.org',
  487. 'httpMethod' => 'GET',
  488. 'responseModel' => 'j'
  489. ]
  490. ],
  491. 'models' => [
  492. 'j' => [
  493. 'type' => 'object',
  494. 'location' => 'json',
  495. 'properties' => [
  496. 'scalar' => [
  497. 'type' => 'string'
  498. ],
  499. 'nested' => [
  500. // array of objects type must be set to `array`
  501. // without that JsonLocation throws an exception
  502. 'type' => 'array',
  503. 'items' => [
  504. // array elements type must be set to `object`
  505. 'type' => 'object',
  506. 'properties' => [
  507. 'bar' => [
  508. 'type' => 'integer',
  509. ],
  510. 'baz' => [
  511. 'type' => 'boolean',
  512. ],
  513. ],
  514. ]
  515. ]
  516. ]
  517. ]
  518. ]
  519. ]);
  520. $guzzle = new GuzzleClient($httpClient, $description);
  521. /** @var ResultInterface $result */
  522. $result = $guzzle->foo();
  523. $expected = [
  524. 'scalar' => 'foo',
  525. 'nested' => [
  526. [
  527. 'bar' => 123,
  528. 'baz' => false,
  529. ],
  530. [
  531. 'bar' => 345,
  532. 'baz' => true,
  533. ],
  534. [
  535. 'bar' => 678,
  536. 'baz' => true,
  537. ],
  538. ]
  539. ];
  540. $this->assertEquals($expected, $result->toArray());
  541. }
  542. }