Skip to content

Commit

Permalink
Utilize JsonFile report writer adding output option to check command
Browse files Browse the repository at this point in the history
  • Loading branch information
dkreuer committed Dec 7, 2021
1 parent ccb234a commit 7230567
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 74 deletions.
29 changes: 28 additions & 1 deletion src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace ComposerRequireChecker\Cli;

use ComposerRequireChecker\ASTLocator\LocateASTFromFiles;
use ComposerRequireChecker\Cli\ResultsWriter\CliJson;
use ComposerRequireChecker\Cli\ResultsWriter\CliText;
use ComposerRequireChecker\DefinedExtensionsResolver\DefinedExtensionsResolver;
use ComposerRequireChecker\DefinedSymbolsLocator\LocateDefinedSymbolsFromASTRoots;
Expand All @@ -19,6 +20,7 @@
use ComposerRequireChecker\GeneratorUtil\ComposeGenerators;
use ComposerRequireChecker\JsonLoader;
use ComposerRequireChecker\UsedSymbolsLocator\LocateUsedSymbolsFromASTRoots;
use DateTimeImmutable;
use InvalidArgumentException;
use LogicException;
use PhpParser\ErrorHandler\Collecting as CollectingErrorHandler;
Expand All @@ -37,6 +39,7 @@
use function array_merge;
use function count;
use function dirname;
use function file_put_contents;
use function gettype;
use function is_string;
use function realpath;
Expand Down Expand Up @@ -69,11 +72,21 @@ protected function configure(): void
InputOption::VALUE_NONE,
'this will cause ComposerRequireChecker to ignore errors when files cannot be parsed, otherwise'
. ' errors will be thrown'
)
->addOption(
'output',
null,
InputOption::VALUE_REQUIRED,
'generate output either as "text" or as "json", if specified, "quiet mode" is implied'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($input->getOption('output') !== null) {
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
}

if (! $output->isQuiet()) {
$application = $this->getApplication();
$output->writeln($application !== null ? $application->getLongVersion() : 'Unknown version');
Expand Down Expand Up @@ -154,7 +167,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 0;
}

$resultsWriter = new CliText($output);
switch ($input->getOption('output')) {
case 'json':
$application = $this->getApplication();
$resultsWriter = new CliJson(
static function (string $string): void {
file_put_contents('php://stdout', $string);
},
$application !== null ? $application->getVersion() : 'Unknown version',
static fn () => new DateTimeImmutable()
);
break;
case 'text':
default:
$resultsWriter = new CliText($output);
}

$output->writeln('The following ' . count($unknownSymbols) . ' unknown symbols were found:');
$guesser = new DependencyGuesser($options);
Expand Down
55 changes: 55 additions & 0 deletions src/ComposerRequireChecker/Cli/ResultsWriter/CliJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace ComposerRequireChecker\Cli\ResultsWriter;

use DateTimeImmutable;

use function json_encode;

use const JSON_THROW_ON_ERROR;

final class CliJson implements ResultsWriter
{
/** @var callable(string): void */
private $writeCallable;
private string $applicationVersion;
/** @var callable(): DateTimeImmutable */
private $nowCallable;

/**
* @param callable(string): void $write
* @param callable(): DateTimeImmutable $now
*/
public function __construct(callable $write, string $applicationVersion, callable $now)
{
$this->writeCallable = $write;
$this->applicationVersion = $applicationVersion;
$this->nowCallable = $now;
}

/**
* {@inheritdoc}
*/
public function write(array $unknownSymbols): void
{
$write = $this->writeCallable;
$now = $this->nowCallable;

$write(
json_encode(
[
'_meta' => [
'composer-require-checker' => [
'version' => $this->applicationVersion,
],
'date' => $now()->format(DateTimeImmutable::ATOM),
],
'unknown-symbols' => $unknownSymbols,
],
JSON_THROW_ON_ERROR
)
);
}
}
58 changes: 0 additions & 58 deletions src/ComposerRequireChecker/Cli/ResultsWriter/JsonFile.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
interface ResultsWriter
{
/**
* @param array<array-key, string[]> $unknownSymbols the unknown symbols found
* @param array<array-key, list<string>> $unknownSymbols the unknown symbols found
*/
public function write(array $unknownSymbols): void;
}
32 changes: 32 additions & 0 deletions test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Symfony\Component\Console\Tester\CommandTester;

use function dirname;
use function file_get_contents;
use function file_put_contents;
use function json_decode;
use function unlink;
use function version_compare;

Expand Down Expand Up @@ -75,6 +77,36 @@ public function testUnknownSymbolsFound(): void
$this->assertStringContainsString('libxml_clear_errors', $display);
}

public function testUnknownSymbolsFoundJsonReport(): void
{
$vfsRoot = vfsStream::setup();

$this->commandTester->execute([
'composer-json' => dirname(__DIR__, 2) . '/fixtures/unknownSymbols/composer.json',
'--report-json' => 'vfs://root/path/report.json',
]);

$this->assertSame(Command::FAILURE, $this->commandTester->getStatusCode());
$display = $this->commandTester->getDisplay();

$this->assertStringContainsString('Doctrine\Common\Collections\ArrayCollection', $display);

/** @var array{'unknown-symbols': array<array-key, string[]>} $actual */
$actual = json_decode(file_get_contents('vfs://root/path/report.json'), true);

$this->assertSame(
[
'Doctrine\Common\Collections\ArrayCollection' => [],
'Example\Library\Dependency' => [],
'FILTER_VALIDATE_URL' => ['ext-filter'],
'filter_var' => ['ext-filter'],
'Foo\Bar\Baz' => [],
'libxml_clear_errors' => ['ext-libxml'],
],
$actual['unknown-symbols']
);
}

public function testSelfCheckShowsNoErrors(): void
{
$this->commandTester->execute([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,30 @@

namespace ComposerRequireCheckerTest\Cli\ResultsWriter;

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

use function file_get_contents;
use function json_decode;

final class JsonFileTest extends TestCase
use const JSON_THROW_ON_ERROR;

final class CliJsonTest extends TestCase
{
private JsonFile $writer;
private vfsStreamDirectory $root;
private CliJson $writer;
private string $output = '';

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

$this->root = vfsStream::setup();
$this->writer = new JsonFile('vfs://root/path/name.json', '0.0.1');
$this->writer = new CliJson(
function (string $string): void {
$this->output .= $string;
},
'0.0.1',
static fn () => new DateTimeImmutable('@0')
);
}

public function testWriteReport(): void
Expand All @@ -33,16 +38,13 @@ public function testWriteReport(): void
'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';
$actual = json_decode($this->output, true, JSON_THROW_ON_ERROR);

self::assertSame(
[
'_meta' => [
'composer-require-checker' => ['version' => '0.0.1'],
'date' => 'ATOM_DATE',
'date' => '1970-01-01T00:00:00+00:00',
],
'unknown-symbols' => [
'Foo' => [],
Expand Down

0 comments on commit 7230567

Please sign in to comment.