Skip to content

Commit

Permalink
Merge pull request #103 from RonasIT/config-logic-update
Browse files Browse the repository at this point in the history
Config logic update
  • Loading branch information
DenTray authored Sep 18, 2023
2 parents a29e8f4 + 271ba0e commit c33ac24
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .deepsource.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ exclude_patterns = [
[[analyzers]]
name = "php"

[analyzers.meta]
skip_doc_coverage = ["class", "magic"]

[[transformers]]
name = "php-cs-fixer"
4 changes: 2 additions & 2 deletions config/auto-doc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '[email protected]'
'email' => null
],
'license' => [
'name' => '',
Expand Down
13 changes: 13 additions & 0 deletions src/Exceptions/EmptyContactEmailException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Support\AutoDoc\Exceptions;

use Exception;

class EmptyContactEmailException extends Exception
{
public function __construct()
{
parent::__construct('Please fill the `contact.email` field in the app-doc.php config file.');
}
}
5 changes: 5 additions & 0 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use ReflectionClass;
use RonasIT\Support\AutoDoc\Exceptions\EmptyContactEmailException;
use RonasIT\Support\AutoDoc\Exceptions\InvalidDriverClassException;
use RonasIT\Support\AutoDoc\Exceptions\LegacyConfigException;
use RonasIT\Support\AutoDoc\Exceptions\SwaggerDriverClassNotFoundException;
Expand Down Expand Up @@ -104,6 +105,10 @@ protected function setDriver()

protected function generateEmptyData(): array
{
if (!Arr::get($this->config, 'contact.email')) {
throw new EmptyContactEmailException();
}

$data = [
'swagger' => Arr::get($this->config, 'swagger.version'),
'host' => $this->getAppUrl(),
Expand Down
85 changes: 70 additions & 15 deletions tests/SwaggerServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 [
Expand Down Expand Up @@ -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);

Expand Down
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class TestCase extends BaseTest
{
protected $globalExportMode = false;

public function setUp(): void
{
parent::setUp();

config(['auto-doc.contact.email' => '[email protected]']);
}

public function tearDown(): void
{
parent::tearDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"title": "Name of Your Application",
"termsOfService": "",
"contact": {
"email": "[email protected]"
"email": null
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"swagger": "2.0",
"host": "localhost",
"basePath": "\/",
"schemes": [],
"paths": [],
"definitions": []
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
15 changes: 11 additions & 4 deletions tests/support/Traits/SwaggerServiceMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit c33ac24

Please sign in to comment.