-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support PHPUnit v9.1 naming for some asserts #7997
base: master
Are you sure you want to change the base?
Changes from all commits
79316a6
2c8ca76
0193955
f04c854
ae5c56f
e9c4087
9850e41
f84f505
c5a6d59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
====================================== | ||
Rule set ``@PHPUnit91Migration:risky`` | ||
====================================== | ||
|
||
Rules to improve tests code for PHPUnit 9.1 compatibility. | ||
|
||
Warning | ||
------- | ||
|
||
This set contains rules that are risky | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Using this rule set may lead to changes in your code's logic and behaviour. Use it with caution and review changes before incorporating them into your code base. | ||
|
||
Rules | ||
----- | ||
|
||
- `@PHPUnit84Migration:risky <./PHPUnit84MigrationRisky.rst>`_ | ||
- `php_unit_dedicate_assert <./../rules/php_unit/php_unit_dedicate_assert.rst>`_ with config: | ||
|
||
``['target' => '9.1']`` | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,77 @@ | |
final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements ConfigurableFixerInterface | ||
{ | ||
/** | ||
* @var array<string, array{positive: string, negative: false|string, argument_count?: int, swap_arguments?: true}|true> | ||
* @var array{ | ||
* array_key_exists: array{ | ||
* positive: string, | ||
* negative: string, | ||
* argument_count: int | ||
* }, | ||
* empty: array{ | ||
* positive: string, | ||
* negative: string | ||
* }, | ||
* file_exists: array{ | ||
* positive: string, | ||
* negative: string | ||
* }, | ||
* is_array: bool, | ||
* is_bool: bool, | ||
* is_callable: bool, | ||
* is_dir: array{ | ||
* positive: string, | ||
* negative: string | ||
* }, | ||
* is_double: bool, | ||
* is_float: bool, | ||
* is_infinite: array{ | ||
* positive: string, | ||
* negative: string | ||
* }, | ||
* is_int: bool, | ||
* is_integer: bool, | ||
* is_long: bool, | ||
* is_nan: array{ | ||
* positive: string, | ||
* negative: bool | ||
* }, | ||
* is_null: array{ | ||
* positive: string, | ||
* negative: string | ||
* }, | ||
* is_numeric: bool, | ||
* is_object: bool, | ||
* is_readable: array{ | ||
* positive: string, | ||
* negative: string | ||
* }, | ||
* is_real: bool, | ||
* is_resource: bool, | ||
* is_scalar: bool, | ||
* is_string: bool, | ||
* is_writable: array{ | ||
* positive: string, | ||
* negative: string | ||
* }, | ||
* str_contains: array{ | ||
* positive: string, | ||
* negative: string, | ||
* argument_count: int, | ||
* swap_arguments: bool | ||
* }, | ||
* str_ends_with: array{ | ||
* positive: string, | ||
* negative: string, | ||
* argument_count: int, | ||
* swap_arguments: bool | ||
* }, | ||
* str_starts_with: array{ | ||
* positive: string, | ||
* negative: string, | ||
* argument_count: int, | ||
* swap_arguments: bool | ||
* } | ||
* } | ||
*/ | ||
private static array $fixMap = [ | ||
'array_key_exists' => [ | ||
|
@@ -106,6 +176,7 @@ | |
'argument_count' => 2, | ||
'swap_arguments' => true, | ||
], | ||
'assertNotIsReadable' => true, | ||
]; | ||
|
||
/** | ||
|
@@ -169,6 +240,15 @@ | |
'str_contains', | ||
]); | ||
} | ||
|
||
if (PhpUnitTargetVersion::fulfills($this->configuration['target'], PhpUnitTargetVersion::VERSION_9_1)) { | ||
$this->functions = array_merge($this->functions, [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. those are not native, global functions but methods of PHPUnit. please don't mix content of |
||
'assertdirectorynotexists', | ||
'assertnotisreadable', | ||
'assertnotiswritable', | ||
'assertfilenotexists', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. more are missing, eg |
||
]); | ||
} | ||
} | ||
|
||
public function isRisky(): bool | ||
|
@@ -230,23 +310,24 @@ | |
|
||
foreach ($this->getPreviousAssertCall($tokens, $startIndex, $endIndex) as $assertCall) { | ||
// test and fix for assertTrue/False to dedicated asserts | ||
if ('asserttrue' === $assertCall['loweredName'] || 'assertfalse' === $assertCall['loweredName']) { | ||
if (\in_array($assertCall['loweredName'], ['asserttrue', 'assertfalse'], true)) { | ||
$this->fixAssertTrueFalse($tokens, $argumentsAnalyzer, $assertCall); | ||
|
||
continue; | ||
} | ||
|
||
if ( | ||
'assertsame' === $assertCall['loweredName'] | ||
|| 'assertnotsame' === $assertCall['loweredName'] | ||
|| 'assertequals' === $assertCall['loweredName'] | ||
|| 'assertnotequals' === $assertCall['loweredName'] | ||
) { | ||
if (\in_array( | ||
$assertCall['loweredName'], | ||
['assertsame', 'assertnotsame', 'assertequals', 'assertnotequals'], | ||
true | ||
)) { | ||
$this->fixAssertSameEquals($tokens, $assertCall); | ||
|
||
continue; | ||
} | ||
} | ||
|
||
foreach ($this->getPreviousAssertCall($tokens, $startIndex, $endIndex) as $assertCall) { | ||
$this->fixAssertNewNames($tokens, $assertCall); | ||
} | ||
} | ||
|
||
protected function createConfigurationDefinition(): FixerConfigurationResolverInterface | ||
|
@@ -259,6 +340,7 @@ | |
PhpUnitTargetVersion::VERSION_3_5, | ||
PhpUnitTargetVersion::VERSION_5_0, | ||
PhpUnitTargetVersion::VERSION_5_6, | ||
PhpUnitTargetVersion::VERSION_9_1, | ||
PhpUnitTargetVersion::VERSION_NEWEST, | ||
]) | ||
->setDefault(PhpUnitTargetVersion::VERSION_NEWEST) | ||
|
@@ -323,11 +405,12 @@ | |
|
||
$isPositive = $isPositive ? 'positive' : 'negative'; | ||
|
||
if (false === self::$fixMap[$content][$isPositive]) { | ||
$replace = self::$fixMap[$content][$isPositive]; | ||
if (false === $replace) { | ||
return; | ||
} | ||
|
||
$tokens[$assertCall['index']] = new Token([T_STRING, self::$fixMap[$content][$isPositive]]); | ||
$tokens[$assertCall['index']] = new Token([T_STRING, (string) $replace]); | ||
Check warning on line 413 in src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
$this->removeFunctionCall($tokens, $testDefaultNamespaceTokenIndex, $testIndex, $testOpenIndex, $testCloseIndex); | ||
|
||
if (self::$fixMap[$content]['swap_arguments'] ?? false) { | ||
|
@@ -484,7 +567,7 @@ | |
|
||
$lowerContent = strtolower($tokens[$countCallIndex]->getContent()); | ||
|
||
if ('count' !== $lowerContent && 'sizeof' !== $lowerContent) { | ||
if (!\in_array($lowerContent, ['count', 'sizeof'], true)) { | ||
return; // not a call to "count" or "sizeOf" | ||
} | ||
|
||
|
@@ -625,4 +708,39 @@ | |
|
||
return $clone; | ||
} | ||
|
||
/** | ||
* @param array{ | ||
* index: int, | ||
* loweredName: string, | ||
* openBraceIndex: int, | ||
* closeBraceIndex: int, | ||
* } $assertCall | ||
*/ | ||
private function fixAssertNewNames(Tokens $tokens, array $assertCall): void | ||
{ | ||
if (!\in_array($assertCall['loweredName'], $this->functions, true)) { | ||
return; | ||
} | ||
|
||
$replacement = null; | ||
if ('assertnotisreadable' === $assertCall['loweredName']) { | ||
$replacement = 'assertIsNotReadable'; | ||
} elseif ('assertnotiswritable' === $assertCall['loweredName']) { | ||
$replacement = 'assertIsNotWritable'; | ||
} elseif ('assertdirectorynotexists' === $assertCall['loweredName']) { | ||
$replacement = 'assertDirectoryDoesNotExist'; | ||
} elseif ('assertfilenotexists' === $assertCall['loweredName']) { | ||
$replacement = 'assertFileDoesNotExist'; | ||
} | ||
Comment on lines
+727
to
+735
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Strange that mess-detector prefers this over switch statement, not everything can be refactored to polymorphism, especially string comparison. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it complains because switch-case uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. $replacements = [
'assertnotisreadable' => 'assertIsNotReadable',
'assertnotiswritable' => 'assertIsNotWritable',
'assertdirectorynotexists' => 'assertDirectoryDoesNotExist',
'assertfilenotexists' => 'assertFileDoesNotExist',
];
$replacement = $replacements[$assertCall['loweredName']] ?? null; (we can't use |
||
|
||
if (null === $replacement) { | ||
return; | ||
} | ||
|
||
$tokens[$assertCall['index']] = new Token([ | ||
T_STRING, | ||
$replacement, | ||
]); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of PHP CS Fixer. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* Dariusz Rumiński <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace PhpCsFixer\RuleSet\Sets; | ||
|
||
use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion; | ||
use PhpCsFixer\RuleSet\AbstractMigrationSetDescription; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class PHPUnit91MigrationRiskySet extends AbstractMigrationSetDescription | ||
{ | ||
public function getRules(): array | ||
{ | ||
return [ | ||
Check warning on line 27 in src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php
|
||
'@PHPUnit84Migration:risky' => true, | ||
Check warning on line 28 in src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php
|
||
'php_unit_dedicate_assert' => [ | ||
'target' => PhpUnitTargetVersion::VERSION_9_1, | ||
], | ||
]; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to have this change reverted.
it's giving extra knowledge of "method X will / will not have those keys", while in fact we do not rely on it anywhere - we rely on contract that is "any method, if configured into array, will have those key obligatory and those keys optional" - for whole algo it doesn't matter which method will have which configuration.
then, you also changed configuration from
swap_arguments: true
toswap_arguments: bool
, which is extending the type (claiming it can takefalse
as value, which is not supported). same for changes fornegative