SwaggerTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. /**
  3. * File SwaggerTest.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\Headers;
  11. use Epfremme\Swagger\Entity\Info;
  12. use Epfremme\Swagger\Entity\Path;
  13. use Epfremme\Swagger\Entity\Response;
  14. use Epfremme\Swagger\Entity\Schemas\SchemaInterface;
  15. use Epfremme\Swagger\Entity\SecurityDefinition;
  16. use Epfremme\Swagger\Entity\Swagger;
  17. use Epfremme\Swagger\Entity\Parameters;
  18. use Epfremme\Swagger\Entity\Schemas\AbstractSchema;
  19. use Epfremme\Swagger\Entity\Schemas\ObjectSchema;
  20. use Epfremme\Swagger\Entity\Tag;
  21. use Epfremme\Swagger\Tests\Mixin\SerializerContextTrait;
  22. /**
  23. * Class SwaggerTest
  24. *
  25. * @package Epfremme\Swagger
  26. * @subpackage Tests\Entity
  27. */
  28. class SwaggerTest extends \PHPUnit_Framework_TestCase
  29. {
  30. use SerializerContextTrait;
  31. /**
  32. * @var Swagger
  33. */
  34. protected $swagger;
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function setUp()
  39. {
  40. $this->swagger = new Swagger();
  41. }
  42. /**
  43. * @covers Epfremme\Swagger\Entity\Swagger::getVersion
  44. * @covers Epfremme\Swagger\Entity\Swagger::setVersion
  45. */
  46. public function testVersion()
  47. {
  48. $this->assertClassHasAttribute('version', Swagger::class);
  49. $this->assertInstanceOf(Swagger::class, $this->swagger->setVersion('2.0'));
  50. $this->assertAttributeEquals('2.0', 'version', $this->swagger);
  51. $this->assertEquals('2.0', $this->swagger->getVersion());
  52. }
  53. /**
  54. * @covers Epfremme\Swagger\Entity\Swagger::getInfo
  55. * @covers Epfremme\Swagger\Entity\Swagger::setInfo
  56. */
  57. public function testInfo()
  58. {
  59. $info = new Info();
  60. $this->assertClassHasAttribute('info', Swagger::class);
  61. $this->assertInstanceOf(Swagger::class, $this->swagger->setInfo($info));
  62. $this->assertAttributeInstanceOf(Info::class, 'info', $this->swagger);
  63. $this->assertAttributeEquals($info, 'info', $this->swagger);
  64. $this->assertEquals($info, $this->swagger->getInfo());
  65. }
  66. /**
  67. * @covers Epfremme\Swagger\Entity\Swagger::getHost
  68. * @covers Epfremme\Swagger\Entity\Swagger::setHost
  69. */
  70. public function testHost()
  71. {
  72. $this->assertClassHasAttribute('host', Swagger::class);
  73. $this->assertInstanceOf(Swagger::class, $this->swagger->setHost('foo'));
  74. $this->assertAttributeEquals('foo', 'host', $this->swagger);
  75. $this->assertEquals('foo', $this->swagger->getHost());
  76. }
  77. /**
  78. * @covers Epfremme\Swagger\Entity\Swagger::getBasePath
  79. * @covers Epfremme\Swagger\Entity\Swagger::setBasePath
  80. */
  81. public function testBasePath()
  82. {
  83. $this->assertClassHasAttribute('basePath', Swagger::class);
  84. $this->assertInstanceOf(Swagger::class, $this->swagger->setBasePath('foo'));
  85. $this->assertAttributeEquals('foo', 'basePath', $this->swagger);
  86. $this->assertEquals('foo', $this->swagger->getBasePath());
  87. }
  88. /**
  89. * @covers Epfremme\Swagger\Entity\Swagger::getSchemes
  90. * @covers Epfremme\Swagger\Entity\Swagger::setSchemes
  91. */
  92. public function testSchemes()
  93. {
  94. $schemes = ['foo', 'bar'];
  95. $this->assertClassHasAttribute('schemes', Swagger::class);
  96. $this->assertInstanceOf(Swagger::class, $this->swagger->setSchemes($schemes));
  97. $this->assertAttributeEquals($schemes, 'schemes', $this->swagger);
  98. $this->assertEquals($schemes, $this->swagger->getSchemes());
  99. }
  100. /**
  101. * @covers Epfremme\Swagger\Entity\Swagger::getConsumes
  102. * @covers Epfremme\Swagger\Entity\Swagger::setConsumes
  103. */
  104. public function testConsumes()
  105. {
  106. $consumes = ['foo', 'bar'];
  107. $this->assertClassHasAttribute('consumes', Swagger::class);
  108. $this->assertInstanceOf(Swagger::class, $this->swagger->setConsumes($consumes));
  109. $this->assertAttributeEquals($consumes, 'consumes', $this->swagger);
  110. $this->assertEquals($consumes, $this->swagger->getConsumes());
  111. }
  112. /**
  113. * @covers Epfremme\Swagger\Entity\Swagger::getProduces
  114. * @covers Epfremme\Swagger\Entity\Swagger::setProduces
  115. */
  116. public function testProduces()
  117. {
  118. $produces = ['foo', 'bar'];
  119. $this->assertClassHasAttribute('produces', Swagger::class);
  120. $this->assertInstanceOf(Swagger::class, $this->swagger->setProduces($produces));
  121. $this->assertAttributeEquals($produces, 'produces', $this->swagger);
  122. $this->assertEquals($produces, $this->swagger->getProduces());
  123. }
  124. /**
  125. * @covers Epfremme\Swagger\Entity\Swagger::getPaths
  126. * @covers Epfremme\Swagger\Entity\Swagger::setPaths
  127. */
  128. public function testPaths()
  129. {
  130. $paths = new ArrayCollection([
  131. 'foo' => new Path(),
  132. 'bar' => new Path(),
  133. 'baz' => new Path(),
  134. ]);
  135. $this->assertClassHasAttribute('paths', Swagger::class);
  136. $this->assertInstanceOf(Swagger::class, $this->swagger->setPaths($paths));
  137. $this->assertAttributeInstanceOf(ArrayCollection::class, 'paths', $this->swagger);
  138. $this->assertAttributeEquals($paths, 'paths', $this->swagger);
  139. $this->assertEquals($paths, $this->swagger->getPaths());
  140. $this->assertContainsOnlyInstancesOf(Path::class, $this->swagger->getPaths());
  141. }
  142. /**
  143. * @covers Epfremme\Swagger\Entity\Swagger::getDefinitions
  144. * @covers Epfremme\Swagger\Entity\Swagger::setDefinitions
  145. */
  146. public function testDefinitions()
  147. {
  148. $definitions = new ArrayCollection([
  149. 'name' => new ObjectSchema(),
  150. ]);
  151. $this->assertClassHasAttribute('definitions', Swagger::class);
  152. $this->assertInstanceOf(Swagger::class, $this->swagger->setDefinitions($definitions));
  153. $this->assertAttributeInstanceOf(ArrayCollection::class, 'definitions', $this->swagger);
  154. $this->assertAttributeEquals($definitions, 'definitions', $this->swagger);
  155. $this->assertEquals($definitions, $this->swagger->getDefinitions());
  156. $this->assertContainsOnlyInstancesOf(ObjectSchema::class, $this->swagger->getDefinitions());
  157. }
  158. /**
  159. * @covers Epfremme\Swagger\Entity\Swagger::getParameters
  160. * @covers Epfremme\Swagger\Entity\Swagger::setParameters
  161. */
  162. public function testParameters()
  163. {
  164. $parameters = new ArrayCollection([
  165. 'foo' => new Parameters\FormParameter\StringType(),
  166. 'bar' => new Parameters\FormParameter\IntegerType(),
  167. 'baz' => new Parameters\FormParameter\BooleanType(),
  168. ]);
  169. $this->assertClassHasAttribute('parameters', Swagger::class);
  170. $this->assertInstanceOf(Swagger::class, $this->swagger->setParameters($parameters));
  171. $this->assertAttributeInstanceOf(ArrayCollection::class, 'parameters', $this->swagger);
  172. $this->assertAttributeEquals($parameters, 'parameters', $this->swagger);
  173. $this->assertEquals($parameters, $this->swagger->getParameters());
  174. $this->assertContainsOnlyInstancesOf(Parameters\AbstractTypedParameter::class, $this->swagger->getParameters());
  175. }
  176. /**
  177. * @covers Epfremme\Swagger\Entity\Swagger::getResponses
  178. * @covers Epfremme\Swagger\Entity\Swagger::setResponses
  179. */
  180. public function testResponses()
  181. {
  182. $responses = new ArrayCollection([
  183. 'foo' => new Response(),
  184. 'bar' => new Response(),
  185. ]);
  186. $this->assertClassHasAttribute('responses', Swagger::class);
  187. $this->assertInstanceOf(Swagger::class, $this->swagger->setResponses($responses));
  188. $this->assertAttributeInstanceOf(ArrayCollection::class, 'responses', $this->swagger);
  189. $this->assertAttributeEquals($responses, 'responses', $this->swagger);
  190. $this->assertEquals($responses, $this->swagger->getResponses());
  191. $this->assertContainsOnlyInstancesOf(Response::class, $this->swagger->getResponses());
  192. }
  193. /**
  194. * @covers Epfremme\Swagger\Entity\Swagger::getSecurityDefinitions
  195. * @covers Epfremme\Swagger\Entity\Swagger::setSecurityDefinitions
  196. */
  197. public function testSecurityDefinitions()
  198. {
  199. $securityDefinitions = new ArrayCollection([
  200. 'foo' => new SecurityDefinition(),
  201. 'bar' => new SecurityDefinition(),
  202. ]);
  203. $this->assertClassHasAttribute('securityDefinitions', Swagger::class);
  204. $this->assertInstanceOf(Swagger::class, $this->swagger->setSecurityDefinitions($securityDefinitions));
  205. $this->assertAttributeInstanceOf(ArrayCollection::class, 'securityDefinitions', $this->swagger);
  206. $this->assertAttributeEquals($securityDefinitions, 'securityDefinitions', $this->swagger);
  207. $this->assertEquals($securityDefinitions, $this->swagger->getSecurityDefinitions());
  208. $this->assertContainsOnlyInstancesOf(SecurityDefinition::class, $this->swagger->getSecurityDefinitions());
  209. }
  210. /**
  211. * @covers Epfremme\Swagger\Entity\Swagger::getSecurity
  212. * @covers Epfremme\Swagger\Entity\Swagger::setSecurity
  213. */
  214. public function testSecurity()
  215. {
  216. $security = new ArrayCollection([
  217. 'foo' => ['foo', 'bar'],
  218. 'bar' => ['baz'],
  219. ]);
  220. $this->assertClassHasAttribute('security', Swagger::class);
  221. $this->assertInstanceOf(Swagger::class, $this->swagger->setSecurity($security));
  222. $this->assertAttributeInstanceOf(ArrayCollection::class, 'security', $this->swagger);
  223. $this->assertAttributeEquals($security, 'security', $this->swagger);
  224. $this->assertEquals($security, $this->swagger->getSecurity());
  225. $this->assertContainsOnly('array', $this->swagger->getSecurity());
  226. }
  227. /**
  228. * @covers Epfremme\Swagger\Entity\Swagger::getTags
  229. * @covers Epfremme\Swagger\Entity\Swagger::setTags
  230. */
  231. public function testTags()
  232. {
  233. $tags = new ArrayCollection([
  234. 'foo' => new SecurityDefinition(),
  235. 'bar' => new SecurityDefinition(),
  236. ]);
  237. $this->assertClassHasAttribute('tags', Swagger::class);
  238. $this->assertInstanceOf(Swagger::class, $this->swagger->setTags($tags));
  239. $this->assertAttributeInstanceOf(ArrayCollection::class, 'tags', $this->swagger);
  240. $this->assertAttributeEquals($tags, 'tags', $this->swagger);
  241. $this->assertEquals($tags, $this->swagger->getTags());
  242. $this->assertContainsOnlyInstancesOf(SecurityDefinition::class, $this->swagger->getTags());
  243. }
  244. /**
  245. * @covers Epfremme\Swagger\Entity\Swagger::getExternalDocs
  246. * @covers Epfremme\Swagger\Entity\Swagger::setExternalDocs
  247. */
  248. public function testExternalDocs()
  249. {
  250. $externalDocs = new ExternalDocumentation();
  251. $this->assertClassHasAttribute('externalDocs', Swagger::class);
  252. $this->assertInstanceOf(Swagger::class, $this->swagger->setExternalDocs($externalDocs));
  253. $this->assertAttributeInstanceOf(ExternalDocumentation::class, 'externalDocs', $this->swagger);
  254. $this->assertAttributeEquals($externalDocs, 'externalDocs', $this->swagger);
  255. $this->assertEquals($externalDocs, $this->swagger->getExternalDocs());
  256. }
  257. /**
  258. * @covers Epfremme\Swagger\Entity\Swagger
  259. */
  260. public function testSerialize()
  261. {
  262. $data = json_encode([
  263. 'swagger' => '2.0',
  264. 'info' => [
  265. 'title' => 'foo',
  266. 'description' => 'bar',
  267. 'termsOfService' => 'baz',
  268. 'contact' => (object)[],
  269. 'license' => (object)[],
  270. 'version' => '1.0.0'
  271. ],
  272. 'host' => 'http://www.example.com',
  273. 'basePath' => '/v1',
  274. 'schemes' => ['http', 'https'],
  275. 'consumes' => [
  276. 'application/x-www-form-urlencoded'
  277. ],
  278. 'produces' => [
  279. 'application/json',
  280. 'application/xml'
  281. ],
  282. 'paths' => [
  283. '/test' => [
  284. 'get' => [
  285. 'summary' => 'foo',
  286. 'description' => 'bar',
  287. 'responses' => [
  288. '200' => [
  289. 'description' => 'Pet updated.'
  290. ],
  291. '405' => [
  292. 'description' => 'Invalid input'
  293. ]
  294. ],
  295. 'schemes' => ['http', 'https'],
  296. 'security' => [
  297. [
  298. 'petstore_auth' => [
  299. 'read:pets',
  300. ]
  301. ]
  302. ],
  303. 'deprecated' => false,
  304. ],
  305. 'post' => [
  306. 'tags' => [
  307. 'foo'
  308. ],
  309. 'summary' => 'foo',
  310. 'description' => 'bar',
  311. 'externalDocs' => (object)[],
  312. 'operationId' => 'baz',
  313. 'consumes' => [
  314. 'application/x-www-form-urlencoded'
  315. ],
  316. 'produces' => [
  317. 'application/json',
  318. 'application/xml'
  319. ],
  320. 'parameters' => [
  321. [
  322. 'name' => 'petId',
  323. 'in' => Parameters\AbstractParameter::IN_PATH,
  324. 'description' => 'ID of pet that needs to be updated',
  325. 'required' => true,
  326. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  327. ],
  328. [
  329. 'name' => 'name',
  330. 'in' => Parameters\AbstractParameter::IN_FORM_DATA,
  331. 'description' => 'Updated name of the pet',
  332. 'required' => false,
  333. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  334. ],
  335. [
  336. 'name' => 'status',
  337. 'in' => Parameters\AbstractParameter::IN_FORM_DATA,
  338. 'description' => 'Updated status of the pet',
  339. 'required' => false,
  340. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  341. ]
  342. ],
  343. 'responses' => [
  344. '200' => [
  345. 'description' => 'Pet updated.'
  346. ],
  347. '405' => [
  348. 'description' => 'Invalid input'
  349. ]
  350. ],
  351. 'schemes' => ['http', 'https'],
  352. 'security' => [
  353. [
  354. 'petstore_auth' => [
  355. 'read:pets',
  356. ]
  357. ]
  358. ],
  359. 'deprecated' => true,
  360. ],
  361. ],
  362. ],
  363. 'definitions' => [
  364. 'User' => [
  365. 'type' => AbstractSchema::OBJECT_TYPE,
  366. 'format' => 'foo',
  367. 'title' => 'bar',
  368. 'description' => 'baz',
  369. 'example' => 'qux',
  370. 'externalDocs' => (object)[],
  371. ]
  372. ],
  373. 'parameters' => [
  374. [
  375. 'name' => 'petId',
  376. 'in' => Parameters\AbstractParameter::IN_PATH,
  377. 'description' => 'ID of pet that needs to be updated',
  378. 'required' => true,
  379. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  380. ],
  381. [
  382. 'name' => 'name',
  383. 'in' => Parameters\AbstractParameter::IN_FORM_DATA,
  384. 'description' => 'Updated name of the pet',
  385. 'required' => false,
  386. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  387. ],
  388. [
  389. 'name' => 'status',
  390. 'in' => Parameters\AbstractParameter::IN_FORM_DATA,
  391. 'description' => 'Updated status of the pet',
  392. 'required' => false,
  393. 'type' => Parameters\AbstractTypedParameter::STRING_TYPE
  394. ]
  395. ],
  396. 'responses' => [
  397. '200' => [
  398. 'description' => 'Pet updated.'
  399. ],
  400. '405' => [
  401. 'description' => 'Invalid input'
  402. ]
  403. ],
  404. 'securityDefinitions' => [
  405. 'api_key' => [
  406. 'type' => 'apiKey',
  407. 'name' => 'api_key',
  408. 'in' => 'header'
  409. ],
  410. 'petstore_auth' => [
  411. 'type' => 'oauth2',
  412. 'authorizationUrl' => 'http =>//swagger.io/api/oauth/dialog',
  413. 'flow' => 'implicit',
  414. 'scopes' => [
  415. 'write =>pets' => 'modify pets in your account',
  416. 'read =>pets' => 'read your pets'
  417. ]
  418. ]
  419. ],
  420. 'security' => [
  421. [
  422. 'petstore_auth' => [
  423. 'write:pets',
  424. 'read:pets',
  425. ]
  426. ]
  427. ],
  428. 'tags' => [
  429. '1.0' => [
  430. 'name' => 'foo',
  431. 'description' => 'bar',
  432. 'externalDocs' => (object)[],
  433. ]
  434. ],
  435. 'externalDocs' => [
  436. 'description' => 'foo',
  437. 'url' => 'bar',
  438. ],
  439. ]);
  440. $swagger = $this->getSerializer()->deserialize($data, Swagger::class, 'json');
  441. $this->assertInstanceOf(Swagger::class, $swagger);
  442. $this->assertAttributeInstanceOf(Info::class, 'info', $swagger);
  443. $this->assertAttributeEquals('http://www.example.com', 'host', $swagger);
  444. $this->assertAttributeEquals('/v1', 'basePath', $swagger);
  445. $this->assertAttributeEquals(['http', 'https'], 'schemes', $swagger);
  446. $this->assertAttributeInternalType('array', 'consumes', $swagger);
  447. $this->assertAttributeContainsOnly('string', 'consumes', $swagger);
  448. $this->assertAttributeInternalType('array', 'produces', $swagger);
  449. $this->assertAttributeContainsOnly('string', 'produces', $swagger);
  450. $this->assertAttributeInstanceOf(ArrayCollection::class, 'paths', $swagger);
  451. $this->assertAttributeContainsOnly(Path::class, 'paths', $swagger);
  452. $this->assertAttributeInstanceOf(ArrayCollection::class, 'definitions', $swagger);
  453. $this->assertAttributeContainsOnly(SchemaInterface::class, 'definitions', $swagger);
  454. $this->assertAttributeInstanceOf(ArrayCollection::class, 'parameters', $swagger);
  455. $this->assertAttributeContainsOnly(Parameters\AbstractTypedParameter::class, 'parameters', $swagger);
  456. $this->assertAttributeInstanceOf(ArrayCollection::class, 'responses', $swagger);
  457. $this->assertAttributeContainsOnly(Response::class, 'responses', $swagger);
  458. $this->assertAttributeInstanceOf(ArrayCollection::class, 'securityDefinitions', $swagger);
  459. $this->assertAttributeContainsOnly(SecurityDefinition::class, 'securityDefinitions', $swagger);
  460. $this->assertAttributeInstanceOf(ArrayCollection::class, 'security', $swagger);
  461. $this->assertAttributeContainsOnly('array', 'security', $swagger);
  462. $this->assertAttributeInstanceOf(ArrayCollection::class, 'tags', $swagger);
  463. $this->assertAttributeContainsOnly(Tag::class, 'tags', $swagger);
  464. $this->assertAttributeInstanceOf(ExternalDocumentation::class, 'externalDocs', $swagger);
  465. $json = $this->getSerializer()->serialize($swagger, 'json');
  466. $this->assertJson($json);
  467. $this->assertJsonStringEqualsJsonString($data, $json);
  468. }
  469. }