* @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:----QbxozJXF+KFX1mZVC6S4SqXfUaKCHlVCjgJcUUL/WBlfRCkMUXo8UFhxnS8kyoVkhe+LqUwW5vG4p+vp59TLoUPYjmQIZkDLQad/ZO57VlPm+A8J9hYwouNkDmeiqIPYY7b5Zr1d6Ihb2kQmzQLKQB6n+1IjmwKQ9t6ZdnV7QHMLkuRrZr+QxhgAP0wr3U804fAi0+uH4+NXOl4AdjQz3BYOVoDjcTJUrn/pbBP4Xi+5CNAQFUdLEyPTFNz2UBikWJp9nv05/dX+juiV5QS2Qpgu4OnkKfgkIl1ekbmDzH/8YRHo61rVwRE19kqoNoYY5MJpzl49ON6fdPJX94nm55D6LADGAcAQ8XuLy/Pn2zTxA35PAooDZ4k+k9F9bPi9nLqmYQLS2zGGCHaUho5TvlPIoS2+sih6vVRMfni0Sx7St6DYBKrOc3oED+fTFtf2Pvz2oE+xBLAY2tQSiDy5Qg1OzTV2MUl3h2sObKbsM7xDlPV/VWeYFHwqjBj9SP6zqKD5QVXmMD4IXzTb+8N/UI++Wk95Rh+4N9MJJQHT2HNWKO+chkPG+9wwcgwgB/Q8uW956c1V0/kPDJjprXts77oXwoZrzGW70dM7g67ibIdzFhfjvNLRHAB7TbCm7VXGV5yCjc2ojosq98zbL91YJVSdgEmp1NGs+1gHxF1nfm8=----ATTACHMENT:----MzA0MDkxOTc1MTEzNjU2NCA0OTY0NjA2MDMzNzk3Mjg1IDU4MjY0NTM4MzA2NjkyMDk=