Skip to content

Commit 3193afb

Browse files
authored
Merge pull request #1 from erikaheidi/improve-test-coverage
adding more tests and updating dependencies
2 parents 3883b2f + f818fdd commit 3193afb

File tree

7 files changed

+297
-302
lines changed

7 files changed

+297
-302
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
}
1111
},
1212
"require": {
13+
"php": "^8.2.0",
1314
"minicli/minicli": "^4.2",
1415
"minicli/stencil": "^0.2.1"
1516
},

composer.lock

+259-300
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DataFeed/JsonDataFeed.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Autodocs\DataFeed;
44

5+
use Autodocs\Exception\JsonException;
56
use Autodocs\Exception\NotFoundException;
67

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

2122
$this->data = file_get_contents($file);
22-
if ($this->data) {
23-
$this->json = json_decode($this->data, true);
23+
24+
if (!$this->data) {
25+
throw new JsonException("JSON file is empty.");
26+
}
27+
28+
$json = json_decode($this->data, true);
29+
if (!is_array($json)) {
30+
throw new JsonException("Unable to decode JSON file.");
2431
}
32+
33+
$this->json = $json;
34+
2535
}
2636

2737
public function load(string $data): void

src/Exception/JsonException.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Autodocs\Exception;
4+
5+
class JsonException extends \Exception
6+
{
7+
8+
}

tests/Resources/broken-json.json

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"something":
3+
}

tests/Resources/empty-json.json

Whitespace-only changes.

tests/Unit/DataFeedTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,17 @@
99

1010
$this->assertNotEmpty($datafeed->json);
1111
});
12+
13+
it('throws JSON exception with a broken JSON', function () {
14+
15+
$datafeed = new JsonDataFeed('test');
16+
$datafeed->loadFile(__DIR__ . '/../Resources/broken-json.json');
17+
18+
})->throws(\Autodocs\Exception\JsonException::class);
19+
20+
it('throws JSON Exception with an empty JSON', function () {
21+
22+
$datafeed = new JsonDataFeed('test');
23+
$datafeed->loadFile(__DIR__ . '/../Resources/empty-json.json');
24+
25+
})->throws(\Autodocs\Exception\JsonException::class);

0 commit comments

Comments
 (0)