Skip to content

Commit

Permalink
Introduce results writer for json format
Browse files Browse the repository at this point in the history
  • Loading branch information
dkreuer committed Dec 6, 2021
1 parent 05a6604 commit ccb234a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/ComposerRequireChecker/Cli/ResultsWriter/JsonFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace ComposerRequireChecker\Cli\ResultsWriter;

use DateTimeImmutable;
use RuntimeException;

use function dirname;
use function file_put_contents;
use function function_exists;
use function is_dir;
use function json_encode;
use function mkdir;
use function sprintf;

final class JsonFile implements ResultsWriter
{
private string $filePathname;
private string $applicationVersion;

public function __construct(string $filePathname, string $applicationVersion)
{
if (! function_exists('json_encode')) {
throw new RuntimeException('Missing extension: json');
}

$this->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,
],
)
);
}
}
56 changes: 56 additions & 0 deletions test/ComposerRequireCheckerTest/Cli/ResultsWriter/JsonFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace ComposerRequireCheckerTest\Cli\ResultsWriter;

use ComposerRequireChecker\Cli\ResultsWriter\JsonFile;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PHPUnit\Framework\TestCase;

use function file_get_contents;
use function json_decode;

final class JsonFileTest extends TestCase
{
private JsonFile $writer;
private vfsStreamDirectory $root;

protected function setUp(): void
{
parent::setUp();

$this->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
);
}
}

0 comments on commit ccb234a

Please sign in to comment.