OperationTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace Guzzle\Tests\Service\Description;
  3. use GuzzleHttp\Command\Guzzle\Description;
  4. use GuzzleHttp\Command\Guzzle\Operation;
  5. /**
  6. * @covers \GuzzleHttp\Command\Guzzle\Operation
  7. */
  8. class OperationTest extends \PHPUnit_Framework_TestCase
  9. {
  10. public static function strtoupper($string)
  11. {
  12. return strtoupper($string);
  13. }
  14. public function testOperationIsDataObject()
  15. {
  16. $c = new Operation([
  17. 'name' => 'test',
  18. 'summary' => 'doc',
  19. 'notes' => 'notes',
  20. 'documentationUrl' => 'http://www.example.com',
  21. 'httpMethod' => 'POST',
  22. 'uri' => '/api/v1',
  23. 'responseModel' => 'abc',
  24. 'deprecated' => true,
  25. 'parameters' => [
  26. 'key' => [
  27. 'required' => true,
  28. 'type' => 'string',
  29. 'maxLength' => 10,
  30. 'name' => 'key'
  31. ],
  32. 'key_2' => [
  33. 'required' => true,
  34. 'type' => 'integer',
  35. 'default' => 10,
  36. 'name' => 'key_2'
  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('abc', $c->getResponseModel());
  46. $this->assertTrue($c->getDeprecated());
  47. $params = array_map(function ($c) {
  48. return $c->toArray();
  49. }, $c->getParams());
  50. $this->assertEquals([
  51. 'key' => [
  52. 'required' => true,
  53. 'type' => 'string',
  54. 'maxLength' => 10,
  55. 'name' => 'key'
  56. ],
  57. 'key_2' => [
  58. 'required' => true,
  59. 'type' => 'integer',
  60. 'default' => 10,
  61. 'name' => 'key_2'
  62. ]
  63. ], $params);
  64. $this->assertEquals([
  65. 'required' => true,
  66. 'type' => 'integer',
  67. 'default' => 10,
  68. 'name' => 'key_2'
  69. ], $c->getParam('key_2')->toArray());
  70. $this->assertNull($c->getParam('afefwef'));
  71. $this->assertArrayNotHasKey('parent', $c->getParam('key_2')->toArray());
  72. }
  73. public function testDeterminesIfHasParam()
  74. {
  75. $command = $this->getTestCommand();
  76. $this->assertTrue($command->hasParam('data'));
  77. $this->assertFalse($command->hasParam('baz'));
  78. }
  79. protected function getTestCommand()
  80. {
  81. return new Operation([
  82. 'parameters' => [
  83. 'data' => ['type' => 'string']
  84. ]
  85. ]);
  86. }
  87. public function testAddsNameToParametersIfNeeded()
  88. {
  89. $command = new Operation(['parameters' => ['foo' => []]]);
  90. $this->assertEquals('foo', $command->getParam('foo')->getName());
  91. }
  92. public function testContainsApiErrorInformation()
  93. {
  94. $command = $this->getOperation();
  95. $this->assertEquals(1, count($command->getErrorResponses()));
  96. }
  97. public function testHasNotes()
  98. {
  99. $o = new Operation(['notes' => 'foo']);
  100. $this->assertEquals('foo', $o->getNotes());
  101. }
  102. public function testHasData()
  103. {
  104. $o = new Operation(['data' => ['foo' => 'baz', 'bar' => 123]]);
  105. $this->assertEquals('baz', $o->getData('foo'));
  106. $this->assertEquals(123, $o->getData('bar'));
  107. $this->assertNull($o->getData('wfefwe'));
  108. $this->assertEquals(['foo' => 'baz', 'bar' => 123], $o->getData());
  109. }
  110. /**
  111. * @expectedException \InvalidArgumentException
  112. * @expectedExceptionMesssage Parameters must be arrays
  113. */
  114. public function testEnsuresParametersAreArrays()
  115. {
  116. new Operation(['parameters' => ['foo' => true]]);
  117. }
  118. public function testHasDescription()
  119. {
  120. $s = new Description([]);
  121. $o = new Operation([], $s);
  122. $this->assertSame($s, $o->getServiceDescription());
  123. }
  124. public function testHasAdditionalParameters()
  125. {
  126. $o = new Operation([
  127. 'additionalParameters' => [
  128. 'type' => 'string', 'name' => 'binks',
  129. ],
  130. 'parameters' => [
  131. 'foo' => ['type' => 'integer'],
  132. ],
  133. ]);
  134. $this->assertEquals('string', $o->getAdditionalParameters()->getType());
  135. }
  136. /**
  137. * @return Operation
  138. */
  139. protected function getOperation()
  140. {
  141. return new Operation([
  142. 'name' => 'OperationTest',
  143. 'class' => get_class($this),
  144. 'parameters' => [
  145. 'test' => ['type' => 'object'],
  146. 'bool_1' => ['default' => true, 'type' => 'boolean'],
  147. 'bool_2' => ['default' => false],
  148. 'float' => ['type' => 'numeric'],
  149. 'int' => ['type' => 'integer'],
  150. 'date' => ['type' => 'string'],
  151. 'timestamp' => ['type' => 'string'],
  152. 'string' => ['type' => 'string'],
  153. 'username' => ['type' => 'string', 'required' => true, 'filters' => 'strtolower'],
  154. 'test_function' => ['type' => 'string', 'filters' => __CLASS__ . '::strtoupper'],
  155. ],
  156. 'errorResponses' => [
  157. [
  158. 'code' => 503,
  159. 'reason' => 'InsufficientCapacity',
  160. 'class' => 'Guzzle\\Exception\\RuntimeException',
  161. ],
  162. ],
  163. ]);
  164. }
  165. public function testCanExtendFromOtherOperations()
  166. {
  167. $d = new Description([
  168. 'operations' => [
  169. 'A' => [
  170. 'parameters' => [
  171. 'A' => [
  172. 'type' => 'object',
  173. 'properties' => ['foo' => ['type' => 'string']]
  174. ],
  175. 'B' => ['type' => 'string']
  176. ],
  177. 'summary' => 'foo'
  178. ],
  179. 'B' => [
  180. 'extends' => 'A',
  181. 'summary' => 'Bar'
  182. ],
  183. 'C' => [
  184. 'extends' => 'B',
  185. 'summary' => 'Bar',
  186. 'parameters' => [
  187. 'B' => ['type' => 'number']
  188. ]
  189. ]
  190. ]
  191. ]);
  192. $a = $d->getOperation('A');
  193. $this->assertEquals('foo', $a->getSummary());
  194. $this->assertTrue($a->hasParam('A'));
  195. $this->assertEquals('string', $a->getParam('B')->getType());
  196. $b = $d->getOperation('B');
  197. $this->assertTrue($a->hasParam('A'));
  198. $this->assertEquals('Bar', $b->getSummary());
  199. $this->assertEquals('string', $a->getParam('B')->getType());
  200. $c = $d->getOperation('C');
  201. $this->assertTrue($a->hasParam('A'));
  202. $this->assertEquals('Bar', $c->getSummary());
  203. $this->assertEquals('number', $c->getParam('B')->getType());
  204. }
  205. }