Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw exception for empty or not exists files #108

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Exceptions/DocFileNotExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Support\AutoDoc\Exceptions;

use Exception;

class DocFileNotExistsException extends Exception
{
public function __construct(string $filename)
{
parent::__construct("Doc file '{$filename}' doesn't exist.");
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/EmptyDocFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace RonasIT\Support\AutoDoc\Exceptions;

use Exception;

class EmptyDocFileException extends Exception
{
public function __construct(string $filename)
{
parent::__construct("Doc file '{$filename}' is empty.");
}
}
10 changes: 8 additions & 2 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use ReflectionClass;
use RonasIT\Support\AutoDoc\Exceptions\DocFileNotExistsException;
use RonasIT\Support\AutoDoc\Exceptions\EmptyContactEmailException;
use RonasIT\Support\AutoDoc\Exceptions\EmptyDocFileException;
use RonasIT\Support\AutoDoc\Exceptions\InvalidDriverClassException;
use RonasIT\Support\AutoDoc\Exceptions\LegacyConfigException;
use RonasIT\Support\AutoDoc\Exceptions\SpecValidation\InvalidSwaggerSpecException;
Expand Down Expand Up @@ -669,6 +671,10 @@ protected function getActionName($uri): string
return Str::camel($action);
}

/**
* @deprecated method is not in use
* @codeCoverageIgnore
*/
protected function saveTempData()
{
$exportFile = Arr::get($this->config, 'files.temporary');
Expand All @@ -692,13 +698,13 @@ public function getDocFileContent()
$fullFilePath = base_path($filePath);

if (!file_exists($fullFilePath)) {
continue;
throw new DocFileNotExistsException($fullFilePath);
}

$fileContent = json_decode(file_get_contents($fullFilePath), true);

if (empty($fileContent)) {
continue;
throw new EmptyDocFileException($fullFilePath);
}

try {
Expand Down
42 changes: 22 additions & 20 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace RonasIT\Support\Tests;

use Illuminate\Http\Response;
use RonasIT\Support\AutoDoc\Exceptions\DocFileNotExistsException;
use RonasIT\Support\AutoDoc\Exceptions\EmptyDocFileException;

class AutoDocControllerTest extends TestCase
{
Expand Down Expand Up @@ -50,32 +52,32 @@ public function testGetJSONDocumentationWithAdditionalPaths()
$this->assertEqualsJsonFixture('tmp_data_with_additional_paths', $response->json());
}

public function getJSONDocumentationInvalidAdditionalDoc(): array
public function getJSONDocumentationDoesntExist()
{
$basePath = 'tests/fixtures/AutoDocControllerTest';
config([
'auto-doc.additional_paths' => ['invalid_path/non_existent_file.json']
]);

$this->expectException(DocFileNotExistsException::class);

$this->json('get', '/auto-doc/documentation');
}

public function getJSONDocumentationIsEmpty()
{
config([
'auto-doc.additional_paths' => ['tests/fixtures/AutoDocControllerTest/documentation__non_json.txt']
]);

$this->expectException(EmptyDocFileException::class);

return [
[
'additionalDocPath' => 'invalid_path/non_existent_file.json'
],
[
'additionalDocPath' => $basePath . '/documentation__non_json.txt'
],
[
'additionalDocPath' => $basePath. '/documentation__invalid_format__missing_field__paths.json'
]
];
$this->json('get', '/auto-doc/documentation');
}

/**
* @dataProvider getJSONDocumentationInvalidAdditionalDoc
*
* @param string $additionalDocPath
*/
public function testGetJSONDocumentationInvalidAdditionalDoc(string $additionalDocPath)
public function testGetJSONDocumentationInvalidAdditionalDoc()
{
config([
'auto-doc.additional_paths' => [$additionalDocPath]
'auto-doc.additional_paths' => ['tests/fixtures/AutoDocControllerTest/documentation__invalid_format__missing_field__paths.json']
]);

$response = $this->json('get', '/auto-doc/documentation');
Expand Down