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

feat: catch and report exception during Open-Api file validation (#45) #109

Merged
merged 2 commits into from
Oct 27, 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"require-dev": {
"orchestra/testbench": "^6.25",
"php-coveralls/php-coveralls": "^2.5"
"php-coveralls/php-coveralls": "^2.5",
"php-mock/php-mock-phpunit": "^2.7"
},
"autoload": {
"psr-4": {
Expand Down
212 changes: 210 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions src/Services/SwaggerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -697,19 +697,19 @@ public function getDocFileContent()
foreach ($additionalDocs as $filePath) {
$fullFilePath = base_path($filePath);

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

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

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

try {
$this->validateSpec($fileContent);
} catch (InvalidSwaggerSpecException $exception) {
} catch (DocFileNotExistsException|EmptyDocFileException|InvalidSwaggerSpecException $exception) {
report($exception);

continue;
Expand Down
33 changes: 24 additions & 9 deletions tests/AutoDocControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace RonasIT\Support\Tests;

use Illuminate\Http\Response;
use RonasIT\Support\AutoDoc\Exceptions\DocFileNotExistsException;
use RonasIT\Support\AutoDoc\Exceptions\EmptyDocFileException;
use phpmock\phpunit\PHPMock;
use RonasIT\Support\Tests\Support\Traits\MockTrait;

class AutoDocControllerTest extends TestCase
{
use MockTrait;
use PHPMock;

protected $documentation;
protected $localDriverFilePath;

Expand Down Expand Up @@ -52,32 +55,44 @@ public function testGetJSONDocumentationWithAdditionalPaths()
$this->assertEqualsJsonFixture('tmp_data_with_additional_paths', $response->json());
}

public function getJSONDocumentationDoesntExist()
public function testGetJSONDocumentationDoesntExist()
{
$mock = $this->getFunctionMock('RonasIT\Support\AutoDoc\Services', 'report');
$mock->expects($this->once());

config([
'auto-doc.additional_paths' => ['invalid_path/non_existent_file.json']
]);

$this->expectException(DocFileNotExistsException::class);
$response = $this->json('get', '/auto-doc/documentation');

$response->assertStatus(Response::HTTP_OK);

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

public function getJSONDocumentationIsEmpty()
public function testGetJSONDocumentationIsEmpty()
{
$mock = $this->getFunctionMock('RonasIT\Support\AutoDoc\Services', 'report');
$mock->expects($this->once());

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

$this->expectException(EmptyDocFileException::class);
$response = $this->json('get', '/auto-doc/documentation');

$this->json('get', '/auto-doc/documentation');
$response->assertStatus(Response::HTTP_OK);

$response->assertJson($this->documentation);
}

public function testGetJSONDocumentationInvalidAdditionalDoc()
{
config([
'auto-doc.additional_paths' => ['tests/fixtures/AutoDocControllerTest/documentation__invalid_format__missing_field__paths.json']
'auto-doc.additional_paths' => [
'tests/fixtures/AutoDocControllerTest/documentation__invalid_format__missing_field__paths.json'
]
]);

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