Skip to content

Commit

Permalink
Squash mutants
Browse files Browse the repository at this point in the history
  • Loading branch information
dkreuer committed Dec 9, 2021
1 parent 91ba74e commit 2c6fe9c
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 24 deletions.
33 changes: 33 additions & 0 deletions src/ComposerRequireChecker/Cli/ApplicationHeaderWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace ComposerRequireChecker\Cli;

use Symfony\Component\Console\Application as AbstractApplication;
use Symfony\Component\Console\Output\OutputInterface;

final class ApplicationHeaderWriter
{
private ?AbstractApplication $application;

public function __construct(?AbstractApplication $application = null)
{
$this->application = $application;
}

public function __invoke(OutputInterface $output): void
{
if ($output->isQuiet()) {
return;
}

if ($this->application === null) {
$output->writeln('Unknown version');

return;
}

$output->writeln($this->application->getLongVersion());
}
}
29 changes: 19 additions & 10 deletions src/ComposerRequireChecker/Cli/CheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
}

if (! $output->isQuiet()) {
$application = $this->getApplication();
$output->writeln($application !== null ? $application->getLongVersion() : 'Unknown version');
}
(new ApplicationHeaderWriter($this->getApplication()))->__invoke($output);

$composerJsonArgument = $input->getArgument('composer-json');

Expand Down Expand Up @@ -180,23 +177,35 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$options->getSymbolWhitelist()
);

// pcov which is used for coverage does not detect executed code in anonymous functions used as callable
// therefore we require to have closure class.
$outputWrapper = new class ($output) {
private OutputInterface $output;

public function __construct(OutputInterface $output)
{
$this->output = $output;
}

public function __invoke(string $string): void
{
$this->output->write($string, false, OutputInterface::VERBOSITY_QUIET);
}
};

switch ($input->getOption('output')) {
case 'json':
$application = $this->getApplication();
$resultsWriter = new CliJson(
static function (string $string) use ($output): void {
$output->write($string, false, OutputInterface::VERBOSITY_QUIET | OutputInterface::OUTPUT_RAW);
},
$outputWrapper,
$application !== null ? $application->getVersion() : 'Unknown version',
static fn () => new DateTimeImmutable()
);
break;
case 'text':
$resultsWriter = new CliText(
$output,
static function (string $string) use ($output): void {
$output->write($string, false, OutputInterface::VERBOSITY_QUIET | OutputInterface::OUTPUT_RAW);
}
$outputWrapper
);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class LocateDefinedSymbolsFromASTRoots
/**
* @param Traversable<int, array<Node>> $ASTs a series of AST roots
*
* @return string[] all the found symbols
* @return list<string> all the found symbols
*/
public function __invoke(Traversable $ASTs): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __invoke(array $composerData, string $packageDir): Generator
/**
* @param array<string> $sourceDirs
*
* @return array<string>
* @return list<string>
*/
private function getFilePaths(array $sourceDirs, string $packageDir): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/ComposerRequireChecker/JsonLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Throwable;
use Webmozart\Assert\Assert;

use function assert;
use function file_get_contents;
use function is_string;
use function json_decode;

use const JSON_THROW_ON_ERROR;
Expand Down Expand Up @@ -56,9 +58,7 @@ private static function getFileContentFromPath(string $path): string

$content = file_get_contents($path);

if ($content === false) {
throw new NotReadable('unable to read ' . $path);
}
assert(is_string($content));

return $content;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function beforeTraverse(array $nodes)
}

/**
* @return array<string>
* @return list<string>
*/
public function getDefinedSymbols(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct()
}

/**
* @return array<string>
* @return list<string>
*/
public function getCollectedSymbols(): array
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace ComposerRequireCheckerTest\Cli;

use ComposerRequireChecker\Cli\ApplicationHeaderWriter;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\BufferedOutput;

/**
* @covers \ComposerRequireChecker\Cli\ApplicationHeaderWriter
*/
final class ApplicationHeaderWriterTest extends TestCase
{
public function testWithoutApplication(): void
{
$output = new BufferedOutput();

(new ApplicationHeaderWriter())->__invoke($output);

self::assertStringContainsString('Unknown version', $output->fetch());
}

public function testWithApplication(): void
{
$output = new BufferedOutput();

$application = new Application('APPNAME', 'APPVERSION');

(new ApplicationHeaderWriter($application))->__invoke($output);

self::assertStringContainsString('APPNAME APPVERSION', $output->fetch());
}
}
3 changes: 3 additions & 0 deletions test/ComposerRequireCheckerTest/Cli/CheckCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use function version_compare;

use const JSON_THROW_ON_ERROR;
use const PHP_EOL;
use const PHP_VERSION;

final class CheckCommandTest extends TestCase
Expand Down Expand Up @@ -112,6 +113,7 @@ public function testUnknownSymbolsFoundJsonReport(): void
],
$actual['unknown-symbols']
);
self::assertStringEndsNotWith(PHP_EOL, $display);
}

public function testUnknownSymbolsFoundTextReport(): void
Expand All @@ -131,6 +133,7 @@ public function testUnknownSymbolsFoundTextReport(): void
$this->assertStringContainsString('filter_var', $display);
$this->assertStringContainsString('Foo\Bar\Baz', $display);
$this->assertStringContainsString('libxml_clear_errors', $display);
$this->assertStringEndsNotWith(PHP_EOL . PHP_EOL, $display);
}

public function testSelfCheckShowsNoErrors(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Catch_;
Expand Down Expand Up @@ -169,18 +170,21 @@ public function testFunctionCallUsage(): void

public function testFunctionParameterType(): void
{
$functionName = new Name('foo');
$node = new Function_($functionName);
$node->name = $functionName;
$param = new Param(new Variable('bar'));
$param->type = new Name('Baz');
$node->params = [$param];
$functionName = new Name('foo');
$node = new Function_($functionName);
$node->name = $functionName;
$param = new Param(new Variable('bar'));
$param->type = new Name('Baz');
$anotherParam = new Param(new Variable('quux'));
$anotherParam->type = new Identifier('foo');
$node->params = [$param, $anotherParam];

$this->visitor->enterNode($node);

$symbols = $this->visitor->getCollectedSymbols();
$this->assertCount(1, $symbols);
$this->assertCount(2, $symbols);
$this->assertContains('Baz', $symbols);
$this->assertContains('foo', $symbols);
}

public function testMethodParameterType(): void
Expand Down

0 comments on commit 2c6fe9c

Please sign in to comment.