diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b33aae7..d9d5d9fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,10 +12,12 @@ For a full diff see [`dcd4cfb...main`][dcd4cfb...main]. * Added `Json` ([#2]), by [@localheinz] * Added `Schema` ([#3]), by [@localheinz] +* Added `Decoder` ([#5]), by [@localheinz] [dcd4cfb...main]: https://github.com/ergebnis/json-schema-validator/compare/dcd4cfb...main [#2]: https://github.com/ergebnis/json-schema-validator/pull/2 [#3]: https://github.com/ergebnis/json-schema-validator/pull/3 +[#5]: https://github.com/ergebnis/json-schema-validator/pull/5 [@localheinz]: https://github.com/localheinz diff --git a/infection.json b/infection.json index a50b766f..627741b8 100644 --- a/infection.json +++ b/infection.json @@ -3,8 +3,8 @@ "logs": { "text": ".build/infection/infection-log.txt" }, - "minCoveredMsi": 92, - "minMsi": 88, + "minCoveredMsi": 82, + "minMsi": 78, "phpUnit": { "configDir": "test\/Unit" }, diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 76c390b0..e4b5c627 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,11 +1,37 @@ + + + $decoded + $decoded + + + null|array|bool|float|int|\stdClass|string + null|array|bool|float|int|string + + + $decoded + $decoded + + $decoded $decoded + + + \Generator<string, array{0: string}> + \Generator<string, array{0: string}> + + + + + $expected + $expected + + $decoded diff --git a/src/Decoder.php b/src/Decoder.php new file mode 100644 index 00000000..a6e831e1 --- /dev/null +++ b/src/Decoder.php @@ -0,0 +1,55 @@ + + */ + public static function validString(): \Generator + { + yield from self::provideDataForValues(\array_map(static function (string $file): string { + $contents + = \file_get_contents($file); + + if (!\is_string($contents)) { + throw new \RuntimeException(\sprintf( + 'File "%s" does not exist or can not be read.', + $file + )); + } + + return $contents; + }, self::filesContainingValidJson())); + } + + /** + * @return \Generator + */ + public function validFile(): \Generator + { + yield from self::provideDataForValues(self::filesContainingValidJson()); + } + + /** + * @return array + */ + private static function filesContainingValidJson(): array + { + return [ + 'array' => __DIR__ . '/../Fixture/Json/valid/array.json', + 'bool-false' => __DIR__ . '/../Fixture/Json/valid/bool-false.json', + 'bool-true' => __DIR__ . '/../Fixture/Json/valid/bool-true.json', + 'float' => __DIR__ . '/../Fixture/Json/valid/float.json', + 'int' => __DIR__ . '/../Fixture/Json/valid/int.json', + 'object' => __DIR__ . '/../Fixture/Json/valid/object.json', + 'string-arbitrary' => __DIR__ . '/../Fixture/Json/valid/string-arbitrary.json', + 'string-empty' => __DIR__ . '/../Fixture/Json/valid/string-empty.json', + ]; + } +} diff --git a/test/Unit/DecoderTest.php b/test/Unit/DecoderTest.php new file mode 100644 index 00000000..9007eb3c --- /dev/null +++ b/test/Unit/DecoderTest.php @@ -0,0 +1,93 @@ +expectException(Exception\InvalidJson::class); + + $decoder->decodeAssociative($value); + } + + /** + * @dataProvider \Ergebnis\Json\SchemaValidator\Test\DataProvider\JsonProvider::validString() + */ + public function testDecodeAssociativeReturnsJsonDecodedValue(string $encoded): void + { + $decoder = new Decoder(); + + $decoded = $decoder->decodeAssociative($encoded); + + $expected = \json_decode( + $encoded, + true + ); + + self::assertSame($expected, $decoded); + } + + public function testDecodeNonAssociativeThrowsExceptionWhenValueCanNotBeDecoded(): void + { + $value = <<<'TXT' +{ + "foo: +TXT; + + $decoder = new Decoder(); + + $this->expectException(Exception\InvalidJson::class); + + $decoder->decodeAssociative($value); + } + + /** + * @dataProvider \Ergebnis\Json\SchemaValidator\Test\DataProvider\JsonProvider::validString() + */ + public function testDecodeNonAssociativeReturnsJsonDecodedValue(string $encoded): void + { + $decoder = new Decoder(); + + $decoded = $decoder->decodeNonAssociative($encoded); + + $expected = \json_decode( + $encoded, + false + ); + + self::assertEquals($expected, $decoded); + } +} diff --git a/test/Unit/JsonTest.php b/test/Unit/JsonTest.php index 43ae2e35..202275af 100644 --- a/test/Unit/JsonTest.php +++ b/test/Unit/JsonTest.php @@ -43,7 +43,7 @@ public function testFromStringThrowsWhenValueIsNotJson(): void } /** - * @dataProvider provideValidJsonString + * @dataProvider \Ergebnis\Json\SchemaValidator\Test\DataProvider\JsonProvider::validString() */ public function testFromStringReturnsJsonWhenValueIsValidJson(string $encoded): void { @@ -52,27 +52,6 @@ public function testFromStringReturnsJsonWhenValueIsValidJson(string $encoded): self::assertSame($encoded, $json->encoded()); } - /** - * @return \Generator - */ - public function provideValidJsonString(): \Generator - { - foreach (self::filesContainingValidJson() as $key => $value) { - $content = \file_get_contents($value); - - if (!\is_string($content)) { - throw new \RuntimeException(\sprintf( - 'File "%s" does not exist or can not be read.', - $value - )); - } - - yield $key => [ - $content, - ]; - } - } - public function testFromFileThrowsWhenFileDoesNotExist(): void { $file = __DIR__ . '/../Fixture/Json/does-not-exist.json'; @@ -101,7 +80,7 @@ public function testFromFileThrowsWhenFileDoesNotContainValidJson(): void } /** - * @dataProvider provideValidJsonFile + * @dataProvider \Ergebnis\Json\SchemaValidator\Test\DataProvider\JsonProvider::validFile() */ public function testFromFileReturnsJsonWhenFileContainsValidJson(string $file): void { @@ -109,33 +88,4 @@ public function testFromFileReturnsJsonWhenFileContainsValidJson(string $file): self::assertStringEqualsFile($file, $json->encoded()); } - - /** - * @return \Generator - */ - public function provideValidJsonFile(): \Generator - { - foreach (self::filesContainingValidJson() as $key => $value) { - yield $key => [ - $value, - ]; - } - } - - /** - * @return array - */ - private static function filesContainingValidJson(): array - { - return [ - 'array' => __DIR__ . '/../Fixture/Json/valid/array.json', - 'bool-false' => __DIR__ . '/../Fixture/Json/valid/bool-false.json', - 'bool-true' => __DIR__ . '/../Fixture/Json/valid/bool-true.json', - 'float' => __DIR__ . '/../Fixture/Json/valid/float.json', - 'int' => __DIR__ . '/../Fixture/Json/valid/int.json', - 'object' => __DIR__ . '/../Fixture/Json/valid/object.json', - 'string-arbitrary' => __DIR__ . '/../Fixture/Json/valid/string-arbitrary.json', - 'string-empty' => __DIR__ . '/../Fixture/Json/valid/string-empty.json', - ]; - } }