PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/iti-note-rest-client-php/vendor/guzzle/guzzle/tests/Guzzle/Tests/Service/Command/CommandTest.php

https://bitbucket.org/alessandro-aglietti/itis-leonardo-da-vinci
PHP | 420 lines | 340 code | 50 blank | 30 comment | 0 complexity | 0f045d0ec3d207900d3f8a732f19d9fd MD5 | raw file
  1. <?php
  2. namespace Guzzle\Tests\Service\Command;
  3. use Guzzle\Plugin\Mock\MockPlugin;
  4. use Guzzle\Http\EntityBody;
  5. use Guzzle\Http\Message\Response;
  6. use Guzzle\Service\Client;
  7. use Guzzle\Service\Command\AbstractCommand;
  8. use Guzzle\Service\Description\Operation;
  9. use Guzzle\Service\Description\Parameter;
  10. use Guzzle\Service\Description\SchemaValidator;
  11. use Guzzle\Service\Description\ServiceDescription;
  12. use Guzzle\Tests\Service\Mock\Command\MockCommand;
  13. use Guzzle\Tests\Service\Mock\Command\Sub\Sub;
  14. /**
  15. * @covers Guzzle\Service\Command\AbstractCommand
  16. */
  17. class CommandTest extends AbstractCommandTest
  18. {
  19. public function testConstructorAddsDefaultParams()
  20. {
  21. $command = new MockCommand();
  22. $this->assertEquals('123', $command->get('test'));
  23. $this->assertFalse($command->isPrepared());
  24. $this->assertFalse($command->isExecuted());
  25. }
  26. public function testDeterminesShortName()
  27. {
  28. $api = new Operation(array('name' => 'foobar'));
  29. $command = new MockCommand(array(), $api);
  30. $this->assertEquals('foobar', $command->getName());
  31. $command = new MockCommand();
  32. $this->assertEquals('mock_command', $command->getName());
  33. $command = new Sub();
  34. $this->assertEquals('sub.sub', $command->getName());
  35. }
  36. /**
  37. * @expectedException RuntimeException
  38. */
  39. public function testGetRequestThrowsExceptionBeforePreparation()
  40. {
  41. $command = new MockCommand();
  42. $command->getRequest();
  43. }
  44. public function testGetResponseExecutesCommandsWhenNeeded()
  45. {
  46. $response = new Response(200);
  47. $client = $this->getClient();
  48. $this->setMockResponse($client, array($response));
  49. $command = new MockCommand();
  50. $command->setClient($client);
  51. $this->assertSame($response, $command->getResponse());
  52. $this->assertSame($response, $command->getResponse());
  53. }
  54. public function testGetResultExecutesCommandsWhenNeeded()
  55. {
  56. $response = new Response(200);
  57. $client = $this->getClient();
  58. $this->setMockResponse($client, array($response));
  59. $command = new MockCommand();
  60. $command->setClient($client);
  61. $this->assertSame($response, $command->getResult());
  62. $this->assertSame($response, $command->getResult());
  63. }
  64. public function testSetClient()
  65. {
  66. $command = new MockCommand();
  67. $client = $this->getClient();
  68. $command->setClient($client);
  69. $this->assertEquals($client, $command->getClient());
  70. unset($client);
  71. unset($command);
  72. $command = new MockCommand();
  73. $client = $this->getClient();
  74. $command->setClient($client)->prepare();
  75. $this->assertEquals($client, $command->getClient());
  76. $this->assertTrue($command->isPrepared());
  77. }
  78. public function testExecute()
  79. {
  80. $client = $this->getClient();
  81. $response = new Response(200, array(
  82. 'Content-Type' => 'application/xml'
  83. ), '<xml><data>123</data></xml>');
  84. $this->setMockResponse($client, array($response));
  85. $command = new MockCommand();
  86. $this->assertSame($command, $command->setClient($client));
  87. // Returns the result of the command
  88. $this->assertInstanceOf('SimpleXMLElement', $command->execute());
  89. $this->assertTrue($command->isPrepared());
  90. $this->assertTrue($command->isExecuted());
  91. $this->assertSame($response, $command->getResponse());
  92. $this->assertInstanceOf('Guzzle\\Http\\Message\\Request', $command->getRequest());
  93. // Make sure that the result was automatically set to a SimpleXMLElement
  94. $this->assertInstanceOf('SimpleXMLElement', $command->getResult());
  95. $this->assertEquals('123', (string) $command->getResult()->data);
  96. }
  97. public function testConvertsJsonResponsesToArray()
  98. {
  99. $client = $this->getClient();
  100. $this->setMockResponse($client, array(
  101. new \Guzzle\Http\Message\Response(200, array(
  102. 'Content-Type' => 'application/json'
  103. ), '{ "key": "Hi!" }'
  104. )
  105. ));
  106. $command = new MockCommand();
  107. $command->setClient($client);
  108. $command->execute();
  109. $this->assertEquals(array(
  110. 'key' => 'Hi!'
  111. ), $command->getResult());
  112. }
  113. /**
  114. * @expectedException \Guzzle\Common\Exception\RuntimeException
  115. */
  116. public function testConvertsInvalidJsonResponsesToArray()
  117. {
  118. $client = $this->getClient();
  119. $this->setMockResponse($client, array(
  120. new \Guzzle\Http\Message\Response(200, array(
  121. 'Content-Type' => 'application/json'
  122. ), '{ "key": "Hi!" }invalid'
  123. )
  124. ));
  125. $command = new MockCommand();
  126. $command->setClient($client);
  127. $command->execute();
  128. }
  129. public function testProcessResponseIsNotXml()
  130. {
  131. $client = $this->getClient();
  132. $this->setMockResponse($client, array(
  133. new Response(200, array(
  134. 'Content-Type' => 'application/octet-stream'
  135. ), 'abc,def,ghi')
  136. ));
  137. $command = new MockCommand();
  138. $client->execute($command);
  139. // Make sure that the result was not converted to XML
  140. $this->assertFalse($command->getResult() instanceof \SimpleXMLElement);
  141. }
  142. /**
  143. * @expectedException RuntimeException
  144. */
  145. public function testExecuteThrowsExceptionWhenNoClientIsSet()
  146. {
  147. $command = new MockCommand();
  148. $command->execute();
  149. }
  150. /**
  151. * @expectedException RuntimeException
  152. */
  153. public function testPrepareThrowsExceptionWhenNoClientIsSet()
  154. {
  155. $command = new MockCommand();
  156. $command->prepare();
  157. }
  158. public function testCommandsAllowsCustomRequestHeaders()
  159. {
  160. $command = new MockCommand();
  161. $command->getRequestHeaders()->set('test', '123');
  162. $this->assertInstanceOf('Guzzle\Common\Collection', $command->getRequestHeaders());
  163. $this->assertEquals('123', $command->getRequestHeaders()->get('test'));
  164. $command->setClient($this->getClient())->prepare();
  165. $this->assertEquals('123', (string) $command->getRequest()->getHeader('test'));
  166. }
  167. public function testCommandsAllowsCustomRequestHeadersAsArray()
  168. {
  169. $command = new MockCommand(array(AbstractCommand::HEADERS_OPTION => array('Foo' => 'Bar')));
  170. $this->assertInstanceOf('Guzzle\Common\Collection', $command->getRequestHeaders());
  171. $this->assertEquals('Bar', $command->getRequestHeaders()->get('Foo'));
  172. }
  173. private function getOperation()
  174. {
  175. return new Operation(array(
  176. 'name' => 'foobar',
  177. 'httpMethod' => 'POST',
  178. 'class' => 'Guzzle\\Tests\\Service\\Mock\\Command\\MockCommand',
  179. 'parameters' => array(
  180. 'test' => array(
  181. 'default' => '123',
  182. 'type' => 'string'
  183. )
  184. )));
  185. }
  186. public function testCommandsUsesOperation()
  187. {
  188. $api = $this->getOperation();
  189. $command = new MockCommand(array(), $api);
  190. $this->assertSame($api, $command->getOperation());
  191. $command->setClient($this->getClient())->prepare();
  192. $this->assertEquals('123', $command->get('test'));
  193. $this->assertSame($api, $command->getOperation($api));
  194. }
  195. public function testCloneMakesNewRequest()
  196. {
  197. $client = $this->getClient();
  198. $command = new MockCommand(array(), $this->getOperation());
  199. $command->setClient($client);
  200. $command->prepare();
  201. $this->assertTrue($command->isPrepared());
  202. $command2 = clone $command;
  203. $this->assertFalse($command2->isPrepared());
  204. }
  205. public function testHasOnCompleteMethod()
  206. {
  207. $that = $this;
  208. $called = 0;
  209. $testFunction = function($command) use (&$called, $that) {
  210. $called++;
  211. $that->assertInstanceOf('Guzzle\Service\Command\CommandInterface', $command);
  212. };
  213. $client = $this->getClient();
  214. $command = new MockCommand(array(
  215. 'command.on_complete' => $testFunction
  216. ), $this->getOperation());
  217. $command->setClient($client);
  218. $command->prepare()->setResponse(new Response(200), true);
  219. $command->execute();
  220. $this->assertEquals(1, $called);
  221. }
  222. /**
  223. * @expectedException \Guzzle\Common\Exception\InvalidArgumentException
  224. */
  225. public function testOnCompleteMustBeCallable()
  226. {
  227. $client = $this->getClient();
  228. $command = new MockCommand();
  229. $command->setOnComplete('foo');
  230. }
  231. public function testCanSetResultManually()
  232. {
  233. $client = $this->getClient();
  234. $client->getEventDispatcher()->addSubscriber(new MockPlugin(array(
  235. new Response(200)
  236. )));
  237. $command = new MockCommand();
  238. $client->execute($command);
  239. $command->setResult('foo!');
  240. $this->assertEquals('foo!', $command->getResult());
  241. }
  242. public function testCanInitConfig()
  243. {
  244. $command = $this->getMockBuilder('Guzzle\\Service\\Command\\AbstractCommand')
  245. ->setConstructorArgs(array(array(
  246. 'foo' => 'bar'
  247. ), new Operation(array(
  248. 'parameters' => array(
  249. 'baz' => new Parameter(array(
  250. 'default' => 'baaar'
  251. ))
  252. )
  253. ))))
  254. ->getMockForAbstractClass();
  255. $this->assertEquals('bar', $command['foo']);
  256. $this->assertEquals('baaar', $command['baz']);
  257. }
  258. public function testAddsCurlOptionsToRequestsWhenPreparing()
  259. {
  260. $command = new MockCommand(array(
  261. 'foo' => 'bar',
  262. 'curl.options' => array('CURLOPT_PROXYPORT' => 8080)
  263. ));
  264. $client = new Client();
  265. $command->setClient($client);
  266. $request = $command->prepare();
  267. $this->assertEquals(8080, $request->getCurlOptions()->get(CURLOPT_PROXYPORT));
  268. }
  269. public function testIsInvokable()
  270. {
  271. $client = $this->getClient();
  272. $response = new Response(200);
  273. $this->setMockResponse($client, array($response));
  274. $command = new MockCommand();
  275. $command->setClient($client);
  276. // Returns the result of the command
  277. $this->assertSame($response, $command());
  278. }
  279. public function testCreatesDefaultOperation()
  280. {
  281. $command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')->getMockForAbstractClass();
  282. $this->assertInstanceOf('Guzzle\Service\Description\Operation', $command->getOperation());
  283. }
  284. public function testAllowsValidatorToBeInjected()
  285. {
  286. $command = $this->getMockBuilder('Guzzle\Service\Command\AbstractCommand')->getMockForAbstractClass();
  287. $v = new SchemaValidator();
  288. $command->setValidator($v);
  289. $this->assertSame($v, $this->readAttribute($command, 'validator'));
  290. }
  291. public function testCanDisableValidation()
  292. {
  293. $command = new MockCommand();
  294. $command->setClient(new \Guzzle\Service\Client());
  295. $v = $this->getMockBuilder('Guzzle\Service\Description\SchemaValidator')
  296. ->setMethods(array('validate'))
  297. ->getMock();
  298. $v->expects($this->never())->method('validate');
  299. $command->setValidator($v);
  300. $command->set(AbstractCommand::DISABLE_VALIDATION, true);
  301. $command->prepare();
  302. }
  303. public function testValidatorDoesNotUpdateNonDefaultValues()
  304. {
  305. $command = new MockCommand(array('test' => 123, 'foo' => 'bar'));
  306. $command->setClient(new \Guzzle\Service\Client());
  307. $command->prepare();
  308. $this->assertEquals(123, $command->get('test'));
  309. $this->assertEquals('bar', $command->get('foo'));
  310. }
  311. public function testValidatorUpdatesDefaultValues()
  312. {
  313. $command = new MockCommand();
  314. $command->setClient(new \Guzzle\Service\Client());
  315. $command->prepare();
  316. $this->assertEquals(123, $command->get('test'));
  317. $this->assertEquals('abc', $command->get('_internal'));
  318. }
  319. /**
  320. * @expectedException \Guzzle\Service\Exception\ValidationException
  321. * @expectedExceptionMessage [Foo] Baz
  322. */
  323. public function testValidatesCommandBeforeSending()
  324. {
  325. $command = new MockCommand();
  326. $command->setClient(new \Guzzle\Service\Client());
  327. $v = $this->getMockBuilder('Guzzle\Service\Description\SchemaValidator')
  328. ->setMethods(array('validate', 'getErrors'))
  329. ->getMock();
  330. $v->expects($this->any())->method('validate')->will($this->returnValue(false));
  331. $v->expects($this->any())->method('getErrors')->will($this->returnValue(array('[Foo] Baz', '[Bar] Boo')));
  332. $command->setValidator($v);
  333. $command->prepare();
  334. }
  335. /**
  336. * @expectedException \Guzzle\Service\Exception\ValidationException
  337. * @expectedExceptionMessage Validation errors: [abc] must be of type string
  338. */
  339. public function testValidatesAdditionalParameters()
  340. {
  341. $description = ServiceDescription::factory(array(
  342. 'operations' => array(
  343. 'foo' => array(
  344. 'parameters' => array(
  345. 'baz' => array('type' => 'integer')
  346. ),
  347. 'additionalParameters' => array(
  348. 'type' => 'string'
  349. )
  350. )
  351. )
  352. ));
  353. $client = new Client();
  354. $client->setDescription($description);
  355. $command = $client->getCommand('foo', array(
  356. 'abc' => false,
  357. 'command.headers' => array('foo' => 'bar')
  358. ));
  359. $command->prepare();
  360. }
  361. public function testCanChangeResponseBody()
  362. {
  363. $body = EntityBody::factory();
  364. $command = new MockCommand();
  365. $command->setClient(new \Guzzle\Service\Client());
  366. $command->set(AbstractCommand::RESPONSE_BODY, $body);
  367. $request = $command->prepare();
  368. $this->assertSame($body, $this->readAttribute($request, 'responseBody'));
  369. }
  370. }