Skip to content

Commit

Permalink
Merge pull request #108 from RonasIT/45-throw-exception-for-empty-or-…
Browse files Browse the repository at this point in the history
…not-exists-files

Throw exception for empty or not exists files
  • Loading branch information
DenTray authored Oct 23, 2023
2 parents 0bd312f + ee086d8 commit e53c27c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
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

0 comments on commit e53c27c

Please sign in to comment.