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

adding more tests and updating dependencies #1

Merged
merged 3 commits into from
Dec 20, 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
}
},
"require": {
"php": "^8.2.0",
"minicli/minicli": "^4.2",
"minicli/stencil": "^0.2.1"
},
Expand Down
559 changes: 259 additions & 300 deletions composer.lock

Large diffs are not rendered by default.

14 changes: 12 additions & 2 deletions src/DataFeed/JsonDataFeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Autodocs\DataFeed;

use Autodocs\Exception\JsonException;
use Autodocs\Exception\NotFoundException;

class JsonDataFeed implements DataFeedInterface
Expand All @@ -19,9 +20,18 @@ public function loadFile(string $file): void
}

$this->data = file_get_contents($file);
if ($this->data) {
$this->json = json_decode($this->data, true);

if (!$this->data) {
throw new JsonException("JSON file is empty.");
}

$json = json_decode($this->data, true);
if (!is_array($json)) {
throw new JsonException("Unable to decode JSON file.");
}

$this->json = $json;

}

public function load(string $data): void
Expand Down
8 changes: 8 additions & 0 deletions src/Exception/JsonException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Autodocs\Exception;

class JsonException extends \Exception
{

}
3 changes: 3 additions & 0 deletions tests/Resources/broken-json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"something":
}
Empty file added tests/Resources/empty-json.json
Empty file.
14 changes: 14 additions & 0 deletions tests/Unit/DataFeedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,17 @@

$this->assertNotEmpty($datafeed->json);
});

it('throws JSON exception with a broken JSON', function () {

$datafeed = new JsonDataFeed('test');
$datafeed->loadFile(__DIR__ . '/../Resources/broken-json.json');

})->throws(\Autodocs\Exception\JsonException::class);

it('throws JSON Exception with an empty JSON', function () {

$datafeed = new JsonDataFeed('test');
$datafeed->loadFile(__DIR__ . '/../Resources/empty-json.json');

})->throws(\Autodocs\Exception\JsonException::class);
Loading