Skip to content

Commit

Permalink
chore(qa): fix phpstan problems and cs fix
Browse files Browse the repository at this point in the history
  • Loading branch information
joelwurtz committed Apr 19, 2024
1 parent bdc1d6d commit ac6e415
Show file tree
Hide file tree
Showing 19 changed files with 167 additions and 101 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@
"amphp/http-client": "To use the web test case trait"
},
"require-dev": {
"amphp/http-client": "^5.0.1",
"friendsofphp/php-cs-fixer": "^3.53.0",
"phpstan/phpstan": "^1.10"
"phpstan/phpstan": "^1.10.67"
},
"conflict": {
"amphp/http-client": "<5.0.1",
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
parameters:
level: max
paths:
- src/
excludePaths:
- src/Assert/Assertion.php
1 change: 0 additions & 1 deletion src/Assert/Assertion.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Asynit\Assert;

use Asynit\Runner\TestStorage;
use Asynit\Test;
use bovigo\assert\Assertion as BaseAssertion;
use bovigo\assert\AssertionFailure;
use bovigo\assert\predicate\Predicate;
Expand Down
35 changes: 28 additions & 7 deletions src/Command/AsynitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class AsynitCommand extends Command
/**
* @internal
*/
final class AsynitCommand extends Command
{
private string $defaultBootstrapFilename = '';

Expand Down Expand Up @@ -45,22 +48,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
throw new \InvalidArgumentException("The bootstrap file '$bootstrapFilename' does not exist.");
}

/** @var string $target */
$target = $input->getArgument('target');

$testsFinder = new TestsFinder();
$testMethods = $testsFinder->findTests($input->getArgument('target'));
$testsSuites = $testsFinder->findTests($target);
$testsCount = array_reduce($testsSuites, fn (int $carry, $suite) => $carry + \count($suite->tests), 0);

/** @phpstan-ignore-next-line */
$useOrder = (boolean) $input->getOption('order');

list($chainOutput, $countOutput) = (new OutputFactory($useOrder))->buildOutput($testsCount);

list($chainOutput, $countOutput) = (new OutputFactory($input->getOption('order')))->buildOutput(\count($testMethods));
/** @phpstan-ignore-next-line */
$timeout = (float) $input->getOption('timeout');
/** @phpstan-ignore-next-line */
$retry = (int) $input->getOption('retry');
/** @phpstan-ignore-next-line */
$concurrency = (int) $input->getOption('concurrency');

if ($concurrency < 1) {
throw new \InvalidArgumentException('Concurrency must be greater than 0');
}

$defaultHttpConfiguration = new HttpClientConfiguration(
timeout: $input->getOption('timeout'),
retry: $input->getOption('retry'),
timeout: $timeout,
retry: $retry,
allowSelfSignedCertificate: $input->hasOption('allow-self-signed-certificate'),
);

$builder = new TestPoolBuilder();
$runner = new PoolRunner($defaultHttpConfiguration, new TestWorkflow($chainOutput), $input->getOption('concurrency'));
$runner = new PoolRunner($defaultHttpConfiguration, new TestWorkflow($chainOutput), $concurrency);

// Build a list of tests from the directory
$pool = $builder->build($testMethods);
$pool = $builder->build($testsSuites);
$runner->loop($pool);

// Return the number of failed tests
Expand Down
10 changes: 8 additions & 2 deletions src/HttpClient/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ApiResponse extends HttpResponse implements \ArrayAccess
/**
* @var array<string, mixed>|null
*/
private ?array $data = null;
private mixed $data = null;

public function __construct(private readonly Response $response)
{
Expand All @@ -24,7 +24,13 @@ public function __construct(private readonly Response $response)
private function ensureBodyIsRead(bool $associative = true): void
{
if (null === $this->data) {
$this->data = json_decode($this->response->getBody()->read(), $associative, flags: JSON_THROW_ON_ERROR);
$data = json_decode((string) $this->response->getBody(), $associative, flags: JSON_THROW_ON_ERROR);

if (!is_array($data)) {
throw new \InvalidArgumentException('The response body is not a valid JSON object.');
}

$this->data = $data;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/Output/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@
class Chain implements OutputInterface
{
/** @var OutputInterface[] */
private $outputs = [];
private array $outputs = [];

/**
* Add output to the chain.
*/
public function addOutput(OutputInterface $output)
public function addOutput(OutputInterface $output): void
{
$this->outputs[] = $output;
}

public function outputStep(Test $test, $debugOutput)
public function outputStep(Test $test, string $debugOutput): void
{
foreach ($this->outputs as $output) {
$output->outputStep($test, $debugOutput);
}
}

public function outputFailure(Test $test, $debugOutput, $failure)
public function outputFailure(Test $test, string $debugOutput, \Throwable $failure): void
{
foreach ($this->outputs as $output) {
$output->outputFailure($test, $debugOutput, $failure);
}
}

public function outputSuccess(Test $test, $debugOutput)
public function outputSuccess(Test $test, string $debugOutput): void
{
foreach ($this->outputs as $output) {
$output->outputSuccess($test, $debugOutput);
}
}

public function outputSkipped(Test $test, $debugOutput)
public function outputSkipped(Test $test, string $debugOutput): void
{
foreach ($this->outputs as $output) {
$output->outputSkipped($test, $debugOutput);
Expand Down
24 changes: 9 additions & 15 deletions src/Output/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,35 @@

class Count implements OutputInterface
{
private $succeed = 0;
private $failed = 0;
private $skipped = 0;
private int $succeed = 0;
private int $failed = 0;
private int $skipped = 0;

public function outputStep(Test $test, $debugOutput)
public function outputStep(Test $test, string $debugOutput): void
{
}

public function outputFailure(Test $test, $debugOutput, $failure)
public function outputFailure(Test $test, string $debugOutput, \Throwable $failure): void
{
++$this->failed;
}

public function outputSuccess(Test $test, $debugOutput)
public function outputSuccess(Test $test, string $debugOutput): void
{
++$this->succeed;
}

public function outputSkipped(Test $test, $debugOutput)
public function outputSkipped(Test $test, string $debugOutput): void
{
++$this->skipped;
}

/**
* @return int
*/
public function getSucceed()
public function getSucceed(): int
{
return $this->succeed;
}

/**
* @return int
*/
public function getFailed()
public function getFailed(): int
{
return $this->failed;
}
Expand Down
12 changes: 7 additions & 5 deletions src/Output/OutputFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@

/**
* Allow to detect current environment and choose the best output.
*
* @internal
*/
class OutputFactory
final class OutputFactory
{
private $order = false;

public function __construct(bool $order = false)
public function __construct(public readonly bool $order = false)
{
$this->order = $order;
}

/**
* @return array{Chain, Count}
*/
public function buildOutput(int $testCount): array
{
$countOutput = new Count();
Expand Down
14 changes: 6 additions & 8 deletions src/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@

/**
* Interface for displaying tests.
*
* @internal
*/
interface OutputInterface
{
public function outputStep(Test $test, $debugOutput);
public function outputStep(Test $test, string $debugOutput): void;

/**
* @param string $debugOutput
* @param \Throwable|\Exception $failure
*/
public function outputFailure(Test $test, $debugOutput, $failure);
public function outputFailure(Test $test, string $debugOutput, \Throwable $failure): void;

public function outputSuccess(Test $test, $debugOutput);
public function outputSuccess(Test $test, string $debugOutput): void;

public function outputSkipped(Test $test, $debugOutput);
public function outputSkipped(Test $test, string $debugOutput): void;
}
15 changes: 10 additions & 5 deletions src/Output/OutputOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
class OutputOrder implements OutputInterface
{
/** @var Test[] */
private $tests = [];
private array $tests = [];

public function outputStep(Test $test, $debugOutput)
public function outputStep(Test $test, string $debugOutput): void
{
}

public function outputFailure(Test $test, $debugOutput, $failure)
public function outputFailure(Test $test, string $debugOutput, \Throwable $failure): void
{
$this->tests[] = $test;
}

public function outputSuccess(Test $test, $debugOutput)
public function outputSuccess(Test $test, string $debugOutput): void
{
$this->tests[] = $test;
}

public function outputSkipped(Test $test, $debugOutput)
public function outputSkipped(Test $test, string $debugOutput): void
{
}

Expand All @@ -48,6 +48,11 @@ public function __destruct()
}
}

/**
* @param array<string, int> $orders
*
* @return int[]
*/
public function createDepends(Test $test, array $orders = []): array
{
$depends = [];
Expand Down
31 changes: 16 additions & 15 deletions src/Output/PhpUnitAlike.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ class PhpUnitAlike implements OutputInterface
public const SPLIT_AT = 60;
public const MAX_TRACE = 10;

private $outputFormatFail;
private $outputFormatSuccess;
private $outputFormatSkipped;
private $testOutputed;
private $failures;
private $assertionCount;
private $start;
private $testCount;
private OutputFormatterStyle $outputFormatFail;
private OutputFormatterStyle $outputFormatSuccess;
private OutputFormatterStyle $outputFormatSkipped;
private int $testOutputed;
/** @var array<array{test: Test, failure: \Throwable}> */
private array $failures;
private int $assertionCount;
private float $start;
private int $testCount;

public function __construct(int $testCount)
{
Expand All @@ -34,11 +35,11 @@ public function __construct(int $testCount)
$this->failures = [];
}

public function outputStep(Test $test, $debugOutput)
public function outputStep(Test $test, string $debugOutput): void
{
}

public function outputFailure(Test $test, $debugOutput, $failure)
public function outputFailure(Test $test, string $debugOutput, \Throwable $failure): void
{
$text = 'F';

Expand All @@ -57,23 +58,23 @@ public function outputFailure(Test $test, $debugOutput, $failure)
];
}

public function outputSuccess(Test $test, $debugOutput)
public function outputSuccess(Test $test, string $debugOutput): void
{
$this->writeTest($test, $this->outputFormatSuccess->apply('.'));
fwrite(STDOUT, $debugOutput);

$this->assertionCount += \count($test->getAssertions());
}

public function outputSkipped(Test $test, $debugOutput)
public function outputSkipped(Test $test, string $debugOutput): void
{
$this->writeTest($test, $this->outputFormatSkipped->apply('S'));
fwrite(STDOUT, $debugOutput);

$this->assertionCount += \count($test->getAssertions());
}

private function writeTest(Test $test, $text)
private function writeTest(Test $test, string $text): void
{
if (!$test->isRealTest) {
return;
Expand All @@ -89,7 +90,7 @@ private function writeTest(Test $test, $text)
++$this->testOutputed;
}

private function writeFailure($step, Test $test, $failure)
private function writeFailure(int $step, Test $test, \Throwable $failure): void
{
fwrite(STDOUT, $step + 1 .') '.$test->getDisplayName()." failed\n");

Expand Down Expand Up @@ -143,7 +144,7 @@ public function __destruct()
fwrite(STDOUT, $outputFormatSuccess->apply("OK, Tests: $this->testOutputed, Assertions: $this->assertionCount.\n"));
}

private function getDisplayableMemory($memory)
private function getDisplayableMemory(int $memory): string
{
$unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];

Expand Down

0 comments on commit ac6e415

Please sign in to comment.