* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class OpenAPITest extends TestCase { public function testModel() { $license = new License(); $license->setName('MIT'); $license->setUrl('https://opensource.org/licenses/MIT'); $contact = new Contact(); $contact->setName('Foo Bar'); $contact->setEmail('foo@bar.com'); $contact->setUrl('http://foo.com'); $info = new Info(); $info->setVersion('1.0.0'); $info->setTitle('Swagger Petstore'); $info->setContact($contact); $info->setLicense($license); $paths = new Paths(); // /pets $parameter = new Parameter(); $parameter->setName('limit'); $parameter->setIn('query'); $parameter->setDescription('How many items to return at one time (max 100)'); $parameter->setRequired(false); $parameter->setSchema(Record::fromArray(['type' => 'integer', 'format' => 'int32'])); $header = new Header(); $header->setDescription('A link to the next page of responses'); $header->setSchema(Record::fromArray(['type' => 'string'])); $headers = new Headers(); $headers['x-next'] = $header; $mediaType = new MediaType(); $mediaType->setSchema(Record::fromArray(['$ref' => '#/components/schemas/Pets'])); $content = new MediaTypes(); $content['application/json'] = $mediaType; $response = new Response(); $response->setDescription('An paged array of pets'); $response->setHeaders($headers); $response->setContent($content); $responses = new Responses(); $responses['200'] = $response; $mediaType = new MediaType(); $mediaType->setSchema(Record::fromArray(['$ref' => '#/components/schemas/Error'])); $content = new MediaTypes(); $content['application/json'] = $mediaType; $errorResponse = new Response(); $errorResponse->setDescription('unexpected error'); $errorResponse->setContent($content); $responses['default'] = $errorResponse; $operation = new Operation(); $operation->setSummary('List all pets'); $operation->setOperationId('listPets'); $operation->setTags(['pets']); $operation->setParameters([$parameter]); $operation->setResponses($responses); $pathItem = new PathItem(); $pathItem->setGet($operation); $response = new Response(); $response->setDescription('Null response'); $responses = new Responses(); $responses['201'] = $response; $responses['default'] = $errorResponse; $operation = new Operation(); $operation->setSummary('Create a pet'); $operation->setOperationId('createPets'); $operation->setTags(['pets']); $operation->setResponses($responses); $pathItem->setPost($operation); $paths['/pets'] = $pathItem; // /pets/{petId} $parameter = new Parameter(); $parameter->setName('petId'); $parameter->setIn('path'); $parameter->setDescription('The id of the pet to retrieve'); $parameter->setRequired(true); $parameter->setSchema(Record::fromArray(['type' => 'string'])); $mediaType = new MediaType(); $mediaType->setSchema(Record::fromArray(['$ref' => '#/components/schemas/Pets'])); $mediaTypes = new MediaTypes(); $mediaTypes['application/json'] = $mediaType; $response = new Response(); $response->setDescription('Expected response to a valid request'); $response->setContent($mediaTypes); $responses = new Responses(); $responses['200'] = $response; $responses['default'] = $errorResponse; $operation = new Operation(); $operation->setSummary('Info for a specific pet'); $operation->setOperationId('showPetById'); $operation->setTags(['pets']); $operation->setParameters([$parameter]); $operation->setResponses($responses); $pathItem = new PathItem(); $pathItem->setGet($operation); $paths['/pets/{petId}'] = $pathItem; $server = new Server(); $server->setUrl('http://petstore.swagger.io/v1'); // schema $schemas = new Schemas(); $schemas['Pet'] = [ 'required' => ['id', 'name'], 'properties' => [ 'id' => ['type' => 'integer', 'format' => 'int64'], 'name' => ['type' => 'string'], 'tag' => ['type' => 'string'], ] ]; $schemas['Pets'] = [ 'type' => 'array', 'items' => ['$ref' => '#/components/schemas/Pet'], ]; $schemas['Error'] = [ 'required' => ['code', 'message'], 'properties' => [ 'code' => ['type' => 'integer', 'format' => 'int32'], 'message' => ['type' => 'string'], ] ]; $components = new Components(); $components->setSchemas($schemas); $tags = []; $tag = new Tag(); $tag->setName('pets'); $tag->setDescription('Pets operations'); $tags[] = $tag; $tag = new Tag(); $tag->setName('bar'); $tag->setDescription('Boo tag'); $tags[] = $tag; $openAPI = new OpenAPI(); $openAPI->setInfo($info); $openAPI->setPaths($paths); $openAPI->setServers([$server]); $openAPI->setComponents($components); $openAPI->setTags($tags); $dumper = new Dumper(); $actual = json_encode($dumper->dump($openAPI), JSON_PRETTY_PRINT); $expect = file_get_contents(__DIR__ . '/resources/openapi.json'); $this->assertJsonStringEqualsJsonString($expect, $actual, $actual); // assert with yaml $expect = json_encode(Yaml::parse(file_get_contents(__DIR__ . '/resources/petstore.yaml'))); $this->assertJsonStringEqualsJsonString($expect, $actual, $actual); } public function testSecurity() { $scopes = new Scopes(); $scopes['foo'] = 'Foo scope'; $scopes['bar'] = 'Bar scope'; $flow = new OAuthFlow(); $flow->setAuthorizationUrl('http://api.phpsx.org/authorization'); $flow->setTokenUrl('http://api.phpsx.org/token'); $flow->setRefreshUrl('http://api.phpsx.org/token/refresh'); $flow->setScopes($scopes); $flows = new OAuthFlows(); $flows->setAuthorizationCode($flow); $scheme = new SecurityScheme(); $scheme->setType('oauth2'); $scheme->setFlows($flows); $paths = new Paths(); $securityReq = new SecurityRequirement(); $securityReq['oauth'] = ['foo', 'bar']; $operation = new Operation(); $operation->setSummary('Info for a specific pet'); $operation->setSecurity([$securityReq]); $pathItem = new PathItem(); $pathItem->setGet($operation); $paths['/pets/{petId}'] = $pathItem; $schemes = new SecuritySchemes(); $schemes['oauth'] = $scheme; $components = new Components(); $components->setSecuritySchemes($schemes); $openAPI = new OpenAPI(); $openAPI->setPaths($paths); $openAPI->setComponents($components); $dumper = new Dumper(); $actual = json_encode($dumper->dump($openAPI), JSON_PRETTY_PRINT); $expect = file_get_contents(__DIR__ . '/resources/openapi_security.json'); $this->assertJsonStringEqualsJsonString($expect, $actual, $actual); } } __halt_compiler();----SIGNATURE:----F/ri84YjtrP2HVUmLHZhGKptSw4BYnoTSxLmpAWlzvqwCdyiYiFXN3Js0RPpupHalqYO82QN5WVqsxwmsnH6RIaXc/wwaTVTqENj7OZbnZVhH0AHQ2VM8ZbqspWXA04nbGQ/KxqdL22uL8Heej0JMOEU4TX1+EID7gGFvtOG5FFKw3ez+6uxsNd7iKaCiuq9B46qL4FtAsyNt7nAmPPhgLbJV6x+9xZPfzun8v5Hnqf9MmnagqG5tnCA4WgLoEKspp/3+N7payMcfuNpOubQtmqBCW24WGIPdGzqrAz89BebgMJbd8a4XilmCgxnSs3jPlU46hJynOvwncfRgq0Cs96AoF2+Xj6/RdSbCKQmFlmIbHnh3xoCWeDyAEiyXFR7T1AcO5CaVXiUfsxM+7ByL/K3RYR+jmxaOy5NXMnCnmm0YOm8WvXzC8+rUN/KnG3i41RQ9OYzJfokTI1f+9T8YAxRYPbczWpS/zhSAmw0U58htGajAkmma4LVE8wZDQLmP9NaXkXjYRJ3QKzh3/bTFxQ9NomzSp2Yqfd8/BZVKaZEpavVsOflkUIrx0mhLCW/CM+bpotBES/UhRh57ulH+WKvdGTQH6JKnE0h0fBXOsNjo0/m+ItNCVh26Fea6spYSSDFCO6FmaL22KMU97uKzy7pVoLEBfltjt7UpJCedAg=----ATTACHMENT:----NTA5Nzc2ODYwMTExNDI1NiA1MTIwMTIzNzU0MzE0MDIgNTc1Njg4NTY3NjU0NDAz