From e4a0f9595bab055ab4c63d3eeb50687b6642ec68 Mon Sep 17 00:00:00 2001 From: Michel Mohrmann Date: Tue, 25 Feb 2020 17:53:30 +0100 Subject: [PATCH 01/10] cakePHP 4.x compatiblity --- README.md | 23 ++++--------------- composer.json | 4 ++-- src/Controller/AppController.php | 2 +- src/Controller/DocsController.php | 2 +- src/Controller/UiController.php | 2 +- src/Shell/SwaggerShell.php | 3 ++- .../index.ctp => templates/Docs/index.php | 0 .../Ui/index.ctp => templates/Ui/index.php | 0 8 files changed, 11 insertions(+), 25 deletions(-) rename src/Template/Docs/index.ctp => templates/Docs/index.php (100%) rename src/Template/Ui/index.ctp => templates/Ui/index.php (100%) diff --git a/README.md b/README.md index 628c3f0..1718fd0 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@ [![Total Downloads](https://img.shields.io/packagist/dt/alt3/cakephp-swagger.svg?style=flat-square)](https://packagist.org/packages/alt3/cakephp-swagger) [![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE.txt) -CakePHP 3.x plugin that adds auto-generated Swagger 2.0 documentation to your projects using swagger-php and swagger-ui. +CakePHP 4.x plugin that adds auto-generated Swagger 2.0 documentation to your projects using swagger-php and swagger-ui. ## Requirements -* CakePHP 3.5+ +* CakePHP 4.0+ * Some [swagger-php](https://github.com/zircote/swagger-php) annotation knowledge ## Installation @@ -21,22 +21,7 @@ Install the plugin using composer: composer require alt3/cakephp-swagger ``` -## Enabling for CakePHP < 3.6 - -To enable the plugin either run the following command: - -```bash -bin/cake plugin load Alt3/Swagger --routes --bootstrap -``` - -or manually add the following line to your `config/bootstrap.php` file: - -```bash -Plugin::load('Alt3/Swagger', ['routes' => true, 'bootstrap' => true]); -``` - -## Enabling for CakePHP >= 3.6 - +## Enabling Enable the plugin in the `bootstrap()` method found in `src/Application.php`: ```php @@ -281,6 +266,6 @@ Which should result in: Before submitting a PR make sure: -- [PHPUnit](http://book.cakephp.org/3.0/en/development/testing.html#running-tests) +- [PHPUnit](http://book.cakephp.org/4.0/en/development/testing.html#running-tests) and [CakePHP Code Sniffer](https://github.com/cakephp/cakephp-codesniffer) tests pass - [Coveralls Code Coverage ](https://coveralls.io/github/alt3/cakephp-swagger) remains at 100% diff --git a/composer.json b/composer.json index d993447..fa4b415 100644 --- a/composer.json +++ b/composer.json @@ -1,11 +1,11 @@ { "name": "alt3/cakephp-swagger", - "description": "Instant Swagger documentation for your CakePHP 3.x APIs", + "description": "Instant Swagger documentation for your CakePHP 4.x APIs", "type": "cakephp-plugin", "license": "MIT", "require": { "php": ">=5.6", - "cakephp/cakephp": "^3.5", + "cakephp/cakephp": "^4.0", "zircote/swagger-php": "^2.0" }, "require-dev": { diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index 7ca96c5..dd1d990 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -30,7 +30,7 @@ class AppController extends BaseController * * @return void */ - public function initialize() + public function initialize(): void { parent::initialize(); diff --git a/src/Controller/DocsController.php b/src/Controller/DocsController.php index 7ed2957..27412a9 100644 --- a/src/Controller/DocsController.php +++ b/src/Controller/DocsController.php @@ -105,7 +105,7 @@ protected function addCorsHeaders() protected function jsonResponse($json) { $this->set('json', $json); - $this->viewBuilder()->setLayout(false); + $this->viewBuilder()->disableAutoLayout(); $this->addCorsHeaders(); $this->response = $this->response->withType('json'); } diff --git a/src/Controller/UiController.php b/src/Controller/UiController.php index 1f16335..14b8008 100644 --- a/src/Controller/UiController.php +++ b/src/Controller/UiController.php @@ -15,7 +15,7 @@ class UiController extends AppController */ public function index() { - $this->viewBuilder()->setLayout(false); + $this->viewBuilder()->disableAutoLayout(); $this->set('uiConfig', $this->config['ui']); $this->set('url', $this->getDefaultDocumentUrl()); } diff --git a/src/Shell/SwaggerShell.php b/src/Shell/SwaggerShell.php index c9efbc2..fb915f1 100644 --- a/src/Shell/SwaggerShell.php +++ b/src/Shell/SwaggerShell.php @@ -6,6 +6,7 @@ use Cake\Console\Shell; use Cake\Core\Configure; use Cake\Utility\Hash; +use Cake\Console\ConsoleOptionParser; class SwaggerShell extends Shell { @@ -17,7 +18,7 @@ class SwaggerShell extends Shell * @return \Cake\Console\ConsoleOptionParser * @throws \Aura\Intl\Exception */ - public function getOptionParser() + public function getOptionParser(): ConsoleOptionParser { $parser = parent::getOptionParser(); diff --git a/src/Template/Docs/index.ctp b/templates/Docs/index.php similarity index 100% rename from src/Template/Docs/index.ctp rename to templates/Docs/index.php diff --git a/src/Template/Ui/index.ctp b/templates/Ui/index.php similarity index 100% rename from src/Template/Ui/index.ctp rename to templates/Ui/index.php From 9c7153b150d151cce98d9b3f0c3d1c89f1302810 Mon Sep 17 00:00:00 2001 From: Mimo Date: Wed, 26 Feb 2020 01:43:26 +0100 Subject: [PATCH 02/10] cakePHP requires at least php 7.2 --- .travis.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 830c900..13ff9d9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,9 @@ language: php php: - - 5.6 - - 7.0 - - 7.1 - 7.2 + - 7.3 + - 7.4 sudo: false From 8748b0941f88e16340b0be8a9e70d8c7cf8adc92 Mon Sep 17 00:00:00 2001 From: Michel Mohrmann Date: Wed, 26 Feb 2020 12:35:04 +0100 Subject: [PATCH 03/10] cakePHP 4 --- composer.json | 4 ++-- phpunit.xml.dist | 1 - tests/App/Application.php | 5 +++-- tests/TestCase/Controller/DocsControllerTest.php | 10 ++++++---- .../Controller/DocsCustomRouteIntegrationTest.php | 4 ++-- .../Controller/UiControllerCustomUiRouteTest.php | 4 ++-- .../Controller/UiControllerDefaultRouteTest.php | 4 ++-- tests/TestCase/Controller/UiControllerTest.php | 8 +++++--- tests/TestCase/Lib/SwaggerToolsTest.php | 4 ++-- tests/TestCase/Shell/SwaggerShellTest.php | 4 ++-- 10 files changed, 26 insertions(+), 22 deletions(-) diff --git a/composer.json b/composer.json index fa4b415..8c1c0cb 100644 --- a/composer.json +++ b/composer.json @@ -4,12 +4,12 @@ "type": "cakephp-plugin", "license": "MIT", "require": { - "php": ">=5.6", + "php": ">=7.0", "cakephp/cakephp": "^4.0", "zircote/swagger-php": "^2.0" }, "require-dev": { - "phpunit/phpunit": "5.*" + "phpunit/phpunit": "^7.0" }, "support": { "issues": "https://github.com/alt3/cakephp-swagger/issues", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 004b426..3177473 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -3,7 +3,6 @@ colors="true" processIsolation="false" stopOnFailure="false" - syntaxCheck="false" bootstrap="./tests/bootstrap.php" > diff --git a/tests/App/Application.php b/tests/App/Application.php index 78d1bd6..2c37774 100644 --- a/tests/App/Application.php +++ b/tests/App/Application.php @@ -5,6 +5,7 @@ use Cake\Http\BaseApplication; use Cake\Routing\Middleware\AssetMiddleware; use Cake\Routing\Middleware\RoutingMiddleware; +use Cake\Http\MiddlewareQueue; class Application extends BaseApplication { @@ -14,7 +15,7 @@ class Application extends BaseApplication * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to set in your App Class * @return \Cake\Http\MiddlewareQueue */ - public function middleware($middlewareQueue) + public function middleware($middlewareQueue): MiddlewareQueue { $middlewareQueue // Handle plugin/theme assets like CakePHP normally does. @@ -25,7 +26,7 @@ public function middleware($middlewareQueue) return $middlewareQueue; } - public function bootstrap() + public function bootstrap(): void { $this->addPlugin(Plugin::class, ['routes' => true, 'bootstrap' => true]); parent::bootstrap(); diff --git a/tests/TestCase/Controller/DocsControllerTest.php b/tests/TestCase/Controller/DocsControllerTest.php index f7da781..9a8113d 100644 --- a/tests/TestCase/Controller/DocsControllerTest.php +++ b/tests/TestCase/Controller/DocsControllerTest.php @@ -4,6 +4,7 @@ use Alt3\Swagger\Controller\DocsController; use Alt3\Swagger\Test\App\Application; use Cake\TestSuite\TestCase; +use Cake\Routing\Router; use StdClass; class DocsControllerTest extends TestCase @@ -36,21 +37,22 @@ class DocsControllerTest extends TestCase /** * setUp method executed before every testMethod. */ - public function setUp() + public function setUp(): void { parent::setUp(); $this->controller = new DocsController(); $app = new Application(CONFIG); $app->bootstrap(); - $app->routes(null); - $app->pluginRoutes(null); + $builder = Router::createRouteBuilder('/'); + $app->routes($builder); + $app->pluginRoutes($builder); } /** * tearDown method executed after every testMethod. */ - public function tearDown() + public function tearDown(): void { parent::tearDownAfterClass(); $testDoc = CACHE . 'cakephp_swagger_testdoc.json'; diff --git a/tests/TestCase/Controller/DocsCustomRouteIntegrationTest.php b/tests/TestCase/Controller/DocsCustomRouteIntegrationTest.php index 98b62e0..443abe2 100644 --- a/tests/TestCase/Controller/DocsCustomRouteIntegrationTest.php +++ b/tests/TestCase/Controller/DocsCustomRouteIntegrationTest.php @@ -15,7 +15,7 @@ class DocsCustomRouteIntegrationTest extends IntegrationTestCase * setUp method. Creates a temporary swagger.php configuration file * specific to this integration test. */ - public function setUp() + public function setUp(): void { parent::setUp(); $this->useHttpServer(true); @@ -27,7 +27,7 @@ public function setUp() /** * tearDown method executed after every test method. */ - public function tearDown() + public function tearDown(): void { parent::tearDown(); unlink($this->tempConfig); diff --git a/tests/TestCase/Controller/UiControllerCustomUiRouteTest.php b/tests/TestCase/Controller/UiControllerCustomUiRouteTest.php index 0574f4b..1f07ef7 100644 --- a/tests/TestCase/Controller/UiControllerCustomUiRouteTest.php +++ b/tests/TestCase/Controller/UiControllerCustomUiRouteTest.php @@ -14,7 +14,7 @@ class UiControllerCustomUiRouteTest extends IntegrationTestCase /** * setUp method. */ - public function setUp() + public function setUp(): void { parent::setUp(); $this->useHttpServer(true); @@ -26,7 +26,7 @@ public function setUp() /** * tearDown method. */ - public function tearDown() + public function tearDown(): void { parent::tearDown(); unlink($this->tempConfig); diff --git a/tests/TestCase/Controller/UiControllerDefaultRouteTest.php b/tests/TestCase/Controller/UiControllerDefaultRouteTest.php index f18db92..59b4244 100644 --- a/tests/TestCase/Controller/UiControllerDefaultRouteTest.php +++ b/tests/TestCase/Controller/UiControllerDefaultRouteTest.php @@ -14,7 +14,7 @@ class UiControllerDefaultRouteTest extends IntegrationTestCase /** * setUp method. */ - public function setUp() + public function setUp(): void { parent::setUp(); $this->useHttpServer(true); @@ -26,7 +26,7 @@ public function setUp() /** * tearDown method. */ - public function tearDown() + public function tearDown(): void { parent::tearDown(); unlink($this->tempConfig); diff --git a/tests/TestCase/Controller/UiControllerTest.php b/tests/TestCase/Controller/UiControllerTest.php index f9bf91d..c1054ee 100644 --- a/tests/TestCase/Controller/UiControllerTest.php +++ b/tests/TestCase/Controller/UiControllerTest.php @@ -3,6 +3,7 @@ use Alt3\Swagger\Controller\UiController; use Alt3\Swagger\Test\App\Application; +use Cake\Routing\Router; use Cake\TestSuite\TestCase; use StdClass; @@ -29,15 +30,16 @@ class UiControllerTest extends TestCase /** * setUp method executed before every testMethod. */ - public function setUp() + public function setUp(): void { parent::setUp(); $this->controller = new UiController(); $app = new Application(CONFIG); $app->bootstrap(); - $app->routes(null); - $app->pluginRoutes(null); + $builder = Router::createRouteBuilder('/'); + $app->routes($builder); + $app->pluginRoutes($builder); } /** diff --git a/tests/TestCase/Lib/SwaggerToolsTest.php b/tests/TestCase/Lib/SwaggerToolsTest.php index 41352f7..37f22bf 100644 --- a/tests/TestCase/Lib/SwaggerToolsTest.php +++ b/tests/TestCase/Lib/SwaggerToolsTest.php @@ -31,7 +31,7 @@ class SwaggerToolsTest extends TestCase /** * setUp method executed before every testMethod. */ - public function setUp() + public function setUp(): void { parent::setUp(); $this->lib = new SwaggerTools(); @@ -40,7 +40,7 @@ public function setUp() /** * tearDown method executed after every testMethod. */ - public function tearDown() + public function tearDown(): void { parent::tearDownAfterClass(); $testDoc = CACHE . 'cakephp_swagger_testdoc.json'; diff --git a/tests/TestCase/Shell/SwaggerShellTest.php b/tests/TestCase/Shell/SwaggerShellTest.php index e5dc9bf..0c1c0be 100644 --- a/tests/TestCase/Shell/SwaggerShellTest.php +++ b/tests/TestCase/Shell/SwaggerShellTest.php @@ -18,7 +18,7 @@ class SwaggerShellTest extends TestCase * * @return void */ - public function setUp() + public function setUp(): void { parent::setUp(); $this->io = $this->createMock('Cake\Console\ConsoleIo'); @@ -30,7 +30,7 @@ public function setUp() * * @return void */ - public function tearDown() + public function tearDown(): void { unset($this->shell); parent::tearDown(); From b06f8eb463e11b33fb975b6044ccf2260c3f04d4 Mon Sep 17 00:00:00 2001 From: Michel Mohrmann Date: Wed, 26 Feb 2020 13:43:58 +0100 Subject: [PATCH 04/10] cakePHP 4 --- composer.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 8c1c0cb..f7561b6 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ "zircote/swagger-php": "^2.0" }, "require-dev": { - "phpunit/phpunit": "^7.0" + "phpunit/phpunit": "^7.0", + "cakephp/cakephp-codesniffer": "^4.0" }, "support": { "issues": "https://github.com/alt3/cakephp-swagger/issues", From 6187364dc68059c8c2e61b471686f2368c1460bc Mon Sep 17 00:00:00 2001 From: Michel Mohrmann Date: Wed, 26 Feb 2020 15:27:44 +0100 Subject: [PATCH 05/10] cakePHP 4 --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 13ff9d9..7de84d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,10 +15,10 @@ matrix: fast_finish: true include: - - php: 7.2 + - php: 7.3 env: PHPCS=1 PHPUNIT=0 - - php: 7.2 + - php: 7.3 env: CODECOVERAGE=1 PHPUNIT=0 allow_failures: From 27b6cb5ce3500d645f21b9c5d6c0eb70befb2130 Mon Sep 17 00:00:00 2001 From: Michel Mohrmann Date: Wed, 26 Feb 2020 15:34:51 +0100 Subject: [PATCH 06/10] cakePHP 4 --- .travis.yml | 2 +- composer.json | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7de84d2..bf4972c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,7 +28,7 @@ before_script: - composer self-update - composer install --prefer-dist --no-interaction - - sh -c "if [ '$PHPCS' = '1' ]; then composer require cakephp/cakephp-codesniffer:dev-master; fi" + - sh -c "if [ '$PHPCS' = '1' ]; then composer require --dev cakephp/cakephp-codesniffer; fi" - phpenv rehash - set +H diff --git a/composer.json b/composer.json index f7561b6..8c1c0cb 100644 --- a/composer.json +++ b/composer.json @@ -9,8 +9,7 @@ "zircote/swagger-php": "^2.0" }, "require-dev": { - "phpunit/phpunit": "^7.0", - "cakephp/cakephp-codesniffer": "^4.0" + "phpunit/phpunit": "^7.0" }, "support": { "issues": "https://github.com/alt3/cakephp-swagger/issues", From 3dd542db5dd7875ea2e12bbee28bbdbe4c43c131 Mon Sep 17 00:00:00 2001 From: Michel Mohrmann Date: Thu, 27 Feb 2020 17:42:28 +0100 Subject: [PATCH 07/10] phpcbf autofix --- config/routes.php | 3 +- src/Controller/AppController.php | 9 ++-- src/Controller/DocsController.php | 9 ++-- src/Controller/UiController.php | 4 +- src/Lib/SwaggerTools.php | 5 ++- src/Plugin.php | 1 + src/Shell/SwaggerShell.php | 8 ++-- tests/App/Application.php | 4 +- .../src/Controller/DummyExcludeController.php | 3 +- .../src/Controller/DummyInlcudeController.php | 2 +- .../Controller/DocsControllerTest.php | 24 ++++++----- .../DocsCustomRouteIntegrationTest.php | 3 +- .../UiControllerCustomUiRouteTest.php | 3 +- .../UiControllerDefaultRouteTest.php | 3 +- .../TestCase/Controller/UiControllerTest.php | 15 ++++--- tests/TestCase/Lib/SwaggerToolsTest.php | 43 ++++++++++--------- tests/TestCase/Shell/SwaggerShellTest.php | 3 +- tests/bootstrap.php | 20 ++++----- 18 files changed, 91 insertions(+), 71 deletions(-) diff --git a/config/routes.php b/config/routes.php index 8411c6e..e92a4a7 100644 --- a/config/routes.php +++ b/config/routes.php @@ -1,4 +1,5 @@ '/' + 'path' => '/', ], function (\Cake\Routing\RouteBuilder $routes) { // UI route diff --git a/src/Controller/AppController.php b/src/Controller/AppController.php index dd1d990..4031990 100644 --- a/src/Controller/AppController.php +++ b/src/Controller/AppController.php @@ -1,4 +1,6 @@ [ - 'crawl' => true + 'crawl' => true, ], 'ui' => [ - 'title' => 'cakephp-swagger' - ] + 'title' => 'cakephp-swagger', + ], ]; /** diff --git a/src/Controller/DocsController.php b/src/Controller/DocsController.php index 27412a9..2bff8f4 100644 --- a/src/Controller/DocsController.php +++ b/src/Controller/DocsController.php @@ -1,4 +1,6 @@ true, - 'data' => [] + 'data' => [], ]; /** @@ -67,8 +68,8 @@ protected function getJsonDocumentList() 'plugin' => 'Alt3/Swagger', 'controller' => 'Docs', 'action' => 'index', - $document - ], true) + $document, + ], true), ]; } diff --git a/src/Controller/UiController.php b/src/Controller/UiController.php index 14b8008..ee11aaa 100644 --- a/src/Controller/UiController.php +++ b/src/Controller/UiController.php @@ -1,4 +1,6 @@ 'Alt3/Swagger', 'controller' => 'Docs', 'action' => 'index', - $defaultDocument + $defaultDocument, ], true); } } diff --git a/src/Lib/SwaggerTools.php b/src/Lib/SwaggerTools.php index b8c1482..f75b5e6 100644 --- a/src/Lib/SwaggerTools.php +++ b/src/Lib/SwaggerTools.php @@ -1,4 +1,6 @@ Configure::read("Swagger.library.$id.exclude") + 'exclude' => Configure::read("Swagger.library.$id.exclude"), ]; } if (Configure::read('Swagger.analyser')) { diff --git a/src/Plugin.php b/src/Plugin.php index 595d6d0..5b8ac40 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -1,4 +1,5 @@ addSubcommand('makedocs', [ - 'description' => __('Crawl-generate fresh swagger file system documents for all entries found in the library.') + 'description' => __('Crawl-generate fresh swagger file system documents for all entries found in the library.'), ]) ->addArgument('host', [ 'help' => __("Swagger host FQDN (without protocol) as to be inserted into the swagger doc property 'host'"), - 'required' => true + 'required' => true, ]); return $parser; diff --git a/tests/App/Application.php b/tests/App/Application.php index 2c37774..c78fc13 100644 --- a/tests/App/Application.php +++ b/tests/App/Application.php @@ -1,11 +1,13 @@ [ - 'crawl' => true + 'crawl' => true, ], 'ui' => [ - 'title' => 'cakephp-swagger' - ] + 'title' => 'cakephp-swagger', + ], ]; /** @@ -31,7 +33,7 @@ class DocsControllerTest extends TestCase */ protected static $apiResponseBody = [ 'success' => true, - 'data' => [] + 'data' => [], ]; /** @@ -85,7 +87,7 @@ public function testMethodGetJsonDocumentList() // no documents in library should return empty json success response $reflection->properties->config->setValue($this->controller, array_merge(self::$defaultConfig, [ - 'library' => [] + 'library' => [], ])); $result = $reflection->methods->getJsonDocumentList->invokeArgs($this->controller, []); $expected = json_encode(self::$apiResponseBody, JSON_PRETTY_PRINT); @@ -95,8 +97,8 @@ public function testMethodGetJsonDocumentList() $reflection->properties->config->setValue($this->controller, array_merge(self::$defaultConfig, [ 'library' => [ 'testdoc1' => [], - 'testdoc2' => [] - ] + 'testdoc2' => [], + ], ])); $expected = <<<'EOF' @@ -146,7 +148,7 @@ public function testEmptyLibrary() { $reflection = self::getReflection($this->controller); $reflection->properties->config->setValue($this->controller, array_merge(self::$defaultConfig, [ - 'library' => [] + 'library' => [], ])); $reflection->methods->index->invokeArgs($this->controller, ['testdoc']); } @@ -168,8 +170,8 @@ public function testAddingCorsHeaders() // cors headers section in configuration but no entries $reflection->properties->config->setValue($this->controller, array_merge(self::$defaultConfig, [ 'docs' => [ - 'cors' => [] - ] + 'cors' => [], + ], ])); $result = $reflection->methods->addCorsHeaders->invokeArgs($this->controller, []); $this->assertFalse($result); diff --git a/tests/TestCase/Controller/DocsCustomRouteIntegrationTest.php b/tests/TestCase/Controller/DocsCustomRouteIntegrationTest.php index 443abe2..2b4ad50 100644 --- a/tests/TestCase/Controller/DocsCustomRouteIntegrationTest.php +++ b/tests/TestCase/Controller/DocsCustomRouteIntegrationTest.php @@ -1,11 +1,12 @@ [ - 'crawl' => true + 'crawl' => true, ], 'ui' => [ - 'title' => 'cakephp-swagger' - ] + 'title' => 'cakephp-swagger', + ], ]; /** @@ -61,7 +62,7 @@ public function testMethodGetDefaultDocumentUrl() // test without library documents $reflection->properties->config->setValue($this->controller, array_merge(self::$defaultConfig, [ - 'library' => [] + 'library' => [], ])); $result = $reflection->methods->getDefaultDocumentUrl->invokeArgs($this->controller, []); $this->assertSame($result, 'http://petstore.swagger.io/v2/swagger.json'); @@ -69,8 +70,8 @@ public function testMethodGetDefaultDocumentUrl() // test with library document $reflection->properties->config->setValue($this->controller, [ 'library' => [ - 'testdoc' => [] - ] + 'testdoc' => [], + ], ]); $result = $reflection->methods->getDefaultDocumentUrl->invokeArgs($this->controller, []); $this->assertSame($result, 'http://localhost/alt3/swagger/docs/testdoc'); diff --git a/tests/TestCase/Lib/SwaggerToolsTest.php b/tests/TestCase/Lib/SwaggerToolsTest.php index 37f22bf..45404c1 100644 --- a/tests/TestCase/Lib/SwaggerToolsTest.php +++ b/tests/TestCase/Lib/SwaggerToolsTest.php @@ -1,4 +1,6 @@ [ - 'crawl' => true + 'crawl' => true, ], 'ui' => [ - 'title' => 'cakephp-swagger' - ] + 'title' => 'cakephp-swagger', + ], ]; /** @@ -65,13 +66,13 @@ public function testMethodGetSwaggerDocumentCrawlSuccess() $reflection = self::getReflection($this->lib); Configure::write('Swagger', array_merge(self::$defaultConfig, [ 'docs' => [ - 'crawl' => true + 'crawl' => true, ], 'library' => [ 'testdoc' => [ 'include' => APP, // all files in dir - ] - ] + ], + ], ])); // make sure all files are being crawled @@ -82,15 +83,15 @@ public function testMethodGetSwaggerDocumentCrawlSuccess() // make sure exclusions are actually being excluded from crawling. Configure::write('Swagger', array_merge(self::$defaultConfig, [ 'docs' => [ - 'crawl' => true + 'crawl' => true, ], 'library' => [ 'testdoc' => [ 'include' => APP, - 'exclude' => APP . DS . 'Controller' . DS . 'DummyExcludeController' - ] + 'exclude' => APP . DS . 'Controller' . DS . 'DummyExcludeController', + ], ], - 'analyser' => new \Swagger\StaticAnalyser() + 'analyser' => new \Swagger\StaticAnalyser(), ])); $result = $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['testdoc', 'www.test.app']); @@ -123,11 +124,11 @@ public function testMethodGetSwaggerDocumentFromFileFail() $reflection = self::getReflection($this->lib); Configure::write('Swagger', array_merge(self::$defaultConfig, [ 'docs' => [ - 'crawl' => false // force loading doc from filesystem + 'crawl' => false, // force loading doc from filesystem ], 'library' => [ - 'nonexisting' => [] - ] + 'nonexisting' => [], + ], ])); $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['nonexisting', 'www.test.app']); } @@ -145,13 +146,13 @@ public function testMethodGetSwaggerDocumentFromFileSuccess() $reflection = self::getReflection($this->lib); Configure::write('Swagger', array_merge(self::$defaultConfig, [ 'docs' => [ - 'crawl' => true + 'crawl' => true, ], 'library' => [ 'testdoc' => [ 'include' => APP, // all files in dir - ] - ] + ], + ], ])); $result = $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['testdoc', 'www.test.app']); @@ -161,13 +162,13 @@ public function testMethodGetSwaggerDocumentFromFileSuccess() // generated file should load from from filesystem when disabling crawl Configure::write('Swagger', array_merge(self::$defaultConfig, [ 'docs' => [ - 'crawl' => false + 'crawl' => false, ], 'library' => [ 'testdoc' => [ 'include' => APP, // crawl all files in this directory - ] - ] + ], + ], ])); $reflection->methods->getSwaggerDocument->invokeArgs($this->lib, ['testdoc', 'www.test.app']); @@ -215,7 +216,7 @@ public function testMakeDocsWithFilledLibrary() Configure::write('Swagger.library', [ 'testdoc' => [ 'include' => APP, // crawl all files in this directory - ] + ], ]); $result = $reflection->methods->makeDocs->invokeArgs($this->lib, ['www.test.app']); $this->assertTrue($result); diff --git a/tests/TestCase/Shell/SwaggerShellTest.php b/tests/TestCase/Shell/SwaggerShellTest.php index 0c1c0be..2a27715 100644 --- a/tests/TestCase/Shell/SwaggerShellTest.php +++ b/tests/TestCase/Shell/SwaggerShellTest.php @@ -1,4 +1,6 @@ 'css/', 'paths' => [ 'plugins' => [APP . 'Plugin' . DS], - 'templates' => [APP . 'Template' . DS] - ] + 'templates' => [APP . 'Template' . DS], + ], ]); Configure::write('Session', [ - 'defaults' => 'php' + 'defaults' => 'php', ]); Cache::setConfig([ '_cake_core_' => [ 'engine' => 'File', 'prefix' => 'cake_core_', - 'serialize' => true + 'serialize' => true, ], '_cake_model_' => [ 'engine' => 'File', 'prefix' => 'cake_model_', - 'serialize' => true + 'serialize' => true, ], 'default' => [ 'engine' => 'File', 'prefix' => 'default_', - 'serialize' => true - ] + 'serialize' => true, + ], ]); // Ensure default test connection is defined @@ -109,5 +109,5 @@ 'engine' => 'Cake\Log\Engine\FileLog', 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'], 'file' => 'error', - ] + ], ]); From f2b2c7609ee9e9b7b7d3f0d5f8f991b6ed42946d Mon Sep 17 00:00:00 2001 From: Michel Mohrmann Date: Thu, 27 Feb 2020 17:57:10 +0100 Subject: [PATCH 08/10] Code CleanUp --- tests/App/src/Controller/DummyExcludeController.php | 2 ++ .../{DummyInlcudeController.php => DummyIncludeController.php} | 2 ++ tests/TestCase/Controller/DocsControllerTest.php | 2 +- tests/TestCase/Controller/UiControllerTest.php | 2 +- tests/TestCase/Lib/SwaggerToolsTest.php | 2 +- tests/TestCase/Shell/SwaggerShellTest.php | 2 +- 6 files changed, 8 insertions(+), 4 deletions(-) rename tests/App/src/Controller/{DummyInlcudeController.php => DummyIncludeController.php} (97%) diff --git a/tests/App/src/Controller/DummyExcludeController.php b/tests/App/src/Controller/DummyExcludeController.php index 304c606..eda04a7 100644 --- a/tests/App/src/Controller/DummyExcludeController.php +++ b/tests/App/src/Controller/DummyExcludeController.php @@ -1,6 +1,8 @@ Date: Thu, 27 Feb 2020 18:35:17 +0100 Subject: [PATCH 09/10] fix: write Swagger content as text --- src/Lib/SwaggerTools.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Lib/SwaggerTools.php b/src/Lib/SwaggerTools.php index f75b5e6..e3af56a 100644 --- a/src/Lib/SwaggerTools.php +++ b/src/Lib/SwaggerTools.php @@ -66,14 +66,14 @@ public static function getSwaggerDocument($id, $host) * Write swagger document to filesystem. * * @param string $path Full path to the json document including filename - * @param string $content Swagger content + * @param object $content Swagger content * @throws \Cake\Http\Exception\InternalErrorException * @return bool */ protected static function writeSwaggerDocumentToFile($path, $content) { $fh = new File($path, true); - if (!$fh->write($content)) { + if (!$fh->write(serialize($content))) { throw new InternalErrorException('Error writing Swagger json document to filesystem'); } From d9e5391adff675222a597e6dd49f60843a9e24a6 Mon Sep 17 00:00:00 2001 From: Michel Mohrmann Date: Thu, 27 Feb 2020 18:44:09 +0100 Subject: [PATCH 10/10] fix: phpcs ignore long strings --- src/Controller/DocsController.php | 2 +- src/Shell/SwaggerShell.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/DocsController.php b/src/Controller/DocsController.php index 2bff8f4..5cedb52 100644 --- a/src/Controller/DocsController.php +++ b/src/Controller/DocsController.php @@ -38,7 +38,7 @@ public function index($id = null) } if (!array_key_exists($id, $this->config['library'])) { - throw new \InvalidArgumentException("Swagger configuration file does not contain a document definition for '$id'"); + throw new \InvalidArgumentException("Swagger configuration file does not contain a document definition for '$id'"); // phpcs:ignore } $document = SwaggerTools::getSwaggerDocument($id, $this->request->host()); diff --git a/src/Shell/SwaggerShell.php b/src/Shell/SwaggerShell.php index 8b95503..c97e8b3 100644 --- a/src/Shell/SwaggerShell.php +++ b/src/Shell/SwaggerShell.php @@ -25,7 +25,7 @@ public function getOptionParser(): ConsoleOptionParser $parser = parent::getOptionParser(); $parser->addSubcommand('makedocs', [ - 'description' => __('Crawl-generate fresh swagger file system documents for all entries found in the library.'), + 'description' => __('Crawl-generate fresh swagger file system documents for all entries found in the library.'), //phpcs:ignore ]) ->addArgument('host', [ 'help' => __("Swagger host FQDN (without protocol) as to be inserted into the swagger doc property 'host'"),