Skip to content

Commit

Permalink
Routes names; Allow json file custom name; Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkaOnLine committed Apr 19, 2016
1 parent 92feca3 commit 4eeab4f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preset: laravel
8 changes: 8 additions & 0 deletions config/l5-swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
10 changes: 6 additions & 4 deletions src/Http/Controllers/SwaggerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions src/routes.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

$router->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'
]);
2 changes: 1 addition & 1 deletion tests/GeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
25 changes: 20 additions & 5 deletions tests/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ 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);
}

/** @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();

Expand All @@ -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();
}
}
10 changes: 9 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

class TestCase extends Orchestra\Testbench\TestCase
{

protected function getPackageProviders($app)
{
return [
Expand Down Expand Up @@ -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()
Expand All @@ -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]);
}
}

0 comments on commit 4eeab4f

Please sign in to comment.