diff --git a/.deepsource.toml b/.deepsource.toml index 70d9db91..2f5679ad 100644 --- a/.deepsource.toml +++ b/.deepsource.toml @@ -11,5 +11,8 @@ exclude_patterns = [ [[analyzers]] name = "php" +[analyzers.meta] +skip_doc_coverage = ["class", "magic"] + [[transformers]] name = "php-cs-fixer" \ No newline at end of file diff --git a/config/auto-doc.php b/config/auto-doc.php index 33a1004c..434d915e 100644 --- a/config/auto-doc.php +++ b/config/auto-doc.php @@ -44,10 +44,10 @@ */ 'description' => 'auto-doc::swagger-description', 'version' => '0.0.0', - 'title' => 'Name of Your Application', + 'title' => env('APP_NAME', 'Name of Your Application'), 'termsOfService' => '', 'contact' => [ - 'email' => 'your@email.com' + 'email' => null ], 'license' => [ 'name' => '', diff --git a/src/Exceptions/EmptyContactEmailException.php b/src/Exceptions/EmptyContactEmailException.php new file mode 100644 index 00000000..7c4c173d --- /dev/null +++ b/src/Exceptions/EmptyContactEmailException.php @@ -0,0 +1,13 @@ +config, 'contact.email')) { + throw new EmptyContactEmailException(); + } + $data = [ 'swagger' => Arr::get($this->config, 'swagger.version'), 'host' => $this->getAppUrl(), diff --git a/tests/SwaggerServiceTest.php b/tests/SwaggerServiceTest.php index 4206954f..89eca052 100755 --- a/tests/SwaggerServiceTest.php +++ b/tests/SwaggerServiceTest.php @@ -3,6 +3,7 @@ namespace RonasIT\Support\Tests; use Illuminate\Http\Testing\File; +use RonasIT\Support\AutoDoc\Exceptions\EmptyContactEmailException; use RonasIT\Support\AutoDoc\Exceptions\InvalidDriverClassException; use RonasIT\Support\AutoDoc\Exceptions\LegacyConfigException; use RonasIT\Support\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException; @@ -51,6 +52,55 @@ public function testConstructorDriverClassNotImplementsInterface() app(SwaggerService::class); } + public function testEmptyContactEmail() + { + config(['auto-doc.contact.email' => null]); + + $this->expectException(EmptyContactEmailException::class); + + app(SwaggerService::class); + } + + public function getAddEmptyData(): array + { + return [ + [ + 'security' => 'laravel', + 'savedTmpDataFixture' => 'tmp_data_request_with_empty_data_laravel', + ], + [ + 'security' => 'jwt', + 'savedTmpDataFixture' => 'tmp_data_request_with_empty_data_jwt', + ], + ]; + } + + /** + * @dataProvider getAddEmptyData + * + * @param string $security + * @param string $savedTmpDataFixture + */ + public function testAddDataRequestWithEmptyDataLaravel(string $security, string $savedTmpDataFixture) + { + config([ + 'auto-doc.security' => $security, + ]); + + $this->mockDriverGetEmptyAndSaveTpmData([], $this->getJsonFixture($savedTmpDataFixture)); + + app(SwaggerService::class); + } + + public function testAddDataRequestWithEmptyDataAndInfo() + { + config(['auto-doc.info' => []]); + + $this->mockDriverGetEmptyAndSaveTpmData([], $this->getJsonFixture('tmp_data_request_with_empty_data_and_info')); + + app(SwaggerService::class); + } + public function getAddData(): array { return [ @@ -131,26 +181,31 @@ public function testAddDataRequestWithAnnotations() $service->addData($request, $response); } - public function testAddDataWithJWTSecurity() + public function addDataWithSecurity(): array { - config(['auto-doc.security' => 'jwt']); - - $this->mockDriverGetEmptyAndSaveTpmData($this->getJsonFixture('tmp_data_search_roles_request_jwt_security')); - - $service = app(SwaggerService::class); - - $request = $this->generateGetRolesRequest(); - - $response = $this->generateResponse('example_success_roles_response.json'); - - $service->addData($request, $response); + return [ + [ + 'security' => 'laravel', + 'requestFixture' => 'tmp_data_search_roles_request_laravel_security', + ], + [ + 'security' => 'jwt', + 'requestFixture' => 'tmp_data_search_roles_request_jwt_security', + ], + ]; } - public function testAddDataWithLaravelSecurity() + /** + * @dataProvider addDataWithSecurity + * + * @param string $security + * @param string $requestFixture + */ + public function testAddDataWithJWTSecurity(string $security, string $requestFixture) { - config(['auto-doc.security' => 'laravel']); + config(['auto-doc.security' => $security]); - $this->mockDriverGetEmptyAndSaveTpmData($this->getJsonFixture('tmp_data_search_roles_request_laravel_security')); + $this->mockDriverGetEmptyAndSaveTpmData($this->getJsonFixture($requestFixture)); $service = app(SwaggerService::class); diff --git a/tests/TestCase.php b/tests/TestCase.php index 9961ff60..c30f0c36 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -17,6 +17,13 @@ class TestCase extends BaseTest { protected $globalExportMode = false; + public function setUp(): void + { + parent::setUp(); + + config(['auto-doc.contact.email' => 'your@mail.com']); + } + public function tearDown(): void { parent::tearDown(); diff --git a/tests/fixtures/PushDocumentationCommandTest/documentation.json b/tests/fixtures/PushDocumentationCommandTest/documentation.json index 88318710..8ed2a650 100644 --- a/tests/fixtures/PushDocumentationCommandTest/documentation.json +++ b/tests/fixtures/PushDocumentationCommandTest/documentation.json @@ -11,7 +11,7 @@ "title": "Name of Your Application", "termsOfService": "", "contact": { - "email": "your@email.com" + "email": null } } } \ No newline at end of file diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_and_info.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_and_info.json new file mode 100644 index 00000000..9d54ef1e --- /dev/null +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_and_info.json @@ -0,0 +1,8 @@ +{ + "swagger": "2.0", + "host": "localhost", + "basePath": "\/", + "schemes": [], + "paths": [], + "definitions": [] +} \ No newline at end of file diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_jwt.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_jwt.json new file mode 100644 index 00000000..91d39624 --- /dev/null +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_jwt.json @@ -0,0 +1,24 @@ +{ + "swagger": "2.0", + "host": "localhost", + "basePath": "\/", + "schemes": [], + "paths": [], + "definitions": [], + "info": { + "description": "This is automatically collected documentation", + "version": "0.0.0", + "title": "Name of Your Application", + "termsOfService": "", + "contact": { + "email": null + } + }, + "securityDefinitions": { + "jwt": { + "type": "apiKey", + "name": "authorization", + "in": "header" + } + } +} \ No newline at end of file diff --git a/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_laravel.json b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_laravel.json new file mode 100644 index 00000000..c7b54714 --- /dev/null +++ b/tests/fixtures/SwaggerServiceTest/tmp_data_request_with_empty_data_laravel.json @@ -0,0 +1,24 @@ +{ + "swagger": "2.0", + "host": "localhost", + "basePath": "\/", + "schemes": [], + "paths": [], + "definitions": [], + "info": { + "description": "This is automatically collected documentation", + "version": "0.0.0", + "title": "Name of Your Application", + "termsOfService": "", + "contact": { + "email": null + } + }, + "securityDefinitions": { + "laravel": { + "type": "apiKey", + "name": "Cookie", + "in": "header" + } + } +} \ No newline at end of file diff --git a/tests/support/Traits/SwaggerServiceMockTrait.php b/tests/support/Traits/SwaggerServiceMockTrait.php index 2c7c8ba4..7a764bec 100644 --- a/tests/support/Traits/SwaggerServiceMockTrait.php +++ b/tests/support/Traits/SwaggerServiceMockTrait.php @@ -8,19 +8,26 @@ trait SwaggerServiceMockTrait { use MockTrait; - protected function mockDriverGetEmptyAndSaveTpmData($tmpData, $driverClass = LocalDriver::class) - { + protected function mockDriverGetEmptyAndSaveTpmData( + $tmpData, + $savedTmpData = null, + $driverClass = LocalDriver::class + ) { $driver = $this->mockClass($driverClass, ['getTmpData', 'saveTmpData']); $driver ->expects($this->exactly(1)) ->method('getTmpData') - ->willReturn(array_merge($tmpData, ['paths' => [], 'definitions' => []])); + ->willReturn( + empty($tmpData) + ? $tmpData + : array_merge($tmpData, ['paths' => [], 'definitions' => []]) + ); $driver ->expects($this->exactly(1)) ->method('saveTmpData') - ->with($tmpData); + ->with($savedTmpData ?? $tmpData); $this->app->instance($driverClass, $driver); }