SerializerTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. namespace Swaggertests;
  3. use Swagger\Annotations;
  4. use Swagger\Serializer;
  5. class SerializerTest extends SwaggerTestCase
  6. {
  7. private function getExpected()
  8. {
  9. $path = new Annotations\Path([]);
  10. $path->path = '/products';
  11. $path->post = new Annotations\Post([]);
  12. $path->post->tags = ['products'];
  13. $path->post->summary = 's1';
  14. $path->post->description = 'd1';
  15. $path->post->consumes = ['application/json'];
  16. $path->post->produces = ['application/json'];
  17. $param = new Annotations\Parameter([]);
  18. $param->in = 'body';
  19. $param->description = 'data in body';
  20. $param->required = true;
  21. $param->type = 'object';
  22. $param->x = [];
  23. $param->x['repository'] = 'abc';
  24. $path->post->parameters = [$param];
  25. $resp = new Annotations\Response([]);
  26. $resp->response = '200';
  27. $resp->x = [];
  28. $resp->x['repository'] = 'def';
  29. $schema = new Annotations\Schema([]);
  30. $schema->ref = '#/definitions/Pet';
  31. $resp->schema = $schema;
  32. $path->post->responses = [$resp];
  33. $expected = new Annotations\Swagger([]);
  34. $expected->swagger = '2.0';
  35. $expected->paths = [
  36. $path,
  37. ];
  38. $definition = new Annotations\Definition([]);
  39. $definition->definition = 'Pet';
  40. $definition->required = ['name', 'photoUrls'];
  41. $expected->definitions = [$definition];
  42. return $expected;
  43. }
  44. public function testDeserializeAnnotation()
  45. {
  46. $serializer = new Serializer();
  47. $json = <<<JSON
  48. {
  49. "swagger": "2.0",
  50. "paths": {
  51. "/products": {
  52. "post": {
  53. "tags": [
  54. "products"
  55. ],
  56. "summary": "s1",
  57. "description": "d1",
  58. "consumes": [
  59. "application/json"
  60. ],
  61. "produces": [
  62. "application/json"
  63. ],
  64. "parameters": [
  65. {
  66. "in": "body",
  67. "description": "data in body",
  68. "required": true,
  69. "type": "object",
  70. "x-repository": "abc"
  71. }
  72. ],
  73. "responses": {
  74. "200": {
  75. "x-repository": "def",
  76. "schema": {
  77. "\$ref": "#/definitions/Pet"
  78. }
  79. }
  80. }
  81. }
  82. }
  83. },
  84. "definitions": {
  85. "Pet": {
  86. "required": [
  87. "name",
  88. "photoUrls"
  89. ]
  90. }
  91. }
  92. }
  93. JSON;
  94. $annotation = $serializer->deserialize($json, 'Swagger\Annotations\Swagger');
  95. $this->assertInstanceOf('Swagger\Annotations\Swagger', $annotation);
  96. $this->assertJsonStringEqualsJsonString(
  97. $annotation->__toString(),
  98. $this->getExpected()->__toString()
  99. );
  100. }
  101. public function testPetstoreExample()
  102. {
  103. $serializer = new Serializer();
  104. $swagger = $serializer->deserializeFile(__DIR__.'/ExamplesOutput/petstore.swagger.io.json');
  105. $this->assertInstanceOf('Swagger\Annotations\Swagger', $swagger);
  106. $this->assertSwaggerEqualsFile(__DIR__ . '/ExamplesOutput/petstore.swagger.io.json', $swagger);
  107. }
  108. }