Skip to content

Commit

Permalink
feat: Upgrade to PHPUnit10 (#45)
Browse files Browse the repository at this point in the history
Note that this is a feature rather than a chore task since this library
provides custom assertions.
  • Loading branch information
theofidry committed Oct 6, 2023
1 parent 58d5618 commit fb346e2
Show file tree
Hide file tree
Showing 21 changed files with 265 additions and 260 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
"bamarni/composer-bin-plugin": "^1.4",
"ergebnis/composer-normalize": "^2.28",
"infection/infection": "^0.26",
"phpspec/prophecy-phpunit": "^2.0",
"phpunit/phpunit": "^9.4.3",
"symfony/phpunit-bridge": "^6.2",
"webmozarts/strict-phpunit": "^7.3"
"phpunit/phpunit": "^10.3"
},
"minimum-stability": "dev",
"prefer-stable": true,
Expand Down
5 changes: 5 additions & 0 deletions infection.json5.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
]
},
"PublicVisibility": false,
"UnwrapArrayValues": {
"ignore": [
"Fidry\\Makefile\\Parser::parseLine"
]
},
"UnwrapFinally": {
"ignore": [
"Fidry\\Makefile\\Test\\BaseMakefileTestCase::executeInDirectory"
Expand Down
12 changes: 2 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,19 @@
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0"/>
</php>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>

<extensions>
<extension class="Webmozarts\StrictPHPUnit\StrictPHPUnitExtension"/>
</extensions>

<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage>
<source>
<include>
<directory>src</directory>
</include>

<exclude>
<file>src/Test/Constraint/BaseConstraint.php</file>
</exclude>
</coverage>
</source>
</phpunit>
15 changes: 8 additions & 7 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
strictBinaryOperands="true"
ignoreInternalFunctionFalseReturn="false"
ignoreInternalFunctionNullReturn="false"
findUnusedVariablesAndParams="true"
findUnusedBaselineEntry="false"
findUnusedCode="true"
findUnusedPsalmSuppress="true"
findUnusedVariablesAndParams="true"
ensureArrayStringOffsetsExist="true"
ensureArrayIntOffsetsExist="true"
phpVersion="8.1">
Expand All @@ -27,12 +28,6 @@
</projectFiles>

<issueHandlers>
<DeprecatedFunction>
<errorLevel type="suppress">
<referencedFunction name="Safe\sprintf"/>
</errorLevel>
</DeprecatedFunction>

<ForbiddenCode>
<errorLevel type="suppress">
<file name="src/Test/BaseMakefileTestCase.php"/>
Expand All @@ -46,6 +41,12 @@
<file name="src/Test/Constraint/BaseConstraint.php"/>
</errorLevel>
</MissingConstructor>

<PossiblyUnusedMethod>
<errorLevel type="suppress">
<directory name="tests"/>
</errorLevel>
</PossiblyUnusedMethod>

<UnusedConstructor errorLevel="suppress"/>
</issueHandlers>
Expand Down
3 changes: 3 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

use Rector\Config\RectorConfig;
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
Expand All @@ -46,6 +47,8 @@

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,

PHPUnitSetList::PHPUNIT_100,
]);

$rectorConfig->skip([
Expand Down
3 changes: 2 additions & 1 deletion src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static function (array $parsedRules, string $line) use (&$multiline, &$ignoreNex
}

/**
* @param list<Rule> $parsedRules
* @param Rule[] $parsedRules
*
* @return list<Rule>
*/
Expand All @@ -86,6 +86,7 @@ private static function parseLine(
bool &$multiline,
bool &$ignoreNextLinesOfMultiline
): array {
$parsedRules = array_values($parsedRules);
$line = rtrim($line);

if (!self::isRule($line)) {
Expand Down
6 changes: 3 additions & 3 deletions src/Test/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static function assertSinglePrerequisitePhony(Rule $rule, string $message
* declares a single target, is optionally followed by a comment line and
* then a rule to declare the command itself.
*
* @param list<Rule> $rules
* @param Rule[] $rules
*/
public static function assertHasValidPhonyTargetDeclarations(
array $rules,
Expand All @@ -74,9 +74,9 @@ public static function assertHasValidPhonyTargetDeclarations(
}

/**
* Asserts that the a rule target is not declared twice.
* Asserts that a rule target is not declared twice.
*
* @param list<Rule> $rules
* @param Rule[] $rules
*/
public static function assertNoDuplicateTarget(
array $rules,
Expand Down
11 changes: 3 additions & 8 deletions src/Test/Constraint/BaseConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ abstract class BaseConstraint extends Constraint
{
protected string $failureDescription;

/**
* @param mixed $other
*/
final public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
final public function evaluate(mixed $other, string $description = '', bool $returnResult = false): ?bool
{
unset($this->failureDescription);
$success = false;
Expand All @@ -72,16 +69,14 @@ final public function evaluate($other, string $description = '', bool $returnRes
return null;
}

final protected function failureDescription($other): string
final protected function failureDescription(mixed $other): string
{
/** @psalm-suppress RedundantPropertyInitializationCheck */
return $this->failureDescription ?? parent::failureDescription($other);
}

/**
* @param mixed $other
*
* @throws MatchingFailure
*/
abstract public function checkOther($other, string $description): void;
abstract public function checkOther(mixed $other, string $description): void;
}
3 changes: 2 additions & 1 deletion src/Test/Constraint/NoDuplicateTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ public function toString(): string
return 'is not declared twice';
}

public function checkOther($other, string $description): void
public function checkOther(mixed $other, string $description): void
{
self::checkIsArrayOfRules($other);
self::checkDuplicates($other);
}

/**
* @psalm-assert Rule[] $other
*
* @throws MatchingFailure
*/
private static function checkIsArrayOfRules(mixed $other): void
Expand Down
3 changes: 2 additions & 1 deletion src/Test/Constraint/SinglePrerequisitePhony.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function toString(): string
return 'is a .PHONY rule with one and only one pre-requisite';
}

public function checkOther($other, string $description): void
public function checkOther(mixed $other, string $description): void
{
self::checkIsRuleInstance($other);
self::checkIsPhonyRule($other);
Expand All @@ -57,6 +57,7 @@ public function checkOther($other, string $description): void

/**
* @psalm-assert Rule $other
*
* @throws MatchingFailure
*/
private static function checkIsRuleInstance(mixed $other): void
Expand Down
2 changes: 1 addition & 1 deletion src/Test/Constraint/ValidCommandDeclaration.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function toString(): string
return 'is a Makefile "command" declaration';
}

public function checkOther($other, string $description): void
public function checkOther(mixed $other, string $description): void
{
self::checkIsArrayOfRules($other);

Expand Down
4 changes: 2 additions & 2 deletions tests/MakefileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@
namespace Fidry\Makefile\Tests;

use Fidry\Makefile\Test\BaseMakefileTestCase;
use PHPUnit\Framework\Attributes\CoversNothing;

/**
* @coversNothing
*
* @internal
*/
#[CoversNothing]
final class MakefileTest extends BaseMakefileTestCase
{
protected static function getMakefilePath(): string
Expand Down
8 changes: 4 additions & 4 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@

use Fidry\Makefile\Parser;
use Fidry\Makefile\Rule;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* @covers \Fidry\Makefile\Parser
* @internal
*/
#[CoversClass(Parser::class)]
final class ParserTest extends TestCase
{
/**
* @dataProvider makefileContentProvider
*/
#[DataProvider('makefileContentProvider')]
public function test_it_can_parse_makefiles(string $content, array $expected): void
{
$actual = Parser::parse($content);
Expand Down
25 changes: 8 additions & 17 deletions tests/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@
namespace Fidry\Makefile\Tests;

use Fidry\Makefile\Rule;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

/**
* @covers \Fidry\Makefile\Rule
*
* @internal
*/
#[CoversClass(Rule::class)]
final class RuleTest extends TestCase
{
public function test_it_can_be_instantiated(): void
Expand Down Expand Up @@ -81,9 +82,7 @@ public function test_it_can_merge_prerequisites(): void
);
}

/**
* @dataProvider phonyTargetProvider
*/
#[DataProvider('phonyTargetProvider')]
public function test_it_can_detect_if_a_target_is_phony(Rule $rule, bool $expected): void
{
self::assertSame($expected, $rule->isPhony());
Expand Down Expand Up @@ -117,9 +116,7 @@ public static function phonyTargetProvider(): iterable
];
}

/**
* @dataProvider makefileCommentProvider
*/
#[DataProvider('makefileCommentProvider')]
public function test_it_can_detect_the_rule_is_a_target_with_a_makefile_comment(
Rule $rule,
bool $expected
Expand Down Expand Up @@ -165,9 +162,7 @@ public static function makefileCommentProvider(): iterable
];
}

/**
* @dataProvider commandCommentProvider
*/
#[DataProvider('commandCommentProvider')]
public function test_it_can_detect_the_rule_is_a_target_with_a_command_comment(
Rule $rule,
bool $expected
Expand Down Expand Up @@ -213,9 +208,7 @@ public static function commandCommentProvider(): iterable
];
}

/**
* @dataProvider regularRuleProvider
*/
#[DataProvider('regularRuleProvider')]
public function test_it_can_detect_the_rule_is_a_regular_rule(
Rule $rule,
bool $expected
Expand Down Expand Up @@ -268,9 +261,7 @@ public function test_it_can_create_a_phony_rule(): void
self::assertTrue($rule->isPhony());
}

/**
* @dataProvider ruleToStringProvider
*/
#[DataProvider('ruleToStringProvider')]
public function test_it_can_be_casted_into_a_string(
Rule $rule,
string $expected
Expand Down
19 changes: 8 additions & 11 deletions tests/Test/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,19 @@
use Fidry\Makefile\Tests\Test\Constraint\SinglePrerequisitePhonyTest;
use Fidry\Makefile\Tests\Test\Constraint\ValidCommandDeclarationTest;
use Fidry\Makefile\Tests\ThrowableToStringMapper;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use function is_array;

/**
* @covers \Fidry\Makefile\Test\Assert
*
* @internal
*/
#[CoversClass(Assert::class)]
final class AssertTest extends TestCase
{
/**
* @dataProvider singlePrerequisitePhonyProvider
*/
#[DataProvider('singlePrerequisitePhonyProvider')]
public function test_it_tests_that_a_rule_is_phony_and_has_one_and_only_one_pre_requisite(
Rule $rule,
?string $expectedAssertionFailureMessage
Expand Down Expand Up @@ -88,12 +87,11 @@ public static function singlePrerequisitePhonyProvider(): iterable
}

/**
* @dataProvider phonyDeclarationsProvider
*
* @param Rule[] $rules
*/
#[DataProvider('phonyDeclarationsProvider')]
public function test_it_tests_that_the_phony_declarations_declare_a_command(
$rules,
array $rules,
?string $expectedAssertionFailureMessage
): void {
$assert = static function () use ($rules): void { Assert::assertHasValidPhonyTargetDeclarations($rules); };
Expand Down Expand Up @@ -130,12 +128,11 @@ public static function phonyDeclarationsProvider(): iterable
}

/**
* @dataProvider duplicateTargetProvider
*
* @param Rule[] $rules
*/
#[DataProvider('duplicateTargetProvider')]
public function test_it_tests_that_no_target_is_declared_twice(
$rules,
array $rules,
?string $expectedAssertionFailureMessage
): void {
$assert = static function () use ($rules): void { Assert::assertNoDuplicateTarget($rules); };
Expand Down
Loading

0 comments on commit fb346e2

Please sign in to comment.