From 4eeab4fdaf52e9f3d49be5e9804837ca23415f4f Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Tue, 19 Apr 2016 11:15:04 +0300 Subject: [PATCH] Routes names; Allow json file custom name; Tests --- .styleci.yml | 1 + config/l5-swagger.php | 8 +++++++ src/Generator.php | 2 +- src/Http/Controllers/SwaggerController.php | 10 +++++---- src/routes.php | 10 +++++++-- tests/GeneratorTest.php | 2 +- tests/RoutesTest.php | 25 +++++++++++++++++----- tests/TestCase.php | 10 ++++++++- 8 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 .styleci.yml diff --git a/.styleci.yml b/.styleci.yml new file mode 100644 index 0000000..0285f17 --- /dev/null +++ b/.styleci.yml @@ -0,0 +1 @@ +preset: laravel diff --git a/config/l5-swagger.php b/config/l5-swagger.php index 8e85864..4a0854d 100644 --- a/config/l5-swagger.php +++ b/config/l5-swagger.php @@ -92,6 +92,14 @@ 'docs' => storage_path('api-docs'), + /* + |-------------------------------------------------------------------------- + | File name of the generated json documentation file + |-------------------------------------------------------------------------- + */ + + 'docs_json' => 'api-docs.json', + /* |-------------------------------------------------------------------------- | Absolute path to directory containing the swagger annotations are stored. diff --git a/src/Generator.php b/src/Generator.php index 6a31e16..e42dfd6 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -20,7 +20,7 @@ public static function generateDocs() $excludeDirs = config('l5-swagger.paths.excludes'); $swagger = \Swagger\scan($appDir, ['exclude' => $excludeDirs]); - $filename = $docDir.'/api-docs.json'; + $filename = $docDir.'/'.config('l5-swagger.paths.docs_json', 'api-docs.json'); $swagger->saveAs($filename); } } diff --git a/src/Http/Controllers/SwaggerController.php b/src/Http/Controllers/SwaggerController.php index adad250..b50346b 100644 --- a/src/Http/Controllers/SwaggerController.php +++ b/src/Http/Controllers/SwaggerController.php @@ -13,12 +13,14 @@ class SwaggerController extends BaseController /** * Dump api-docs.json content endpoint. * - * @param string $page + * @param string $jsonFile + * * @return \Response */ - public function docs($page = 'api-docs.json') + public function docs($jsonFile = null) { - $filePath = config('l5-swagger.paths.docs') . '/' . $page; + $filePath = config('l5-swagger.paths.docs') . '/' . + (!is_null($jsonFile) ? $jsonFile : config('l5-swagger.paths.docs_json', 'api-docs.json')); if (File::extension($filePath) === '') { $filePath .= '.json'; @@ -66,7 +68,7 @@ public function api() 'securityDefinition' => config('l5-swagger.api.security_definition'), 'apiKeyInject' => config('l5-swagger.api.key_inject'), 'secure' => Request::secure(), - 'urlToDocs' => url(config('l5-swagger.routes.docs')), + 'urlToDocs' => route('l5-swagger.docs', config('l5-swagger.paths.docs_json', 'api-docs.json')), 'requestHeaders' => config('l5-swagger.headers.request'), ], $extras), 200 diff --git a/src/routes.php b/src/routes.php index d3bb3d1..1ed4d57 100644 --- a/src/routes.php +++ b/src/routes.php @@ -1,5 +1,11 @@ any(config('l5-swagger.routes.docs') . '/{page?}', '\L5Swagger\Http\Controllers\SwaggerController@docs'); +$router->any(config('l5-swagger.routes.docs') . '/{jsonFile?}', [ + 'as' => 'l5-swagger.docs', + 'uses' => '\L5Swagger\Http\Controllers\SwaggerController@docs' +]); -$router->get(config('l5-swagger.routes.api'), '\L5Swagger\Http\Controllers\SwaggerController@api'); +$router->get(config('l5-swagger.routes.api'), [ + 'as' => 'l5-swagger.api', + 'uses' => '\L5Swagger\Http\Controllers\SwaggerController@api' +]); diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index bdb813d..9e197bd 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -11,7 +11,7 @@ public function can_generate_api_json_file() $this->assertTrue(file_exists($this->jsonDocsFile())); - $this->visit(config('l5-swagger.routes.docs')) + $this->visit(route('l5-swagger.docs')) ->see('L5 Swagger API') ->assertResponseOk(); } diff --git a/tests/RoutesTest.php b/tests/RoutesTest.php index 294acce..6cbcd1d 100644 --- a/tests/RoutesTest.php +++ b/tests/RoutesTest.php @@ -5,7 +5,7 @@ class RoutesTest extends \TestCase /** @test */ public function user_cant_access_json_file_if_it_is_not_generated() { - $jsonUrl = config('l5-swagger.routes.docs'); + $jsonUrl = route('l5-swagger.docs'); $this->setExpectedException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class); $this->visit($jsonUrl); } @@ -13,7 +13,7 @@ public function user_cant_access_json_file_if_it_is_not_generated() /** @test */ public function user_can_access_json_file_if_it_is_generated() { - $jsonUrl = config('l5-swagger.routes.docs'); + $jsonUrl = route('l5-swagger.docs'); $this->crateJsonDocumentationFile(); @@ -23,10 +23,25 @@ public function user_can_access_json_file_if_it_is_generated() } /** @test */ - public function user_can_access_documentation_interface() + public function user_can_access_and_generate_custom_json_file() { - $this->get(config('l5-swagger.routes.api')); + $customJsonFileName = 'docs.v1.json'; + + $jsonUrl = route('l5-swagger.docs', $customJsonFileName); + + $this->setCustomDocsFileName($customJsonFileName); + $this->crateJsonDocumentationFile(); - $this->assertResponseOk(); + $this->visit($jsonUrl) + ->see('{}') + ->assertResponseOk(); + } + + /** @test */ + public function user_can_access_documentation_interface() + { + $this->visit(config('l5-swagger.routes.api')) + ->see(route('l5-swagger.docs', config('l5-swagger.paths.docs_json', 'api-docs.json'))) + ->assertResponseOk(); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 0fbc6fe..2648e42 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,6 +2,7 @@ class TestCase extends Orchestra\Testbench\TestCase { + protected function getPackageProviders($app) { return [ @@ -29,7 +30,7 @@ protected function jsonDocsFile() mkdir(config('l5-swagger.paths.docs')); } - return config('l5-swagger.paths.docs').'/api-docs.json'; + return config('l5-swagger.paths.docs') . '/' . config('l5-swagger.paths.docs_json'); } protected function setAnnotationsPath() @@ -39,4 +40,11 @@ protected function setAnnotationsPath() $cfg['generate_always'] = true; config(['l5-swagger' => $cfg]); } + + protected function setCustomDocsFileName($fileName) + { + $cfg = config('l5-swagger'); + $cfg['paths']['docs_json'] = $fileName; + config(['l5-swagger' => $cfg]); + } }