From 99ec1c855577fe4cc4cd987f4625ae35ee39a297 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Mon, 19 Feb 2018 12:29:34 +0200 Subject: [PATCH] OpenApi 3.0 test drop cache travis allow 2.0 and 3.0 branches readme update --- .travis.yml | 12 ++- README.md | 22 +++- composer.json | 2 +- config/l5-swagger.php | 4 - tests/ConsoleTest.php | 2 +- tests/GeneratorTest.php | 8 +- tests/TestCase.php | 26 +++-- .../annotations/OpenApi/Anotations.php | 102 ++++++++++++++++++ .../Anotations.php} | 0 9 files changed, 157 insertions(+), 21 deletions(-) create mode 100644 tests/storage/annotations/OpenApi/Anotations.php rename tests/storage/annotations/{ApiAnotationsExample.php => Swagger/Anotations.php} (100%) diff --git a/.travis.yml b/.travis.yml index d5166c0..aed2c58 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,19 +6,25 @@ env: global: - DEFAULT_COMPOSER_FLAGS="--no-interaction --no-progress --optimize-autoloader" - REPORT_TESTS_COVERAGE=0 + - SWAGGER_VERSION=2.0 matrix: fast_finish: true include: - php: 7.1 + env: CACHE_NAME=SWAGGER + - php: 7.1 + env: SWAGGER_VERSION=3.0 CACHE_NAME=OPEN_API + - php: 7.2 + env: REPORT_TESTS_COVERAGE=1 CACHE_NAME=SWAGGER - php: 7.2 - env: REPORT_TESTS_COVERAGE=1 + env: SWAGGER_VERSION=3.0 CACHE_NAME=OPEN_API cache: directories: - "$HOME/.composer/cache" -before_install: - - travis_retry composer global require $DEFAULT_COMPOSER_FLAGS hirak/prestissimo install: - travis_retry composer update $DEFAULT_COMPOSER_FLAGS + - if [ $SWAGGER_VERSION == 2.0 ]; then composer require 'zircote/swagger-php:2.*'; fi + - if [ $SWAGGER_VERSION == 3.0 ]; then composer require 'zircote/swagger-php:3.*'; fi - composer info -D | sort before_script: - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter diff --git a/README.md b/README.md index a131737..215084c 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,8 @@ Installation 5.3.x | 2.2 | 1.1, 1.2, 2.0 | ```php composer require "darkaonline/l5-swagger:~3.0" ``` 5.4.x | 2.2 | 1.1, 1.2, 2.0 | ```php composer require "darkaonline/l5-swagger:~4.0" ``` 5.4.x | 3 | 2.0 | ```php composer require "darkaonline/l5-swagger:5.4.*" ``` - 5.5.x | 3 | 2.0 | ```php composer require "darkaonline/l5-swagger:5.5.*" ``` - 5.6.x | 3 | 2.0 | ```php composer require "darkaonline/l5-swagger:5.6.*" ``` + 5.5.x | 3 | 2.0, 3.0 | ```php composer require "darkaonline/l5-swagger:5.5.*" ``` + 5.6.x | 3 | 2.0, 3.0 | ```php composer require "darkaonline/l5-swagger:5.6.*" ``` You can publish L5-Swagger's config and view files into your project by running: @@ -41,6 +41,24 @@ or open your `config/app.php` and add this line in `providers` section L5Swagger\L5SwaggerServiceProvider::class, ``` +Using [OpenApi 3.0 Specification](https://github.com/OAI/OpenAPI-Specification) +============ +If you would like to use lattes OpenApi specifications (originally known as the Swagger Specification) in you project you should: +- Explicitly require `swagger-php` version 3.* in your projects composer by running: +```bash +composer require 'zircote/swagger-php:3.*' +``` +- Set environment variable `SWAGGER_VERSION` to **3.0** in your `.env` file: +``` +SWAGGER_VERSION=3.0 +``` +or in your `config/l5-swagger.php`: +```php +'swagger_version' => env('SWAGGER_VERSION', '3.0'), +``` +- Use examples provided here: https://github.com/zircote/swagger-php/tree/3.x/Examples/petstore-3.0 + + Using Swagger UI with Passport ============ The easiest way to build and test your Laravel-based API using Swagger-php is to use Passport's `CreateFreshApiToken` middleware. This middleware, built into Laravel's core, adds a cookie to all responses, and the cookie authenticates all subsequent requests through Passport's `TokenGuard`. diff --git a/composer.json b/composer.json index dba66b4..02f33b7 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "require": { "php": ">=7.1.3", "laravel/framework": "5.6.*", - "zircote/swagger-php": "~2.0", + "zircote/swagger-php": "~2.0|3.*", "swagger-api/swagger-ui": "^3.0" }, "require-dev": { diff --git a/config/l5-swagger.php b/config/l5-swagger.php index d09309f..80d76fe 100644 --- a/config/l5-swagger.php +++ b/config/l5-swagger.php @@ -1,7 +1,6 @@ [ /* |-------------------------------------------------------------------------- @@ -13,7 +12,6 @@ ], 'routes' => [ - /* |-------------------------------------------------------------------------- | Route for accessing api documentation interface @@ -49,11 +47,9 @@ 'docs' => [], 'oauth2_callback' => [], ], - ], 'paths' => [ - /* |-------------------------------------------------------------------------- | Absolute path to location where parsed swagger annotations will be stored diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index cdba022..f9a8a28 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -18,7 +18,7 @@ public function canGenerate() $fileContent = file_get_contents($this->jsonDocsFile()); $this->assertJson($fileContent); - $this->assertContains('L5 Swagger API', $fileContent); + $this->assertContains('L5 Swagger', $fileContent); } /** @test */ diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index ea3e9f3..2dac109 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -16,7 +16,7 @@ public function canGenerateApiJsonFile() $this->assertTrue(file_exists($this->jsonDocsFile())); $this->get(route('l5-swagger.docs')) - ->assertSee('L5 Swagger API') + ->assertSee('L5 Swagger') ->assertSee('my-default-host.com') ->isOk(); } @@ -24,6 +24,10 @@ public function canGenerateApiJsonFile() /** @test */ public function canGenerateApiJsonFileWithChangedBasePath() { + if ($this->isOpenApi() == true) { + $this->markTestSkipped('only for openApi 2.0'); + } + $this->setAnnotationsPath(); $cfg = config('l5-swagger'); @@ -35,7 +39,7 @@ public function canGenerateApiJsonFileWithChangedBasePath() $this->assertTrue(file_exists($this->jsonDocsFile())); $this->get(route('l5-swagger.docs')) - ->assertSee('L5 Swagger API') + ->assertSee('L5 Swagger') ->assertSee('new_path') ->isOk(); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 38f87bb..f5416db 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -7,13 +7,6 @@ class TestCase extends OrchestraTestCase { - protected function getPackageProviders($app) - { - return [ - L5SwaggerServiceProvider::class, - ]; - } - public function setUp() { parent::setUp(); @@ -30,6 +23,18 @@ public function tearDown() parent::tearDown(); } + protected function isOpenApi() + { + return version_compare(config('l5-swagger.swagger_version'), '3.0', '>='); + } + + protected function getPackageProviders($app) + { + return [ + L5SwaggerServiceProvider::class, + ]; + } + protected function crateJsonDocumentationFile() { file_put_contents($this->jsonDocsFile(), '{}'); @@ -47,7 +52,12 @@ protected function jsonDocsFile() protected function setAnnotationsPath() { $cfg = config('l5-swagger'); - $cfg['paths']['annotations'] = __DIR__.'/storage/annotations'; + $cfg['paths']['annotations'] = __DIR__.'/storage/annotations/Swagger'; + + if ($this->isOpenApi()) { + $cfg['paths']['annotations'] = __DIR__.'/storage/annotations/OpenApi'; + } + $cfg['generate_always'] = true; //Adding constants which will be replaced in generated json file diff --git a/tests/storage/annotations/OpenApi/Anotations.php b/tests/storage/annotations/OpenApi/Anotations.php new file mode 100644 index 0000000..e531105 --- /dev/null +++ b/tests/storage/annotations/OpenApi/Anotations.php @@ -0,0 +1,102 @@ +