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

/vendor/guzzle/guzzle/tests/Guzzle/Tests/Service/Command/OperationResponseParserTest.php

https://gitlab.com/techniconline/kmc
PHP | 335 lines | 294 code | 21 blank | 20 comment | 0 complexity | ccc424e1b6deffbbff51aba4fa99f549 MD5 | raw file
  1. <?php
  2. namespace Guzzle\Tests\Service\Command;
  3. use Guzzle\Http\Message\Response;
  4. use Guzzle\Service\Client;
  5. use Guzzle\Service\Command\OperationResponseParser;
  6. use Guzzle\Service\Command\OperationCommand;
  7. use Guzzle\Service\Description\Operation;
  8. use Guzzle\Service\Description\ServiceDescription;
  9. use Guzzle\Service\Command\LocationVisitor\Response\StatusCodeVisitor;
  10. use Guzzle\Service\Command\LocationVisitor\Response\ReasonPhraseVisitor;
  11. use Guzzle\Service\Command\LocationVisitor\Response\JsonVisitor;
  12. use Guzzle\Service\Command\LocationVisitor\Response\BodyVisitor;
  13. use Guzzle\Service\Command\LocationVisitor\VisitorFlyweight;
  14. /**
  15. * @covers Guzzle\Service\Command\OperationResponseParser
  16. * @covers Guzzle\Service\Command\CreateResponseClassEvent
  17. */
  18. class OperationResponseParserTest extends \Guzzle\Tests\GuzzleTestCase
  19. {
  20. public function testHasVisitors()
  21. {
  22. $p = new OperationResponseParser(new VisitorFlyweight(array()));
  23. $visitor = new BodyVisitor();
  24. $p->addVisitor('foo', $visitor);
  25. $this->assertSame($visitor, $this->readAttribute($p, 'factory')->getResponseVisitor('foo'));
  26. }
  27. public function testUsesParentParser()
  28. {
  29. $p = new OperationResponseParser(new VisitorFlyweight());
  30. $operation = new Operation();
  31. $operation->setServiceDescription(new ServiceDescription());
  32. $op = new OperationCommand(array(), $operation);
  33. $op->setResponseParser($p)->setClient(new Client());
  34. $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/xml'), '<F><B>C</B></F>'), true);
  35. $this->assertInstanceOf('SimpleXMLElement', $op->execute());
  36. }
  37. public function testVisitsLocations()
  38. {
  39. $parser = new OperationResponseParser(new VisitorFlyweight(array()));
  40. $parser->addVisitor('statusCode', new StatusCodeVisitor());
  41. $parser->addVisitor('reasonPhrase', new ReasonPhraseVisitor());
  42. $parser->addVisitor('json', new JsonVisitor());
  43. $op = new OperationCommand(array(), $this->getDescription()->getOperation('test'));
  44. $op->setResponseParser($parser)->setClient(new Client());
  45. $op->prepare()->setResponse(new Response(201), true);
  46. $result = $op->execute();
  47. $this->assertEquals(201, $result['code']);
  48. $this->assertEquals('Created', $result['phrase']);
  49. }
  50. public function testVisitsLocationsForJsonResponse()
  51. {
  52. $parser = OperationResponseParser::getInstance();
  53. $operation = $this->getDescription()->getOperation('test');
  54. $op = new OperationCommand(array(), $operation);
  55. $op->setResponseParser($parser)->setClient(new Client());
  56. $op->prepare()->setResponse(new Response(200, array(
  57. 'Content-Type' => 'application/json'
  58. ), '{"baz":"bar","enigma":"123"}'), true);
  59. $result = $op->execute();
  60. $this->assertEquals(array(
  61. 'baz' => 'bar',
  62. 'enigma' => '123',
  63. 'code' => 200,
  64. 'phrase' => 'OK'
  65. ), $result->toArray());
  66. }
  67. public function testSkipsUnkownModels()
  68. {
  69. $parser = OperationResponseParser::getInstance();
  70. $operation = $this->getDescription()->getOperation('test');
  71. $operation->setResponseClass('Baz')->setResponseType('model');
  72. $op = new OperationCommand(array(), $operation);
  73. $op->setResponseParser($parser)->setClient(new Client());
  74. $op->prepare()->setResponse(new Response(201), true);
  75. $this->assertInstanceOf('Guzzle\Http\Message\Response', $op->execute());
  76. }
  77. public function testAllowsModelProcessingToBeDisabled()
  78. {
  79. $parser = OperationResponseParser::getInstance();
  80. $operation = $this->getDescription()->getOperation('test');
  81. $op = new OperationCommand(array('command.response_processing' => 'native'), $operation);
  82. $op->setResponseParser($parser)->setClient(new Client());
  83. $op->prepare()->setResponse(new Response(200, array(
  84. 'Content-Type' => 'application/json'
  85. ), '{"baz":"bar","enigma":"123"}'), true);
  86. $result = $op->execute();
  87. $this->assertInstanceOf('Guzzle\Service\Resource\Model', $result);
  88. $this->assertEquals(array(
  89. 'baz' => 'bar',
  90. 'enigma' => '123'
  91. ), $result->toArray());
  92. }
  93. public function testCanInjectModelSchemaIntoModels()
  94. {
  95. $parser = new OperationResponseParser(VisitorFlyweight::getInstance(), true);
  96. $desc = $this->getDescription();
  97. $operation = $desc->getOperation('test');
  98. $op = new OperationCommand(array(), $operation);
  99. $op->setResponseParser($parser)->setClient(new Client());
  100. $op->prepare()->setResponse(new Response(200, array(
  101. 'Content-Type' => 'application/json'
  102. ), '{"baz":"bar","enigma":"123"}'), true);
  103. $result = $op->execute();
  104. $this->assertSame($result->getStructure(), $desc->getModel('Foo'));
  105. }
  106. public function testDoesNotParseXmlWhenNotUsingXmlVisitor()
  107. {
  108. $parser = OperationResponseParser::getInstance();
  109. $description = ServiceDescription::factory(array(
  110. 'operations' => array('test' => array('responseClass' => 'Foo')),
  111. 'models' => array(
  112. 'Foo' => array(
  113. 'type' => 'object',
  114. 'properties' => array('baz' => array('location' => 'body'))
  115. )
  116. )
  117. ));
  118. $operation = $description->getOperation('test');
  119. $op = new OperationCommand(array(), $operation);
  120. $op->setResponseParser($parser)->setClient(new Client());
  121. $brokenXml = '<broken><><><<xml>>>>>';
  122. $op->prepare()->setResponse(new Response(200, array(
  123. 'Content-Type' => 'application/xml'
  124. ), $brokenXml), true);
  125. $result = $op->execute();
  126. $this->assertEquals(array('baz'), $result->getKeys());
  127. $this->assertEquals($brokenXml, (string) $result['baz']);
  128. }
  129. public function testVisitsAdditionalProperties()
  130. {
  131. $parser = OperationResponseParser::getInstance();
  132. $description = ServiceDescription::factory(array(
  133. 'operations' => array('test' => array('responseClass' => 'Foo')),
  134. 'models' => array(
  135. 'Foo' => array(
  136. 'type' => 'object',
  137. 'properties' => array(
  138. 'code' => array('location' => 'statusCode')
  139. ),
  140. 'additionalProperties' => array(
  141. 'location' => 'json',
  142. 'type' => 'object',
  143. 'properties' => array(
  144. 'a' => array(
  145. 'type' => 'string',
  146. 'filters' => 'strtoupper'
  147. )
  148. )
  149. )
  150. )
  151. )
  152. ));
  153. $operation = $description->getOperation('test');
  154. $op = new OperationCommand(array(), $operation);
  155. $op->setResponseParser($parser)->setClient(new Client());
  156. $json = '[{"a":"test"},{"a":"baz"}]';
  157. $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), $json), true);
  158. $result = $op->execute()->toArray();
  159. $this->assertEquals(array(
  160. 'code' => 200,
  161. array('a' => 'TEST'),
  162. array('a' => 'BAZ')
  163. ), $result);
  164. }
  165. /**
  166. * @group issue-399
  167. * @link https://github.com/guzzle/guzzle/issues/399
  168. */
  169. public function testAdditionalPropertiesDisabledDiscardsData()
  170. {
  171. $parser = OperationResponseParser::getInstance();
  172. $description = ServiceDescription::factory(array(
  173. 'operations' => array('test' => array('responseClass' => 'Foo')),
  174. 'models' => array(
  175. 'Foo' => array(
  176. 'type' => 'object',
  177. 'additionalProperties' => false,
  178. 'properties' => array(
  179. 'name' => array(
  180. 'location' => 'json',
  181. 'type' => 'string',
  182. ),
  183. 'nested' => array(
  184. 'location' => 'json',
  185. 'type' => 'object',
  186. 'additionalProperties' => false,
  187. 'properties' => array(
  188. 'width' => array(
  189. 'type' => 'integer'
  190. )
  191. ),
  192. ),
  193. 'code' => array('location' => 'statusCode')
  194. ),
  195. )
  196. )
  197. ));
  198. $operation = $description->getOperation('test');
  199. $op = new OperationCommand(array(), $operation);
  200. $op->setResponseParser($parser)->setClient(new Client());
  201. $json = '{"name":"test", "volume":2.0, "nested":{"width":10,"bogus":1}}';
  202. $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), $json), true);
  203. $result = $op->execute()->toArray();
  204. $this->assertEquals(array(
  205. 'name' => 'test',
  206. 'nested' => array(
  207. 'width' => 10,
  208. ),
  209. 'code' => 200
  210. ), $result);
  211. }
  212. public function testCreatesCustomResponseClassInterface()
  213. {
  214. $parser = OperationResponseParser::getInstance();
  215. $description = ServiceDescription::factory(array(
  216. 'operations' => array('test' => array('responseClass' => 'Guzzle\Tests\Mock\CustomResponseModel'))
  217. ));
  218. $operation = $description->getOperation('test');
  219. $op = new OperationCommand(array(), $operation);
  220. $op->setResponseParser($parser)->setClient(new Client());
  221. $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), 'hi!'), true);
  222. $result = $op->execute();
  223. $this->assertInstanceOf('Guzzle\Tests\Mock\CustomResponseModel', $result);
  224. $this->assertSame($op, $result->command);
  225. }
  226. /**
  227. * @expectedException \Guzzle\Service\Exception\ResponseClassException
  228. * @expectedExceptionMessage must exist
  229. */
  230. public function testEnsuresResponseClassExists()
  231. {
  232. $parser = OperationResponseParser::getInstance();
  233. $description = ServiceDescription::factory(array(
  234. 'operations' => array('test' => array('responseClass' => 'Foo\Baz\Bar'))
  235. ));
  236. $operation = $description->getOperation('test');
  237. $op = new OperationCommand(array(), $operation);
  238. $op->setResponseParser($parser)->setClient(new Client());
  239. $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), 'hi!'), true);
  240. $op->execute();
  241. }
  242. /**
  243. * @expectedException \Guzzle\Service\Exception\ResponseClassException
  244. * @expectedExceptionMessage and implement
  245. */
  246. public function testEnsuresResponseClassImplementsResponseClassInterface()
  247. {
  248. $parser = OperationResponseParser::getInstance();
  249. $description = ServiceDescription::factory(array(
  250. 'operations' => array('test' => array('responseClass' => __CLASS__))
  251. ));
  252. $operation = $description->getOperation('test');
  253. $op = new OperationCommand(array(), $operation);
  254. $op->setResponseParser($parser)->setClient(new Client());
  255. $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), 'hi!'), true);
  256. $op->execute();
  257. }
  258. protected function getDescription()
  259. {
  260. return ServiceDescription::factory(array(
  261. 'operations' => array('test' => array('responseClass' => 'Foo')),
  262. 'models' => array(
  263. 'Foo' => array(
  264. 'type' => 'object',
  265. 'properties' => array(
  266. 'baz' => array('type' => 'string', 'location' => 'json'),
  267. 'code' => array('location' => 'statusCode'),
  268. 'phrase' => array('location' => 'reasonPhrase'),
  269. )
  270. )
  271. )
  272. ));
  273. }
  274. public function testCanAddListenerToParseDomainObjects()
  275. {
  276. $client = new Client();
  277. $client->setDescription(ServiceDescription::factory(array(
  278. 'operations' => array('test' => array('responseClass' => 'FooBazBar'))
  279. )));
  280. $foo = new \stdClass();
  281. $client->getEventDispatcher()->addListener('command.parse_response', function ($e) use ($foo) {
  282. $e['result'] = $foo;
  283. });
  284. $command = $client->getCommand('test');
  285. $command->prepare()->setResponse(new Response(200), true);
  286. $result = $command->execute();
  287. $this->assertSame($result, $foo);
  288. }
  289. /**
  290. * @group issue-399
  291. * @link https://github.com/guzzle/guzzle/issues/501
  292. */
  293. public function testAdditionalPropertiesWithRefAreResolved()
  294. {
  295. $parser = OperationResponseParser::getInstance();
  296. $description = ServiceDescription::factory(array(
  297. 'operations' => array('test' => array('responseClass' => 'Foo')),
  298. 'models' => array(
  299. 'Baz' => array('type' => 'string'),
  300. 'Foo' => array(
  301. 'type' => 'object',
  302. 'additionalProperties' => array('$ref' => 'Baz', 'location' => 'json')
  303. )
  304. )
  305. ));
  306. $operation = $description->getOperation('test');
  307. $op = new OperationCommand(array(), $operation);
  308. $op->setResponseParser($parser)->setClient(new Client());
  309. $json = '{"a":"a","b":"b","c":"c"}';
  310. $op->prepare()->setResponse(new Response(200, array('Content-Type' => 'application/json'), $json), true);
  311. $result = $op->execute()->toArray();
  312. $this->assertEquals(array('a' => 'a', 'b' => 'b', 'c' => 'c'), $result);
  313. }
  314. }