Skip to content

Commit

Permalink
Merge pull request #1 from erikaheidi/improve-test-coverage
Browse files Browse the repository at this point in the history
adding more tests and updating dependencies
  • Loading branch information
erikaheidi authored Dec 20, 2023
2 parents 3883b2f + f818fdd commit 3193afb
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 302 deletions.
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);

0 comments on commit 3193afb

Please sign in to comment.