Skip to content

Commit

Permalink
JsonFileTest should fail if file is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
sanmai committed Mar 23, 2019
1 parent b9322bf commit 0d91b4e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Json/JsonFile.php
Expand Up @@ -71,6 +71,10 @@ public function decode(): \stdClass

private function parse(): void
{
if (!is_file($this->path)) {
throw ParseException::invalidJson($this->path, 'file not found');
}

$data = json_decode((string) file_get_contents($this->path));

if (null === $data && JSON_ERROR_NONE !== json_last_error()) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Json/JsonFileTest.php
Expand Up @@ -101,6 +101,16 @@ public function test_it_throws_parse_exception_with_invalid_json(): void
(new JsonFile($jsonPath))->decode();
}

public function test_it_throws_parse_exception_when_file_is_not_found(): void
{
$jsonPath = $this->tmpDir . '/missing-invalid.json';
self::assertFileNotExists($jsonPath);

self::expectException(ParseException::class);

(new JsonFile($jsonPath))->decode();
}

public function test_it_throws_schema_validation_exception(): void
{
$jsonString = '{"timeout": 25}';
Expand Down

0 comments on commit 0d91b4e

Please sign in to comment.