Skip to content

Commit

Permalink
Merge pull request #33 from sunrise-php/release/v2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fenric authored Oct 7, 2021
2 parents 6b9158c + 80c7395 commit 5761522
Show file tree
Hide file tree
Showing 125 changed files with 3,959 additions and 3,893 deletions.
65 changes: 40 additions & 25 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,67 @@ jobs:
docker:
- image: circleci/php:7.1-cli-node-browsers
steps:
- run: sudo pecl install apcu
- run: sudo docker-php-ext-enable apcu
- run: php -v
- checkout
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
- run: sudo apt update -y
- run: sudo apt install -y libyaml-dev
- run: sudo pecl channel-update pecl.php.net
- run: sudo pecl install yaml
- run: sudo docker-php-ext-enable yaml
- run: php -v
- run: composer install --no-interaction --no-suggest --prefer-source
- run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text
php72:
docker:
- image: circleci/php:7.2-cli-node-browsers
steps:
- run: sudo pecl install apcu
- run: sudo docker-php-ext-enable apcu
- run: php -v
- checkout
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
- run: sudo apt update -y
- run: sudo apt install -y libyaml-dev
- run: sudo pecl channel-update pecl.php.net
- run: sudo pecl install yaml
- run: sudo docker-php-ext-enable yaml
- run: php -v
- run: composer install --no-interaction --no-suggest --prefer-source
- run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text
php73:
docker:
- image: circleci/php:7.3-cli-node-browsers
steps:
- run: sudo pecl install apcu
- run: sudo docker-php-ext-enable apcu
- run: php -v
- checkout
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
- run: sudo apt update -y
- run: sudo apt install -y libyaml-dev
- run: sudo pecl channel-update pecl.php.net
- run: sudo pecl install yaml
- run: sudo docker-php-ext-enable yaml
- run: php -v
- run: composer install --no-interaction --no-suggest --prefer-source
- run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text
php74:
docker:
- image: circleci/php:7.4-cli-node-browsers
steps:
- run: sudo pecl install apcu
- run: sudo docker-php-ext-enable apcu
- run: php -v
- checkout
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
- run: sudo apt update -y
- run: sudo apt install -y libyaml-dev
- run: sudo pecl channel-update pecl.php.net
- run: sudo pecl install yaml
- run: sudo docker-php-ext-enable yaml
- run: php -v
- run: composer install --no-interaction --no-suggest --prefer-source
- run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text
php80:
docker:
- image: circleci/php:8.0-cli-node-browsers
steps:
- run: sudo pecl install apcu
- run: sudo docker-php-ext-enable apcu
- run: php -v
- checkout
- run: composer install --no-interaction --prefer-source --no-suggest
- run: php vendor/bin/phpunit --colors=always
- run: sudo apt update -y
- run: sudo apt install -y libyaml-dev
- run: sudo pecl channel-update pecl.php.net
- run: sudo pecl install yaml
- run: sudo docker-php-ext-enable yaml
- run: php -v
- run: composer install --no-interaction --no-suggest --prefer-source
- run: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-text
workflows:
version: 2
build:
Expand Down
13 changes: 7 additions & 6 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ build:
environment:
php:
version: '8.0'
pecl_extensions:
- apcu
ini:
'apc.enable_cli': '1'
'xdebug.mode': 'coverage'
nodes:
analysis:
tests:
override:
- php-scrutinizer-run
coverage:
dependencies:
before:
- sudo apt update -y
- sudo apt install -y libyaml-dev
- pecl channel-update pecl.php.net
- pecl install yaml
tests:
override:
- command: php vendor/bin/phpunit --coverage-clover coverage.xml
- command: XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-clover coverage.xml
coverage:
file: coverage.xml
format: clover
159 changes: 155 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## OpenApi (Swagger) Specification support for Sunrise router
## OpenAPI (Swagger) Specification Support for Sunrise Router

[![Build Status](https://circleci.com/gh/sunrise-php/http-router-openapi.svg?style=shield)](https://circleci.com/gh/sunrise-php/http-router-openapi)
[![Code Coverage](https://scrutinizer-ci.com/g/sunrise-php/http-router-openapi/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/sunrise-php/http-router-openapi/?branch=master)
Expand All @@ -9,12 +9,163 @@

---

## Installation via composer
## Installation

```bash
composer require 'sunrise/http-router-openapi'
composer require 'sunrise/http-router-openapi:^2.0'
```

## QuickStart

```php
use Psr\SimpleCache\CacheInterface;
use Sunrise\Http\Router\OpenApi\Object\Info;
use Sunrise\Http\Router\OpenApi\OpenApi;
use Sunrise\Http\Router\Router;

$openapi = new OpenApi(new Info('Acme', '1.0.0'));

// Passing PSR-16 cache to the openapi object:
/** @var CacheInterface $cache */
$openapi->setCache($cache);

// Passing all routes to the openapi object:
/** @var Router $router */
$openapi->addRoute(...$router->getRoutes());
```

#### Building OpenAPI Document

```php
// Converting the openapi object to JSON document:
$openapi->toJson();
// Converting the openapi object to YAML document:
$openapi->toYaml();
// Converting the openapi object to an array
$openapi->toArray();
```

#### Building JSON Schemas

> Converts an operation part to [JSON Schema](https://json-schema.org).
```php
$openapi->getRequestCookieJsonSchema();
$openapi->getRequestHeaderJsonSchema();
$openapi->getRequestQueryJsonSchema();
$openapi->getRequestBodyJsonSchema();
$openapi->getResponseBodyJsonSchema();
```

## PSR-15 Middlewares

#### RequestValidationMiddleware

> Validates a request using a route description.
```php
use Sunrise\Http\Router\OpenApi\Middleware\RequestValidationMiddleware;
use Sunrise\Http\Router\OpenApi\OpenApi;
use Sunrise\Http\Router\Route;

/** @var OpenApi $openapi */
$middleware = new RequestValidationMiddleware($openapi);
```

## Symfony Commands

#### GenerateOpenapiDocumentCommand

> Generates OpenAPI document.
```php
use Sunrise\Http\Router\OpenApi\Command\GenerateOpenapiDocumentCommand;
use Sunrise\Http\Router\OpenApi\OpenApi;

/** @var OpenApi $openapi */
$command = new GenerateOpenapiDocumentCommand($openapi);
```

```bash
php bin/app router:generate-openapi-document --help
```

#### GenerateJsonSchemaCommand

> Generates an operation part to [JSON Schema](https://json-schema.org).
```php
use Sunrise\Http\Router\OpenApi\Command\GenerateJsonSchemaCommand;
use Sunrise\Http\Router\OpenApi\OpenApi;

/** @var OpenApi $openapi */
$command = new GenerateJsonSchemaCommand($openapi);
```

```bash
php bin/app router:generate-json-schema --help
```

## Test Kit

#### assertResponseBodyMatchesDescription

> The assertion fails if the given response body doesn't match a description of the operation identified by the given ID.
```php
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Sunrise\Http\Router\OpenApi\Test\OpenapiTestKit;

class SomeTest extends TestCase
{
use OpenapiTestKit;

public function testResponseBodyMatchesDescription() : void
{
// some logic to run a route...

/** @var ResponseInterface $response */
$this->assertResponseBodyMatchesDescription('route.name', $response);
}
}
```

## Simple Route Description

```php
class SomeController
{

/**
* @OpenApi\Operation(
* requestBody=@OpenApi\RequestBody(
* content={
* "application/json": @OpenApi\MediaType(
* schema=@OpenApi\Schema(
* type="object",
* properties={
* "foo": @OpenApi\Schema(
* type="string",
* ),
* },
* ),
* ),
* },
* ),
* responses={
* 200: @OpenApi\Response(
* description="Ok",
* ),
* },
* )
*/
public function someAction()
{
}
}
```

---

[All details on the router page.](https://github.com/sunrise-php/http-router)
Look for more examples here: [Some App](https://github.com/sunrise-php/http-router-openapi/tree/be27acedfc1f100f8efdcdd9da9430714890baa3/tests/fixtures/SomeApp)
19 changes: 8 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "sunrise/http-router-openapi",
"homepage": "https://github.com/sunrise-php/http-router-openapi",
"description": "Sunrise // OpenApi (Swagger) Specification support for Sunrise router",
"description": "Sunrise // OpenApi (Swagger) Specification support for Sunrise Router",
"license": "MIT",
"keywords": [
"fenric",
Expand All @@ -23,14 +23,14 @@
],
"require": {
"php": "^7.1|^8.0",
"doctrine/annotations": "^1.6",
"doctrine/cache": "^1.6",
"sunrise/http-router": "^2.4"
"sunrise/http-router": "^2.10",
"doctrine/annotations": "^1.6"
},
"require-dev": {
"phpunit/phpunit": "7.5.20||9.5.0",
"phpunit/phpunit": "7.5.20|9.5.0",
"sunrise/coding-standard": "1.0.0",
"sunrise/http-factory": "1.1.0",
"symfony/console": "^4.4",
"justinrainbow/json-schema": "5.2.10"
},
"autoload": {
Expand All @@ -40,16 +40,13 @@
},
"autoload-dev": {
"psr-4": {
"Sunrise\\Http\\Router\\OpenApi\\Tests\\": "tests/"
"Sunrise\\Http\\Router\\OpenApi\\Tests\\Fixtures\\": "tests/fixtures/"
}
},
"scripts": {
"test": [
"phpunit --colors=always --coverage-text",
"phpcs"
],
"bench": [
"phpbench run --report='generator: \"table\", cols: [\"groups\", \"subject\", \"mean\", \"diff\"], sort: {mean: \"asc\"}'"
"phpcs",
"XDEBUG_MODE=coverage phpunit --coverage-text"
]
}
}
4 changes: 0 additions & 4 deletions phpbench.json.dist

This file was deleted.

4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0"?>
<phpunit bootstrap="tests/bootstrap.php" colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<phpunit colors="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="OpenAPI Specification Support for Sunrise HTTP Router Test Suite">
<testsuite name="sunrise.http.router.openapi.testSuite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
Expand Down
Loading

0 comments on commit 5761522

Please sign in to comment.