diff --git a/src/Json/JsonFile.php b/src/Json/JsonFile.php index e98bb5f7c8..7939a3f073 100644 --- a/src/Json/JsonFile.php +++ b/src/Json/JsonFile.php @@ -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()) { diff --git a/tests/Json/JsonFileTest.php b/tests/Json/JsonFileTest.php index 6fdbb9b261..96bbab908f 100644 --- a/tests/Json/JsonFileTest.php +++ b/tests/Json/JsonFileTest.php @@ -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}';