-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce results writer for json format
- Loading branch information
Showing
2 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
test/ComposerRequireCheckerTest/Cli/ResultsWriter/JsonFileTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |