OperationTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * File OperationTest.php
  4. *
  5. * @author Edward Pfremmer <epfremme@nerdery.com>
  6. */
  7. namespace Epfremme\Swagger\Tests\Entity;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Epfremme\Swagger\Entity\ExternalDocumentation;
  10. use Epfremme\Swagger\Entity\Operation;
  11. use Epfremme\Swagger\Entity\Parameters;
  12. use Epfremme\Swagger\Entity\Response;
  13. use Epfremme\Swagger\Tests\Mixin\SerializerContextTrait;
  14. /**
  15. * Class OperationTest
  16. *
  17. * @package Epfremme\Swagger
  18. * @subpackage Tests\Entity
  19. */
  20. class OperationTest extends \PHPUnit_Framework_TestCase
  21. {
  22. use SerializerContextTrait;
  23. /**
  24. * @var Operation
  25. */
  26. protected $operation;
  27. /**
  28. * {@inheritdoc}
  29. */
  30. protected function setUp()
  31. {
  32. $this->operation = new Operation();
  33. }
  34. /**
  35. * @covers Epfremme\Swagger\Entity\Operation::getTags
  36. * @covers Epfremme\Swagger\Entity\Operation::setTags
  37. */
  38. public function testTags()
  39. {
  40. $tags = ['foo', 'bar'];
  41. $this->assertClassHasAttribute('tags', Operation::class);
  42. $this->assertInstanceOf(Operation::class, $this->operation->setTags($tags));
  43. $this->assertAttributeEquals($tags, 'tags', $this->operation);
  44. $this->assertEquals($tags, $this->operation->getTags());
  45. }
  46. /**
  47. * @covers Epfremme\Swagger\Entity\Operation::getSummary
  48. * @covers Epfremme\Swagger\Entity\Operation::setSummary
  49. */
  50. public function testSummary()
  51. {
  52. $this->assertClassHasAttribute('summary', Operation::class);
  53. $this->assertInstanceOf(Operation::class, $this->operation->setSummary('foo'));
  54. $this->assertAttributeEquals('foo', 'summary', $this->operation);
  55. $this->assertEquals('foo', $this->operation->getSummary());
  56. }
  57. /**
  58. * @covers Epfremme\Swagger\Entity\Operation::getDescription
  59. * @covers Epfremme\Swagger\Entity\Operation::setDescription
  60. */
  61. public function testDescription()
  62. {
  63. $this->assertClassHasAttribute('description', Operation::class);
  64. $this->assertInstanceOf(Operation::class, $this->operation->setDescription('foo'));
  65. $this->assertAttributeEquals('foo', 'description', $this->operation);
  66. $this->assertEquals('foo', $this->operation->getDescription());
  67. }
  68. /**
  69. * @covers Epfremme\Swagger\Entity\Operation::getExternalDocs
  70. * @covers Epfremme\Swagger\Entity\Operation::setExternalDocs
  71. */
  72. public function testExternalDocs()
  73. {
  74. $externalDocs = new ExternalDocumentation();
  75. $this->assertClassHasAttribute('externalDocs', Operation::class);
  76. $this->assertInstanceOf(Operation::class, $this->operation->setExternalDocs($externalDocs));
  77. $this->assertAttributeInstanceOf(ExternalDocumentation::class, 'externalDocs', $this->operation);
  78. $this->assertAttributeEquals($externalDocs, 'externalDocs', $this->operation);
  79. $this->assertEquals($externalDocs, $this->operation->getExternalDocs());
  80. }
  81. /**
  82. * @covers Epfremme\Swagger\Entity\Operation::getOperationId
  83. * @covers Epfremme\Swagger\Entity\Operation::setOperationId
  84. */
  85. public function testOperationId()
  86. {
  87. $this->assertClassHasAttribute('operationId', Operation::class);
  88. $this->assertInstanceOf(Operation::class, $this->operation->setOperationId('foo'));
  89. $this->assertAttributeEquals('foo', 'operationId', $this->operation);
  90. $this->assertEquals('foo', $this->operation->getOperationId());
  91. }
  92. /**
  93. * @covers Epfremme\Swagger\Entity\Operation::getConsumes
  94. * @covers Epfremme\Swagger\Entity\Operation::setConsumes
  95. */
  96. public function testConsumes()
  97. {
  98. $consumes = ['foo', 'bar'];
  99. $this->assertClassHasAttribute('consumes', Operation::class);
  100. $this->assertInstanceOf(Operation::class, $this->operation->setConsumes($consumes));
  101. $this->assertAttributeEquals($consumes, 'consumes', $this->operation);
  102. $this->assertEquals($consumes, $this->operation->getConsumes());
  103. }
  104. /**
  105. * @covers Epfremme\Swagger\Entity\Operation::getProduces
  106. * @covers Epfremme\Swagger\Entity\Operation::setProduces
  107. */
  108. public function testProduces()
  109. {
  110. $produces = ['foo', 'bar'];
  111. $this->assertClassHasAttribute('produces', Operation::class);
  112. $this->assertInstanceOf(Operation::class, $this->operation->setProduces($produces));
  113. $this->assertAttributeEquals($produces, 'produces', $this->operation);
  114. $this->assertEquals($produces, $this->operation->getProduces());
  115. }
  116. /**
  117. * @covers Epfremme\Swagger\Entity\Operation::getParameters
  118. * @covers Epfremme\Swagger\Entity\Operation::setParameters
  119. */
  120. public function testParameters()
  121. {
  122. $parameters = new ArrayCollection([
  123. 'foo' => new Parameters\FormParameter\StringType(),
  124. 'bar' => new Parameters\FormParameter\IntegerType(),
  125. 'baz' => new Parameters\FormParameter\BooleanType(),
  126. ]);
  127. $this->assertClassHasAttribute('parameters', Operation::class);
  128. $this->assertInstanceOf(Operation::class, $this->operation->setParameters($parameters));
  129. $this->assertAttributeInstanceOf(ArrayCollection::class, 'parameters', $this->operation);
  130. $this->assertAttributeEquals($parameters, 'parameters', $this->operation);
  131. $this->assertEquals($parameters, $this->operation->getParameters());
  132. $this->assertContainsOnlyInstancesOf(Parameters\AbstractTypedParameter::class, $this->operation->getParameters());
  133. }
  134. /**
  135. * @covers Epfremme\Swagger\Entity\Operation::getResponses
  136. * @covers Epfremme\Swagger\Entity\Operation::setResponses
  137. */
  138. public function testResponses()
  139. {
  140. $responses = new ArrayCollection([
  141. 'foo' => new Response(),
  142. 'bar' => new Response(),
  143. ]);
  144. $this->assertClassHasAttribute('responses', Operation::class);
  145. $this->assertInstanceOf(Operation::class, $this->operation->setResponses($responses));
  146. $this->assertAttributeInstanceOf(ArrayCollection::class, 'responses', $this->operation);
  147. $this->assertAttributeEquals($responses, 'responses', $this->operation);
  148. $this->assertEquals($responses, $this->operation->getResponses());
  149. $this->assertContainsOnlyInstancesOf(Response::class, $this->operation->getResponses());
  150. }
  151. /**
  152. * @covers Epfremme\Swagger\Entity\Operation::getSchemes
  153. * @covers Epfremme\Swagger\Entity\Operation::setSchemes
  154. */
  155. public function testSchemes()
  156. {
  157. $schemes = ['foo', 'bar'];
  158. $this->assertClassHasAttribute('schemes', Operation::class);
  159. $this->assertInstanceOf(Operation::class, $this->operation->setSchemes($schemes));
  160. $this->assertAttributeEquals($schemes, 'schemes', $this->operation);
  161. $this->assertEquals($schemes, $this->operation->getSchemes());
  162. }
  163. /**
  164. * @covers Epfremme\Swagger\Entity\Operation::isDeprecated
  165. * @covers Epfremme\Swagger\Entity\Operation::setDeprecated
  166. */
  167. public function testDeprecated()
  168. {
  169. $this->assertClassHasAttribute('deprecated', Operation::class);
  170. $this->assertInstanceOf(Operation::class, $this->operation->setDeprecated(true));
  171. $this->assertAttributeInternalType('boolean', 'deprecated', $this->operation);
  172. $this->assertAttributeEquals(true, 'deprecated', $this->operation);
  173. $this->assertTrue($this->operation->isDeprecated());
  174. }
  175. /**
  176. * @covers Epfremme\Swagger\Entity\Operation::getSecurity
  177. * @covers Epfremme\Swagger\Entity\Operation::setSecurity
  178. */
  179. public function testSecurity()
  180. {
  181. $security = new ArrayCollection([
  182. 'foo' => ['foo', 'bar'],
  183. 'bar' => ['baz'],
  184. ]);
  185. $this->assertClassHasAttribute('security', Operation::class);
  186. $this->assertInstanceOf(Operation::class, $this->operation->setSecurity($security));
  187. $this->assertAttributeInstanceOf(ArrayCollection::class, 'security', $this->operation);
  188. $this->assertAttributeEquals($security, 'security', $this->operation);
  189. $this->assertEquals($security, $this->operation->getSecurity());
  190. $this->assertContainsOnly('array', $this->operation->getSecurity());
  191. }
  192. /**
  193. * @covers Epfremme\Swagger\Entity\Operation
  194. */
  195. public function testSerialize()
  196. {
  197. $data = json_encode([
  198. 'tags' => [
  199. 'foo'
  200. ],
  201. 'summary' => 'foo',
  202. 'description' => 'bar',
  203. 'externalDocs' => (object)[],
  204. 'operationId' => 'baz',
  205. 'consumes' => [
  206. 'application/x-www-form-urlencoded'
  207. ],
  208. 'produces' => [
  209. 'application/json',
  210. 'application/xml'
  211. ],
  212. 'parameters' => [
  213. [
  214. 'name' => 'petId',
  215. 'in' => Parameters\AbstractParameter::IN_PATH,
  216. 'description' => 'ID of pet that needs to be updated',
  217. 'required' => true,
  218. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  219. ],
  220. [
  221. 'name' => 'name',
  222. 'in' => Parameters\AbstractParameter::IN_FORM_DATA,
  223. 'description' => 'Updated name of the pet',
  224. 'required' => false,
  225. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  226. ],
  227. [
  228. 'name' => 'status',
  229. 'in' => Parameters\AbstractParameter::IN_FORM_DATA,
  230. 'description' => 'Updated status of the pet',
  231. 'required' => false,
  232. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  233. ]
  234. ],
  235. 'responses' => [
  236. '200' => [
  237. 'description' => 'Pet updated.'
  238. ],
  239. '405' => [
  240. 'description' => 'Invalid input'
  241. ]
  242. ],
  243. 'schemes' => ['http', 'https'],
  244. 'security' => [
  245. [
  246. 'petstore_auth' => [
  247. 'write:pets',
  248. 'read:pets',
  249. ]
  250. ]
  251. ],
  252. 'deprecated' => true,
  253. ]);
  254. $operation = $this->getSerializer()->deserialize($data, Operation::class, 'json');
  255. $this->assertInstanceOf(Operation::class, $operation);
  256. $this->assertAttributeEquals(['foo'], 'tags', $operation);
  257. $this->assertAttributeEquals('foo', 'summary', $operation);
  258. $this->assertAttributeEquals('bar', 'description', $operation);
  259. $this->assertAttributeEquals('baz', 'operationId', $operation);
  260. $this->assertAttributeInstanceOf(ExternalDocumentation::class, 'externalDocs', $operation);
  261. $this->assertAttributeInternalType('array', 'consumes', $operation);
  262. $this->assertAttributeContainsOnly('string', 'consumes', $operation);
  263. $this->assertAttributeInternalType('array', 'produces', $operation);
  264. $this->assertAttributeContainsOnly('string', 'produces', $operation);
  265. $this->assertAttributeInstanceOf(ArrayCollection::class, 'parameters', $operation);
  266. $this->assertAttributeContainsOnly(Parameters\AbstractTypedParameter::class, 'parameters', $operation);
  267. $this->assertAttributeInstanceOf(ArrayCollection::class, 'responses', $operation);
  268. $this->assertAttributeContainsOnly(Response::class, 'responses', $operation);
  269. $this->assertAttributeInternalType('array', 'schemes', $operation);
  270. $this->assertAttributeContainsOnly('string', 'schemes', $operation);
  271. $this->assertAttributeInstanceOf(ArrayCollection::class, 'security', $operation);
  272. $this->assertAttributeContainsOnly('array', 'security', $operation);
  273. $this->assertAttributeEquals(true, 'deprecated', $operation);
  274. $json = $this->getSerializer()->serialize($operation, 'json');
  275. $this->assertJson($json);
  276. $this->assertJsonStringEqualsJsonString($data, $json);
  277. }
  278. }