From 5926fb55c88578b9376b54c0c79d3e88b7bba113 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Thu, 17 Mar 2022 09:33:56 +0200 Subject: [PATCH 1/7] Add codacy code coverage report --- .github/workflows/test-config.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test-config.yml b/.github/workflows/test-config.yml index 3f2707e..d752146 100644 --- a/.github/workflows/test-config.yml +++ b/.github/workflows/test-config.yml @@ -71,10 +71,12 @@ jobs: - name: Publish code coverage env: - CC_TEST_REPORTER_ID: d5b1b36d663604a8900fb8a586625a94eae6cdb6f6d7372aec8f066c174b4a5c + CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }} + CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_PROJECT_TOKEN }} run: | cp ${{github.workspace}}/tests/storage/logs/test-reports/clover.xml ${{github.workspace}}/clover.xml ./cc-test-reporter after-build -t clover --exit-code 0 + bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r ${{github.workspace}}/clover.xml if: matrix.php == '8.1' && matrix.l5-swagger-flags == 'latest' - name: Publish coveralls report From b074e38a6298ad8b3efadfe11f2eeff81fc5d3d2 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Thu, 17 Mar 2022 13:27:42 +0200 Subject: [PATCH 2/7] Use Filesystem instead of File fasade --- Dockerfile | 2 +- src/Generator.php | 24 ++++++++----- tests/GeneratorTest.php | 56 +++++++++++++++++------------ tests/TestCase.php | 80 +++++++++++++++++++++++++++++++---------- 4 files changed, 112 insertions(+), 50 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9d37795..369ff67 100644 --- a/Dockerfile +++ b/Dockerfile @@ -60,7 +60,7 @@ WORKDIR /app/l5-swagger-app RUN /usr/local/bin/php -dxdebug.mode=off /usr/local/bin/composer config repositories.l5-swagger path '../' -RUN /usr/local/bin/php -dxdebug.mode=off /usr/local/bin/composer require 'DarkaOnLine/l5-swagger:dev-master' +RUN /usr/local/bin/php -dxdebug.mode=off /usr/local/bin/composer require 'darkaonline/l5-swagger:dev-master' RUN ln -s /app/tests/storage/annotations/ app/annotations diff --git a/src/Generator.php b/src/Generator.php index 3820949..4766b2b 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -4,7 +4,7 @@ use Exception; use Illuminate\Support\Arr; -use Illuminate\Support\Facades\File; +use Illuminate\Filesystem\Filesystem; use L5Swagger\Exceptions\L5SwaggerException; use OpenApi\Annotations\OpenApi; use OpenApi\Annotations\Server; @@ -84,6 +84,11 @@ class Generator */ protected $scanOptions; + /** + * @var ?Filesystem + */ + protected $fileSystem; + /** * Generator constructor. * @@ -98,7 +103,8 @@ public function __construct( array $constants, bool $yamlCopyRequired, SecurityDefinitions $security, - array $scanOptions + array $scanOptions, + ?Filesystem $filesystem = null ) { $this->annotationsDir = $paths['annotations']; $this->docDir = $paths['docs']; @@ -110,6 +116,8 @@ public function __construct( $this->yamlCopyRequired = $yamlCopyRequired; $this->security = $security; $this->scanOptions = $scanOptions; + + $this->fileSystem = $filesystem ?? new Filesystem(); } /** @@ -134,15 +142,15 @@ public function generateDocs(): void */ protected function prepareDirectory(): self { - if (File::exists($this->docDir) && ! File::isWritable($this->docDir)) { + if ($this->fileSystem->exists($this->docDir) && ! $this->fileSystem->isWritable($this->docDir)) { throw new L5SwaggerException('Documentation storage directory is not writable'); } - if (! File::exists($this->docDir)) { - File::makeDirectory($this->docDir); + if (! $this->fileSystem->exists($this->docDir)) { + $this->fileSystem->makeDirectory($this->docDir); } - if (! File::exists($this->docDir)) { + if (! $this->fileSystem->exists($this->docDir)) { throw new L5SwaggerException('Documentation storage directory could not be created'); } @@ -293,13 +301,13 @@ protected function makeYamlCopy(): void { if ($this->yamlCopyRequired) { $yamlDocs = (new YamlDumper(2))->dump( - json_decode(file_get_contents($this->docsFile), true), + json_decode($this->fileSystem->get($this->docsFile), true), 20, 0, Yaml::DUMP_OBJECT_AS_MAP ^ Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE ); - file_put_contents( + $this->fileSystem->put( $this->yamlDocsFile, $yamlDocs ); diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 540cfc2..d69239c 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -20,19 +20,22 @@ public function itThrowsExceptionIfDocumentationDirIsNotWritable(): void $config = $this->configFactory->documentationConfig(); $docs = $config['paths']['docs']; - File::shouldReceive('exists') - ->once() + $this->fileSystem + ->expects($this->once()) + ->method('exists') ->with($docs) - ->andReturn(true); + ->willReturn(true); - File::shouldReceive('isWritable') - ->once() + $this->fileSystem + ->expects($this->once()) + ->method('isWritable') ->with($docs) - ->andReturn(false); + ->willReturn(false); $this->expectException(L5SwaggerException::class); $this->expectExceptionMessage('Documentation storage directory is not writable'); + $this->makeGeneratorWithMockedFileSystem(); $this->generator->generateDocs(); } @@ -44,22 +47,26 @@ public function itWillCreateDocumentationDirIfItIsWritable(): void $config = $this->configFactory->documentationConfig(); $docs = $config['paths']['docs']; - File::shouldReceive('exists') - ->times(3) + $this->fileSystem + ->expects($this->exactly(3)) + ->method('exists') ->with($docs) - ->andReturnValues([true, false, true]); + ->willReturnOnConsecutiveCalls(true, false, true); - File::shouldReceive('isWritable') - ->once() + $this->fileSystem + ->expects($this->once()) + ->method('isWritable') ->with($docs) - ->andReturn(true); + ->willReturn(true); - File::shouldReceive('makeDirectory') - ->once() + $this->fileSystem + ->expects($this->once()) + ->method('makeDirectory') ->with($docs); mkdir($docs, 0777); + $this->makeGeneratorWithMockedFileSystem(); $this->generator->generateDocs(); } @@ -71,23 +78,28 @@ public function itThrowsExceptionIfDocumentationDirWasNotCreated(): void $config = $this->configFactory->documentationConfig(); $docs = $config['paths']['docs']; - File::shouldReceive('exists') - ->times(3) + + $this->fileSystem + ->expects($this->exactly(3)) + ->method('exists') ->with($docs) - ->andReturnValues([true, false, false]); + ->willReturnOnConsecutiveCalls(true, false, false); - File::shouldReceive('isWritable') - ->once() + $this->fileSystem + ->expects($this->once()) + ->method('isWritable') ->with($docs) - ->andReturn(true); + ->willReturn(true); - File::shouldReceive('makeDirectory') - ->once() + $this->fileSystem + ->expects($this->once()) + ->method('makeDirectory') ->with($docs); $this->expectException(L5SwaggerException::class); $this->expectExceptionMessage('Documentation storage directory could not be created'); + $this->makeGeneratorWithMockedFileSystem(); $this->generator->generateDocs(); } diff --git a/tests/TestCase.php b/tests/TestCase.php index 5d523b7..5602761 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,12 +2,14 @@ namespace Tests; +use Illuminate\Filesystem\Filesystem; use Illuminate\Foundation\Application; use L5Swagger\ConfigFactory; use L5Swagger\Exceptions\L5SwaggerException; use L5Swagger\Generator; use L5Swagger\L5SwaggerServiceProvider; use Orchestra\Testbench\TestCase as OrchestraTestCase; +use PHPUnit\Framework\MockObject\MockObject; class TestCase extends OrchestraTestCase { @@ -26,6 +28,23 @@ class TestCase extends OrchestraTestCase */ protected $generator; + /** + * @var MockObject|Filesystem + */ + protected $fileSystem; + + /** + * @before + * @return void + */ + public function setUpFileSystem(): void + { + $this->fileSystem = $this->createMock(Filesystem::class); + } + + /** + * @return void + */ public function setUp(): void { parent::setUp(); @@ -38,19 +57,21 @@ public function setUp(): void public function tearDown(): void { + $fileSystem = new Filesystem(); + try { $config = $this->configFactory->documentationConfig(); - if (file_exists($this->jsonDocsFile())) { - unlink($this->jsonDocsFile()); + if ($fileSystem->exists($this->jsonDocsFile())) { + $fileSystem->delete($this->jsonDocsFile()); } - if (file_exists($this->yamlDocsFile())) { - unlink($this->yamlDocsFile()); + if ($fileSystem->exists($this->yamlDocsFile())) { + $fileSystem->delete($this->yamlDocsFile()); } - if (file_exists($config['paths']['docs'])) { - rmdir($config['paths']['docs']); + if ($fileSystem->exists($config['paths']['docs'])) { + $fileSystem->deleteDirectory($config['paths']['docs']); } } catch (L5SwaggerException $e) { } @@ -76,7 +97,8 @@ protected function getPackageProviders($app): array */ protected function crateJsonDocumentationFile(): void { - file_put_contents($this->jsonDocsFile(), '{}'); + $fileSystem = new Filesystem(); + $fileSystem->put($this->jsonDocsFile(), '{}'); } /** @@ -86,7 +108,8 @@ protected function crateJsonDocumentationFile(): void */ protected function createYamlDocumentationFile(): void { - file_put_contents($this->yamlDocsFile(), ''); + $fileSystem = new Filesystem(); + $fileSystem->put($this->yamlDocsFile(), ''); } /** @@ -98,11 +121,12 @@ protected function createYamlDocumentationFile(): void */ protected function jsonDocsFile(): string { + $fileSystem = new Filesystem(); $config = $this->configFactory->documentationConfig(); $docs = $config['paths']['docs']; - if (! is_dir($docs)) { - mkdir($docs); + if (! $fileSystem->isDirectory($docs)) { + $fileSystem->makeDirectory($docs); } return $docs.DIRECTORY_SEPARATOR.$config['paths']['docs_json']; @@ -117,11 +141,12 @@ protected function jsonDocsFile(): string */ protected function yamlDocsFile(): string { + $fileSystem = new Filesystem(); $config = $this->configFactory->documentationConfig(); $docs = $config['paths']['docs']; - if (! is_dir($docs)) { - mkdir($docs); + if (! $fileSystem->isDirectory($docs)) { + $fileSystem->makeDirectory($docs); } return $docs.DIRECTORY_SEPARATOR.$config['paths']['docs_yaml']; @@ -167,6 +192,20 @@ protected function makeGenerator(): void $this->generator = $this->app->make(Generator::class); } + /** + * @return void + */ + protected function makeGeneratorWithMockedFileSystem(): void + { + $this->generator = $this->app->make(Generator::class); + + $reflectionObject = new \ReflectionObject($this->generator); + $reflectionProperty = $reflectionObject->getProperty('fileSystem'); + $reflectionProperty->setAccessible(true); + + $reflectionProperty->setValue($this->generator, $this->fileSystem); + } + /** * @param string $fileName * @param string $type @@ -199,27 +238,30 @@ protected function setCustomDocsFileName(string $fileName, string $type = 'json' */ protected function copyAssets(): void { + $fileSystem = new Filesystem(); $src = __DIR__.'/../vendor/swagger-api/swagger-ui/dist/'; $destination = __DIR__.'/../vendor/orchestra/testbench-core/laravel/vendor/swagger-api/swagger-ui/dist/'; - if (! is_dir($destination)) { + if (! $fileSystem->isDirectory($destination)) { $base = realpath( - __DIR__.'/../vendor/orchestra/testbench-core/laravel/vendor' + __DIR__.'/../vendor/orchestra/testbench-core' ); - mkdir($base = $base.'/swagger-api'); - mkdir($base = $base.'/swagger-ui'); - mkdir($base = $base.'/dist'); + $fileSystem->makeDirectory( + $base.'/laravel/vendor/swagger-api/swagger-ui/dist' + ,0777, + true + ); } foreach (scandir($src) as $file) { $filePath = $src.$file; - if (! is_readable($filePath) || is_dir($filePath)) { + if (! $fileSystem->isReadable($filePath) || $fileSystem->isDirectory($filePath)) { continue; } - copy( + $fileSystem->copy( $filePath, $destination.$file ); From 164d2667775d56fc98ebbc210149cff765e4a830 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 17 Mar 2022 11:27:53 +0000 Subject: [PATCH 3/7] Apply fixes from StyleCI --- src/Generator.php | 2 +- tests/GeneratorTest.php | 2 -- tests/TestCase.php | 6 +++--- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Generator.php b/src/Generator.php index 4766b2b..58d10f2 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -3,8 +3,8 @@ namespace L5Swagger; use Exception; -use Illuminate\Support\Arr; use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Arr; use L5Swagger\Exceptions\L5SwaggerException; use OpenApi\Annotations\OpenApi; use OpenApi\Annotations\Server; diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index d69239c..a711df6 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -3,7 +3,6 @@ namespace Tests; use Illuminate\Http\Request; -use Illuminate\Support\Facades\File; use L5Swagger\Exceptions\L5SwaggerException; use OpenApi\Analysers\TokenAnalyser; use OpenApi\Processors\CleanUnmerged; @@ -78,7 +77,6 @@ public function itThrowsExceptionIfDocumentationDirWasNotCreated(): void $config = $this->configFactory->documentationConfig(); $docs = $config['paths']['docs']; - $this->fileSystem ->expects($this->exactly(3)) ->method('exists') diff --git a/tests/TestCase.php b/tests/TestCase.php index 5602761..6972a29 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -35,6 +35,7 @@ class TestCase extends OrchestraTestCase /** * @before + * * @return void */ public function setUpFileSystem(): void @@ -199,7 +200,7 @@ protected function makeGeneratorWithMockedFileSystem(): void { $this->generator = $this->app->make(Generator::class); - $reflectionObject = new \ReflectionObject($this->generator); + $reflectionObject = new \ReflectionObject($this->generator); $reflectionProperty = $reflectionObject->getProperty('fileSystem'); $reflectionProperty->setAccessible(true); @@ -248,8 +249,7 @@ protected function copyAssets(): void ); $fileSystem->makeDirectory( - $base.'/laravel/vendor/swagger-api/swagger-ui/dist' - ,0777, + $base.'/laravel/vendor/swagger-api/swagger-ui/dist', 0777, true ); } From bbeea5b653c66eeed9b442ec0a5e725d0e0976af Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Thu, 17 Mar 2022 14:12:30 +0200 Subject: [PATCH 4/7] Use Filesystem instead of File fasade --- .../Controllers/SwaggerAssetController.php | 4 +++- src/Http/Controllers/SwaggerController.php | 21 ++++++++++++------- src/SecurityDefinitions.php | 11 +++++++--- tests/ConsoleTest.php | 10 ++++++--- tests/SecurityDefinitionsTest.php | 5 ++++- 5 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/Http/Controllers/SwaggerAssetController.php b/src/Http/Controllers/SwaggerAssetController.php index abc53f8..02ebcc6 100644 --- a/src/Http/Controllers/SwaggerAssetController.php +++ b/src/Http/Controllers/SwaggerAssetController.php @@ -2,6 +2,7 @@ namespace L5Swagger\Http\Controllers; +use Illuminate\Filesystem\Filesystem; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Routing\Controller as BaseController; @@ -11,13 +12,14 @@ class SwaggerAssetController extends BaseController { public function index(Request $request, $asset) { + $fileSystem = new Filesystem(); $documentation = $request->offsetGet('documentation'); try { $path = swagger_ui_dist_path($documentation, $asset); return (new Response( - file_get_contents($path), + $fileSystem->get($path), 200, [ 'Content-Type' => (pathinfo($asset))['extension'] == 'css' diff --git a/src/Http/Controllers/SwaggerController.php b/src/Http/Controllers/SwaggerController.php index 70b853a..c2d84a6 100644 --- a/src/Http/Controllers/SwaggerController.php +++ b/src/Http/Controllers/SwaggerController.php @@ -2,10 +2,11 @@ namespace L5Swagger\Http\Controllers; +use Illuminate\Contracts\Filesystem\FileNotFoundException; +use Illuminate\Filesystem\Filesystem; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Routing\Controller as BaseController; -use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Request as RequestFacade; use Illuminate\Support\Facades\Response as ResponseFacade; @@ -27,21 +28,23 @@ public function __construct(GeneratorFactory $generatorFactory) /** * Dump api-docs content endpoint. Supports dumping a json, or yaml file. * - * @param Request $request - * @param string $file + * @param Request $request + * @param ?string $file * @return Response * * @throws L5SwaggerException + * @throws FileNotFoundException */ public function docs(Request $request, string $file = null) { + $fileSystem = new Filesystem(); $documentation = $request->offsetGet('documentation'); $config = $request->offsetGet('config'); $targetFile = $config['paths']['docs_json'] ?? 'api-docs.json'; $yaml = false; - if (! is_null($file)) { + if ($file !== null) { $targetFile = $file; $parts = explode('.', $file); @@ -72,11 +75,11 @@ public function docs(Request $request, string $file = null) } } - if (! file_exists($filePath)) { + if (! $fileSystem->exists($filePath)) { abort(404, sprintf('Unable to locate documentation file at: "%s"', $filePath)); } - $content = File::get($filePath); + $content = $fileSystem->get($filePath); if ($yaml) { return ResponseFacade::make($content, 200, [ @@ -129,16 +132,18 @@ public function api(Request $request) /** * Display Oauth2 callback pages. * - * @param Request $request + * @param Request $request * @return string * * @throws L5SwaggerException + * @throws FileNotFoundException */ public function oauth2Callback(Request $request) { + $fileSystem = new Filesystem(); $documentation = $request->offsetGet('documentation'); - return File::get(swagger_ui_dist_path($documentation, 'oauth2-redirect.html')); + return $fileSystem->get(swagger_ui_dist_path($documentation, 'oauth2-redirect.html')); } /** diff --git a/src/SecurityDefinitions.php b/src/SecurityDefinitions.php index 352fd11..9bf2d08 100644 --- a/src/SecurityDefinitions.php +++ b/src/SecurityDefinitions.php @@ -2,6 +2,7 @@ namespace L5Swagger; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Collection; class SecurityDefinitions @@ -31,12 +32,16 @@ public function __construct(array $securitySchemesConfig = [], array $securityCo /** * Reads in the l5-swagger configuration and appends security settings to documentation. * - * @param string $filename The path to the generated json documentation + * @param string $filename The path to the generated json documentation + * + * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ public function generate($filename) { + $fileSystem = new Filesystem(); + $documentation = collect( - json_decode(file_get_contents($filename)) + json_decode($fileSystem->get($filename)) ); if (is_array($this->securitySchemesConfig) && ! empty($this->securitySchemesConfig)) { @@ -47,7 +52,7 @@ public function generate($filename) $documentation = $this->injectSecurity($documentation, $this->securityConfig); } - file_put_contents( + $fileSystem->put( $filename, $documentation->toJson(JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ); diff --git a/tests/ConsoleTest.php b/tests/ConsoleTest.php index 9345004..bc524d0 100644 --- a/tests/ConsoleTest.php +++ b/tests/ConsoleTest.php @@ -2,6 +2,7 @@ namespace Tests; +use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Facades\Artisan; use L5Swagger\Exceptions\L5SwaggerException; @@ -17,13 +18,15 @@ class ConsoleTest extends TestCase */ public function canGenerate(string $artisanCommand): void { + $fileSystem = new Filesystem(); + $this->setAnnotationsPath(); Artisan::call($artisanCommand); $this->assertFileExists($this->jsonDocsFile()); - $fileContent = file_get_contents($this->jsonDocsFile()); + $fileContent = $fileSystem->get($this->jsonDocsFile()); $this->assertJson($fileContent); $this->assertStringContainsString('L5 Swagger', $fileContent); @@ -49,11 +52,12 @@ public function provideGenerateCommands(): iterable */ public function canPublish(): void { + $fileSystem = new Filesystem(); Artisan::call('vendor:publish', ['--provider' => 'L5Swagger\L5SwaggerServiceProvider']); $config = $this->configFactory->documentationConfig(); - $this->assertTrue(file_exists(config_path('l5-swagger.php'))); - $this->assertTrue(file_exists($config['paths']['views'].'/index.blade.php')); + $this->assertTrue($fileSystem->exists(config_path('l5-swagger.php'))); + $this->assertTrue($fileSystem->exists($config['paths']['views'].'/index.blade.php')); } } diff --git a/tests/SecurityDefinitionsTest.php b/tests/SecurityDefinitionsTest.php index 65ed2f4..4d267ac 100644 --- a/tests/SecurityDefinitionsTest.php +++ b/tests/SecurityDefinitionsTest.php @@ -2,6 +2,7 @@ namespace Tests; +use Illuminate\Filesystem\Filesystem; use L5Swagger\Exceptions\L5SwaggerException; class SecurityDefinitionsTest extends TestCase @@ -19,6 +20,8 @@ public function canGenerateApiJsonFileWithSecurityDefinition( array $securitySchemes, array $security ): void { + $fileSystem = new Filesystem(); + $this->setAnnotationsPath(); $config = config('l5-swagger.documentations.default'); @@ -36,7 +39,7 @@ public function canGenerateApiJsonFileWithSecurityDefinition( $this->generator->generateDocs(); - $this->assertTrue(file_exists($this->jsonDocsFile())); + $this->assertTrue($fileSystem->exists($this->jsonDocsFile())); $this->get(route('l5-swagger.default.docs')) ->assertSee('new_api_key_securitye') From 451e82d33500297058ce9b6aa7c9101c63b8cf30 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Thu, 17 Mar 2022 12:12:54 +0000 Subject: [PATCH 5/7] Apply fixes from StyleCI --- src/Http/Controllers/SwaggerController.php | 6 +++--- src/SecurityDefinitions.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Http/Controllers/SwaggerController.php b/src/Http/Controllers/SwaggerController.php index c2d84a6..415d739 100644 --- a/src/Http/Controllers/SwaggerController.php +++ b/src/Http/Controllers/SwaggerController.php @@ -28,8 +28,8 @@ public function __construct(GeneratorFactory $generatorFactory) /** * Dump api-docs content endpoint. Supports dumping a json, or yaml file. * - * @param Request $request - * @param ?string $file + * @param Request $request + * @param ?string $file * @return Response * * @throws L5SwaggerException @@ -132,7 +132,7 @@ public function api(Request $request) /** * Display Oauth2 callback pages. * - * @param Request $request + * @param Request $request * @return string * * @throws L5SwaggerException diff --git a/src/SecurityDefinitions.php b/src/SecurityDefinitions.php index 9bf2d08..b2f8070 100644 --- a/src/SecurityDefinitions.php +++ b/src/SecurityDefinitions.php @@ -32,7 +32,7 @@ public function __construct(array $securitySchemesConfig = [], array $securityCo /** * Reads in the l5-swagger configuration and appends security settings to documentation. * - * @param string $filename The path to the generated json documentation + * @param string $filename The path to the generated json documentation * * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException */ From d844f04cad1523d1eee96c9dd89cfb63f01bbaa0 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Thu, 17 Mar 2022 14:19:28 +0200 Subject: [PATCH 6/7] Use Filesystem instead of File fasade --- tests/TestCase.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/TestCase.php b/tests/TestCase.php index 6972a29..6625405 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -10,6 +10,7 @@ use L5Swagger\L5SwaggerServiceProvider; use Orchestra\Testbench\TestCase as OrchestraTestCase; use PHPUnit\Framework\MockObject\MockObject; +use ReflectionObject; class TestCase extends OrchestraTestCase { @@ -200,7 +201,7 @@ protected function makeGeneratorWithMockedFileSystem(): void { $this->generator = $this->app->make(Generator::class); - $reflectionObject = new \ReflectionObject($this->generator); + $reflectionObject = new ReflectionObject($this->generator); $reflectionProperty = $reflectionObject->getProperty('fileSystem'); $reflectionProperty->setAccessible(true); @@ -249,7 +250,8 @@ protected function copyAssets(): void ); $fileSystem->makeDirectory( - $base.'/laravel/vendor/swagger-api/swagger-ui/dist', 0777, + $base.'/laravel/vendor/swagger-api/swagger-ui/dist', + 0777, true ); } From 86c5f9db1de82f2a0b716d1d9cface862a798d56 Mon Sep 17 00:00:00 2001 From: Darius Matulionis Date: Thu, 31 Mar 2022 09:30:18 +0300 Subject: [PATCH 7/7] Do not add empty security items to documentation --- src/SecurityDefinitions.php | 21 ++++++++++------- tests/SecurityDefinitionsTest.php | 39 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/SecurityDefinitions.php b/src/SecurityDefinitions.php index b2f8070..b8f2378 100644 --- a/src/SecurityDefinitions.php +++ b/src/SecurityDefinitions.php @@ -2,6 +2,7 @@ namespace L5Swagger; +use Illuminate\Contracts\Filesystem\FileNotFoundException; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Collection; @@ -34,9 +35,9 @@ public function __construct(array $securitySchemesConfig = [], array $securityCo * * @param string $filename The path to the generated json documentation * - * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException + * @throws FileNotFoundException */ - public function generate($filename) + public function generate(string $filename): void { $fileSystem = new Filesystem(); @@ -65,7 +66,7 @@ public function generate($filename) * @param array $config The securityScheme settings from l5-swagger * @return Collection */ - protected function injectSecuritySchemes(Collection $documentation, array $config) + protected function injectSecuritySchemes(Collection $documentation, array $config): Collection { $components = collect(); if ($documentation->has('components')) { @@ -78,7 +79,7 @@ protected function injectSecuritySchemes(Collection $documentation, array $confi } foreach ($config as $key => $cfg) { - $securitySchemes->offsetSet($key, self::arrayToObject($cfg)); + $securitySchemes->offsetSet($key, $this->arrayToObject($cfg)); } $components->offsetSet('securitySchemes', $securitySchemes); @@ -95,7 +96,7 @@ protected function injectSecuritySchemes(Collection $documentation, array $confi * @param array $config The security settings from l5-swagger * @return Collection */ - protected function injectSecurity(Collection $documentation, array $config) + protected function injectSecurity(Collection $documentation, array $config): Collection { $security = collect(); if ($documentation->has('security')) { @@ -103,10 +104,14 @@ protected function injectSecurity(Collection $documentation, array $config) } foreach ($config as $key => $cfg) { - $security->offsetSet($key, self::arrayToObject($cfg)); + if (!empty($cfg)) { + $security->put($key, $this->arrayToObject($cfg)); + } } - $documentation->offsetSet('security', $security); + if ($security->count()) { + $documentation->offsetSet('security', $security); + } return $documentation; } @@ -117,7 +122,7 @@ protected function injectSecurity(Collection $documentation, array $config) * @param $array * @return object */ - public static function arrayToObject($array) + protected function arrayToObject($array) { return json_decode(json_encode($array)); } diff --git a/tests/SecurityDefinitionsTest.php b/tests/SecurityDefinitionsTest.php index 4d267ac..8725b48 100644 --- a/tests/SecurityDefinitionsTest.php +++ b/tests/SecurityDefinitionsTest.php @@ -7,6 +7,45 @@ class SecurityDefinitionsTest extends TestCase { + /** + * @test + * + * @throws L5SwaggerException + */ + public function itWillNotAddEmptySecurityItems(): void { + $fileSystem = new Filesystem(); + + $this->setAnnotationsPath(); + + $defaultConfig = config('l5-swagger.defaults'); + $defaultConfig['securityDefinitions']['securitySchemes'] = [[]]; + $defaultConfig['securityDefinitions']['security'] = [[]]; + + $config = config('l5-swagger.documentations.default'); + + $config['securityDefinitions']['securitySchemes'] = [[]]; + $config['securityDefinitions']['security'] = [[]]; + + config(['l5-swagger' => [ + 'default' => 'default', + 'documentations' => [ + 'default' => $config, + ], + 'defaults' => $defaultConfig, + ]]); + + $this->generator->generateDocs(); + + $this->assertTrue($fileSystem->exists($this->jsonDocsFile())); + + $this->get(route('l5-swagger.default.docs')) + ->assertSee('oauth2') // From annotations + ->assertSee('read:oauth2') // From annotations + ->assertJsonMissing(['securitySchemes' => []]) + ->assertJsonMissing(['security' => []]) + ->isOk(); + } + /** * @test * @dataProvider provideConfigAndSchemes