Skip to content

Commit

Permalink
Fix code after upgrade to PHPStan v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed Dec 2, 2024
1 parent 32d9b2b commit 7c6ab49
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 46 deletions.
3 changes: 0 additions & 3 deletions src/Configuration/OutputPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ public function __construct(string $path)
$this->path = $path;
}

/**
* @throws \RuntimeException
*/
public function getPath(): string
{
return $this->path;
Expand Down
2 changes: 1 addition & 1 deletion src/Logs/ValueObject/LogData.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function __construct(
) {}

/**
* @psalm-assert array{status: string, test: string|array, message?: string|null} $log
* @psalm-assert array{status: string, test: string|array<mixed>, message?: string|null} $log
*/
private static function validateLogFormat(mixed $log): void
{
Expand Down
25 changes: 10 additions & 15 deletions src/Logs/ValueObject/TestMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,17 @@ private static function validate(mixed $data): void
throw self::invalidDeserializeInput($data);
}

self::assertPropertyIsString(self::CLASS_NAME, $data);
self::assertPropertyIsString(self::METHOD_NAME, $data);
self::assertPropertyIsString(self::FULL_NAME, $data);
}
$properties = [
self::CLASS_NAME,
self::METHOD_NAME,
self::FULL_NAME,
];

foreach ($properties as $property) {
if (is_string($data[$property] ?? null)) {
continue;
}

/**
* @template T of string
*
* @param T $property
* @param mixed[] $data
*
* @psalm-assert array{T: string, ...} $data
*/
private static function assertPropertyIsString(string $property, array $data): void
{
if (! is_string($data[$property] ?? false)) {
throw new \InvalidArgumentException($property . ' field missing or invalid');
}
}
Expand Down
1 change: 0 additions & 1 deletion tests/BaseIntegrationTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ protected function assertOutputOrder(UnformattedOutputStub $output, array $strin
$previousPosition = 0;
$previousString = '<beginning of output>';
foreach ($strings as $string) {
/** @var int $position */
$position = strpos($output->getOutput(), $string, $previousPosition);
$this->assertNotFalse($position, $output->getOutput() . PHP_EOL . 'String not found: ' . $string);
$this->assertGreaterThan(
Expand Down
1 change: 0 additions & 1 deletion tests/Functional/Command/CoverageCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public function testExecutionWithTextSummaryToConsole(): void

private function getTempCoverageFilename(): string
{
/** @var string $filename */
$filename = tempnam(sys_get_temp_dir(), 'coverage.txt');
$this->assertNotFalse($filename);

Expand Down
2 changes: 0 additions & 2 deletions tests/Functional/Printer/FilesRecapPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public function testOnEngineEndPrintsInTheRightOrder(): void
$this->populateTestResultContainerWithAllPossibleStatuses();

$printer = $this->getService(FilesRecapPrinter::class);
$this->assertInstanceOf(FilesRecapPrinter::class, $printer);

$printer->onEngineEnd();

Expand Down Expand Up @@ -47,7 +46,6 @@ public function testRegressionDuplicateFilesDueToMethodNames(): void

$output = $this->getConsoleOutput();
$runner = $this->getService(Runner::class);
$this->assertInstanceOf(Runner::class, $runner);
$this->assertNotEquals(0, $runner->run());

$this->assertOutputOrder($output, [
Expand Down
14 changes: 9 additions & 5 deletions tests/Unit/Configuration/CoverageConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tests\BaseUnitTestCase;

class CoverageConfigurationTest extends BaseUnitTestCase
Expand Down Expand Up @@ -110,9 +109,7 @@ public function testBuildContainerWithDebug(): void

$container = $paraunit->buildContainer($input->reveal(), $output->reveal());

$service = $this->getService($container, DebugPrinter::class); // test instantiation, to prevent misconfiguration
$this->assertInstanceOf(DebugPrinter::class, $service);
$this->assertInstanceOf(EventSubscriberInterface::class, $service);
$this->getService($container, DebugPrinter::class); // test instantiation, to prevent misconfiguration
}

#[DataProvider('cliOptionsProvider')]
Expand Down Expand Up @@ -257,10 +254,17 @@ public function testBuildContainerWithColoredTextToConsoleCoverage(): void
$this->assertTrue($property->getValue($processor));
}

/**
* @template T of object
*
* @param class-string<T> $serviceName
*
* @return T
*/
private function getService(ContainerBuilder $container, string $serviceName): object
{
$service = $container->get(sprintf(CoverageConfiguration::PUBLIC_ALIAS_FORMAT, $serviceName));
$this->assertNotNull($service);
$this->assertInstanceOf($serviceName, $service);

return $service;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Configuration/ParallelConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public function testBuildContainerWithDebug(): void

// test instantiation, to prevent misconfiguration
$service = $this->getService($container, DebugPrinter::class);
$this->assertInstanceOf(DebugPrinter::class, $service);
$this->assertInstanceOf(EventSubscriberInterface::class, $service);
$this->assertInstanceOf(DebugPrinter::class, $service);
}

public function testBuildContainerWithSortRandom(): void
Expand Down
3 changes: 0 additions & 3 deletions tests/Unit/Logs/LogFetcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public function testFetchReturnsEmptyListWithMissingLog(): void

$logs = $fetcher->fetch($process);

$this->assertNotNull($logs, 'Fetcher returning a non-array');
$this->assertEmpty($logs);
}

Expand All @@ -42,12 +41,10 @@ public function testFetch(): void

$logs = $fetcher->fetch($process);

$this->assertNotNull($logs, 'Fetcher returning a non-array');
$this->assertCount(4 + 1, $logs, 'Log ending missing');
$this->assertContainsOnlyInstancesOf(LogData::class, $logs);

$endingLog = end($logs);
$this->assertInstanceOf(LogData::class, $endingLog);
$this->assertEquals(LogStatus::LogTerminated, $endingLog->status);

$this->assertFileDoesNotExist($filename, 'Log file should be deleted to preserve memory');
Expand Down
4 changes: 0 additions & 4 deletions tests/Unit/Logs/ValueObject/LogDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function testLogEnding(): void
$logStubEntry = $parsedResult[0];
$this->assertEquals($logData, $logStubEntry);
$logEndingEntry = $parsedResult[1];
$this->assertInstanceOf(LogData::class, $logEndingEntry);
$this->assertEquals($logData->test, $logEndingEntry->test);
$this->assertEquals(LogStatus::LogTerminated, $logEndingEntry->status);
$this->assertNull($logEndingEntry->message);
Expand All @@ -52,7 +51,6 @@ public function testSerializationWithSimpleTest(): void
$parsedResult = LogData::parse(json_encode($logData, JSON_THROW_ON_ERROR));

$this->assertCount(2, $parsedResult);
$this->assertInstanceOf(LogData::class, $parsedResult[0]);
$this->assertEquals($logData, $parsedResult[0]);
$this->assertSame('Foo', $parsedResult[0]->test->name);
}
Expand All @@ -64,7 +62,6 @@ public function testSerializationWithTestMethod(): void
$parsedResult = LogData::parse(json_encode($logData, JSON_THROW_ON_ERROR));

$this->assertCount(2, $parsedResult);
$this->assertInstanceOf(LogData::class, $parsedResult[0]);
$this->assertEquals($logData, $parsedResult[0]);
$this->assertObjectHasProperty('className', $parsedResult[0]->test);
$this->assertObjectHasProperty('methodName', $parsedResult[0]->test);
Expand All @@ -78,7 +75,6 @@ public function testSerializationError(): void
$parsedResult = LogData::parse('{}');

$this->assertCount(2, $parsedResult);
$this->assertInstanceOf(LogData::class, $parsedResult[0]);
$this->assertEquals(LogStatus::Unknown, $parsedResult[0]->status);
$this->assertEquals(Test::unknown(), $parsedResult[0]->test);
$this->assertIsString($parsedResult[0]->message);
Expand Down
6 changes: 0 additions & 6 deletions tests/Unit/Printer/DebugPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Paraunit\Lifecycle\ProcessTerminated;
use Paraunit\Lifecycle\ProcessToBeRetried;
use Paraunit\Printer\DebugPrinter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Tests\BaseUnitTestCase;
use Tests\Stub\StubbedParaunitProcess;
use Tests\Stub\UnformattedOutputStub;
Expand All @@ -18,11 +17,6 @@ class DebugPrinterTest extends BaseUnitTestCase
{
public function testIsSubscribedToAllProcessEvents(): void
{
$this->assertTrue(
is_subclass_of(DebugPrinter::class, EventSubscriberInterface::class),
DebugPrinter::class . ' is not an EventSubscriber!'
);

$subscribedEvents = array_keys(DebugPrinter::getSubscribedEvents());
$processEvents = [
ProcessParsingCompleted::class,
Expand Down
4 changes: 0 additions & 4 deletions tests/Unit/Process/ProcessFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Paraunit\Coverage\CoverageDriver;
use Paraunit\Process\CommandLine;
use Paraunit\Process\CommandLineWithCoverage;
use Paraunit\Process\Process;
use Paraunit\Process\ProcessFactory;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\BaseUnitTestCase;
Expand Down Expand Up @@ -51,14 +50,12 @@ public function testCreateProcess(): void

$processWrapper = $factory->create('TestTest.php');

$this->assertInstanceOf(Process::class, $processWrapper);
$commandLine = $processWrapper->getCommandLine();
$this->assertStringContainsString('TestTest.php', $commandLine);
$this->assertStringContainsString('--specific=value-for-TestTest.php', $commandLine);

$processWrapper = $factory->create('TestTest2.php');

$this->assertInstanceOf(Process::class, $processWrapper);
$commandLine = $processWrapper->getCommandLine();
$this->assertStringContainsString('TestTest2.php', $commandLine);
$this->assertStringContainsString('--specific=value-for-TestTest2.php', $commandLine);
Expand Down Expand Up @@ -137,7 +134,6 @@ public function testCreateProcessChunked(): void

$processWrapper = $factory->create('phpunit.xml');

$this->assertInstanceOf(Process::class, $processWrapper);
$commandLine = $processWrapper->getCommandLine();
$this->assertStringContainsString('--configuration=phpunit.xml', $commandLine);
$this->assertStringContainsString('--specific=value-for-phpunit.xml', $commandLine);
Expand Down

0 comments on commit 7c6ab49

Please sign in to comment.