diff --git a/src/ComposerRequireChecker/Cli/ResultsWriter/JsonFile.php b/src/ComposerRequireChecker/Cli/ResultsWriter/JsonFile.php new file mode 100644 index 00000000..4a6ce0ec --- /dev/null +++ b/src/ComposerRequireChecker/Cli/ResultsWriter/JsonFile.php @@ -0,0 +1,58 @@ +filePathname = $filePathname; + $this->applicationVersion = $applicationVersion; + } + + /** + * {@inheritdoc} + */ + public function write(array $unknownSymbols): void + { + $filePath = dirname($this->filePathname); + if (! mkdir($filePath, 0777, true) && ! is_dir($filePath)) { + throw new RuntimeException(sprintf('Directory "%s" was not created', $filePath)); + } + + file_put_contents( + $this->filePathname, + json_encode( + [ + '_meta' => [ + 'composer-require-checker' => [ + 'version' => $this->applicationVersion, + ], + 'date' => (new DateTimeImmutable())->format(DateTimeImmutable::ATOM), + ], + 'unknown-symbols' => $unknownSymbols, + ], + ) + ); + } +} diff --git a/test/ComposerRequireCheckerTest/Cli/ResultsWriter/JsonFileTest.php b/test/ComposerRequireCheckerTest/Cli/ResultsWriter/JsonFileTest.php new file mode 100644 index 00000000..6e66c40f --- /dev/null +++ b/test/ComposerRequireCheckerTest/Cli/ResultsWriter/JsonFileTest.php @@ -0,0 +1,56 @@ +root = vfsStream::setup(); + $this->writer = new JsonFile('vfs://root/path/name.json', '0.0.1'); + } + + public function testWriteReport(): void + { + $this->writer->write([ + 'Foo' => [], + 'opcache_get_status' => ['ext-opcache'], + 'dummy' => ['ext-dummy', 'ext-other'], + ]); + + self::assertFileExists('vfs://root/path/name.json'); + + $actual = json_decode(file_get_contents('vfs://root/path/name.json'), true); + $actual['_meta']['date'] = 'ATOM_DATE'; + + self::assertSame( + [ + '_meta' => [ + 'composer-require-checker' => ['version' => '0.0.1'], + 'date' => 'ATOM_DATE', + ], + 'unknown-symbols' => [ + 'Foo' => [], + 'opcache_get_status' => ['ext-opcache'], + 'dummy' => ['ext-dummy', 'ext-other'], + ], + ], + $actual + ); + } +}