Skip to content

Commit

Permalink
add openapi test case
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Aug 9, 2017
1 parent dd8e12a commit acb1330
Show file tree
Hide file tree
Showing 10 changed files with 488 additions and 4 deletions.
178 changes: 178 additions & 0 deletions tests/Api/Generator/OpenAPITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

namespace PSX\Project\Tests\Api\Generator;

use PSX\Project\Tests\ApiTestCase;

class OpenAPITest extends ApiTestCase
{
public function testGet()
{
$response = $this->sendRequest('http://127.0.0.1/generator/openapi/*/population/popo', 'GET');

$body = (string) $response->getBody();
$expect = <<<'JSON'
{
"openapi": "3.0.0",
"info": {
"title": "PSX",
"version": "0"
},
"servers": [
{
"url": "http:\/\/127.0.0.1\/"
}
],
"paths": {
"\/population\/popo": {
"get": {
"operationId": "doGet",
"parameters": [
{
"name": "startIndex",
"in": "query",
"required": false,
"schema": {
"type": "integer"
}
},
{
"name": "count",
"in": "query",
"required": false,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "Collection result",
"content": {
"application\/json": {
"schema": {
"$ref": "#\/components\/schemas\/Collection"
}
}
}
}
}
},
"post": {
"operationId": "doPost",
"requestBody": {
"content": {
"application\/json": {
"schema": {
"$ref": "#\/components\/schemas\/Entity"
}
}
}
},
"responses": {
"201": {
"description": "Operation message",
"content": {
"application\/json": {
"schema": {
"$ref": "#\/components\/schemas\/Message"
}
}
}
}
}
},
"parameters": []
}
},
"components": {
"schemas": {
"Collection": {
"type": "object",
"title": "collection",
"description": "Collection result",
"properties": {
"totalResults": {
"type": "integer"
},
"entry": {
"type": "array",
"items": {
"$ref": "#\/components\/schemas\/Entity"
}
}
},
"class": "PSX\\Project\\Tests\\Model\\Collection"
},
"Entity": {
"type": "object",
"title": "entity",
"description": "Represents an internet population entity",
"properties": {
"id": {
"type": "integer",
"description": "Unique id for each entry"
},
"place": {
"type": "integer",
"description": "Position in the top list",
"minimum": 1,
"maximum": 64
},
"region": {
"type": "string",
"description": "Name of the region",
"pattern": "[A-z]+",
"minLength": 3,
"maxLength": 64
},
"population": {
"type": "integer",
"description": "Complete number of population"
},
"users": {
"type": "integer",
"description": "Number of internet users"
},
"worldUsers": {
"type": "number",
"description": "Percentage users of the world"
},
"datetime": {
"type": "string",
"description": "Date when the entity was created",
"format": "date-time"
}
},
"required": [
"place",
"region",
"population",
"users",
"worldUsers"
],
"class": "PSX\\Project\\Tests\\Model\\Entity"
},
"Message": {
"type": "object",
"title": "message",
"description": "Operation message",
"properties": {
"success": {
"type": "boolean"
},
"message": {
"type": "string"
}
},
"class": "PSX\\Project\\Tests\\Model\\Message"
}
}
}
}
JSON;

$this->assertEquals(null, $response->getStatusCode(), $body);
$this->assertJsonStringEqualsJsonString($expect, $body, $body);
}
}
4 changes: 2 additions & 2 deletions tests/Api/Generator/SwaggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testGet()
"paths": {
"\/population\/popo": {
"get": {
"operationId": "getCollection",
"operationId": "doGet",
"parameters": [
{
"name": "startIndex",
Expand All @@ -47,7 +47,7 @@ public function testGet()
}
},
"post": {
"operationId": "postEntity",
"operationId": "doPost",
"parameters": [
{
"description": "Represents an internet population entity",
Expand Down
45 changes: 45 additions & 0 deletions tests/Api/Population/CollectionOpenAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace PSX\Project\Tests\Api\Population;

use PSX\Api\Parser\OpenAPI;
use PSX\Framework\Controller\SchemaApiAbstract;
use PSX\Framework\Loader\Context;

class CollectionOpenAPI extends SchemaApiAbstract
{
/**
* @Inject
* @var \PSX\Project\Tests\Service\Population
*/
protected $populationService;

public function getDocumentation($version = null)
{
return OpenAPI::fromFile(__DIR__ . '/../../Resource/population.json', $this->context->get(Context::KEY_PATH));
}

protected function doGet()
{
return $this->populationService->getAll(
$this->queryParameters->getProperty('startIndex'),
$this->queryParameters->getProperty('count')
);
}

protected function doPost($record)
{
$this->populationService->create(
$record['place'],
$record['region'],
$record['population'],
$record['users'],
$record['worldUsers']
);

return [
'success' => true,
'message' => 'Create population successful',
];
}
}
1 change: 1 addition & 0 deletions tests/Api/Population/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ public function routeDataProvider()
['population/popo'],
['population/jsonschema'],
['population/raml'],
['population/openapi'],
];
}
}
57 changes: 57 additions & 0 deletions tests/Api/Population/EntityOpenAPI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace PSX\Project\Tests\Api\Population;

use PSX\Api\Parser\OpenAPI;
use PSX\Framework\Controller\SchemaApiAbstract;
use PSX\Framework\Loader\Context;

class EntityOpenAPI extends SchemaApiAbstract
{
/**
* @Inject
* @var \PSX\Project\Tests\Service\Population
*/
protected $populationService;

public function getDocumentation($version = null)
{
return OpenAPI::fromFile(__DIR__ . '/../../Resource/population.json', $this->context->get(Context::KEY_PATH));
}

protected function doGet()
{
return $this->populationService->get(
$this->pathParameters['id']
);
}

protected function doPut($record)
{
$this->populationService->update(
$this->pathParameters['id'],
$record['place'],
$record['region'],
$record['population'],
$record['users'],
$record['worldUsers']
);

return [
'success' => true,
'message' => 'Update successful',
];
}

protected function doDelete($record)
{
$this->populationService->delete(
$this->pathParameters['id']
);

return [
'success' => true,
'message' => 'Delete successful',
];
}
}
1 change: 1 addition & 0 deletions tests/Api/Population/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public function routeDataProvider()
['population/popo/:id'],
['population/jsonschema/:id'],
['population/raml/:id'],
['population/openapi/:id'],
];
}
}
24 changes: 24 additions & 0 deletions tests/Api/Tool/DocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ public function testGet()
],
"version": "*"
},
{
"path": "\/population\/openapi",
"methods": [
"GET",
"POST",
"PUT",
"DELETE"
],
"version": "*"
},
{
"path": "\/population\/openapi\/:id",
"methods": [
"GET",
"POST",
"PUT",
"DELETE"
],
"version": "*"
},
{
"path": "\/population",
"methods": [
Expand Down Expand Up @@ -250,6 +270,10 @@ public function testGetDetail()
}
},
"links": [
{
"rel": "openapi",
"href": "\/generator\/openapi\/*\/population\/popo"
},
{
"rel": "swagger",
"href": "\/generator\/swagger\/*\/population\/popo"
Expand Down
27 changes: 27 additions & 0 deletions tests/Api/Tool/RoutingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ public function testGet()
"path": "\/population\/raml\/:id",
"source": "PSX\\Project\\Tests\\Api\\Population\\EntityRaml"
},
{
"methods": [
"GET",
"POST",
"PUT",
"DELETE"
],
"path": "\/population\/openapi",
"source": "PSX\\Project\\Tests\\Api\\Population\\CollectionOpenAPI"
},
{
"methods": [
"GET",
"POST",
"PUT",
"DELETE"
],
"path": "\/population\/openapi\/:id",
"source": "PSX\\Project\\Tests\\Api\\Population\\EntityOpenAPI"
},
{
"methods": [
"GET",
Expand Down Expand Up @@ -143,6 +163,13 @@ public function testGet()
"path": "\/generator\/swagger\/:version\/*path",
"source": "PSX\\Framework\\Controller\\Generator\\SwaggerController"
},
{
"methods": [
"GET"
],
"path": "\/generator\/openapi\/:version\/*path",
"source": "PSX\\Framework\\Controller\\Generator\\OpenAPIController"
},
{
"methods": [
"GET",
Expand Down

0 comments on commit acb1330

Please sign in to comment.