From 79316a633a351780dd381d372648e969de64cc32 Mon Sep 17 00:00:00 2001 From: Krzysztof Ciszewski Date: Wed, 8 May 2024 19:18:31 +0200 Subject: [PATCH 1/8] php-cs-fixer#7968 use new naming for some asserts --- .../PhpUnit/PhpUnitDedicateAssertFixer.php | 182 +++++++++++------- src/Fixer/PhpUnit/PhpUnitTargetVersion.php | 1 + .../PhpUnitDedicateAssertFixerTest.php | 66 ++++++- 3 files changed, 174 insertions(+), 75 deletions(-) diff --git a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php index c8a60a99035..a0d5c64cfe1 100644 --- a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php @@ -36,83 +36,22 @@ final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements C /** * @var array|true> */ - private static array $fixMap = [ - 'array_key_exists' => [ - 'positive' => 'assertArrayHasKey', - 'negative' => 'assertArrayNotHasKey', - 'argument_count' => 2, - ], - 'empty' => [ - 'positive' => 'assertEmpty', - 'negative' => 'assertNotEmpty', - ], - 'file_exists' => [ - 'positive' => 'assertFileExists', - 'negative' => 'assertFileNotExists', - ], - 'is_array' => true, - 'is_bool' => true, - 'is_callable' => true, - 'is_dir' => [ - 'positive' => 'assertDirectoryExists', - 'negative' => 'assertDirectoryNotExists', - ], - 'is_double' => true, - 'is_float' => true, - 'is_infinite' => [ - 'positive' => 'assertInfinite', - 'negative' => 'assertFinite', - ], - 'is_int' => true, - 'is_integer' => true, - 'is_long' => true, - 'is_nan' => [ - 'positive' => 'assertNan', - 'negative' => false, - ], - 'is_null' => [ - 'positive' => 'assertNull', - 'negative' => 'assertNotNull', - ], - 'is_numeric' => true, - 'is_object' => true, - 'is_readable' => [ - 'positive' => 'assertIsReadable', - 'negative' => 'assertNotIsReadable', - ], - 'is_real' => true, - 'is_resource' => true, - 'is_scalar' => true, - 'is_string' => true, - 'is_writable' => [ - 'positive' => 'assertIsWritable', - 'negative' => 'assertNotIsWritable', - ], - 'str_contains' => [ // since 7.5 - 'positive' => 'assertStringContainsString', - 'negative' => 'assertStringNotContainsString', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - 'str_ends_with' => [ // since 3.4 - 'positive' => 'assertStringEndsWith', - 'negative' => 'assertStringEndsNotWith', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - 'str_starts_with' => [ // since 3.4 - 'positive' => 'assertStringStartsWith', - 'negative' => 'assertStringStartsNotWith', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - ]; + private static array $fixMap = []; /** * @var list */ private array $functions = []; + public function __construct() + { + parent::__construct(); + + if (self::$fixMap === []) { + self::$fixMap = $this->getFixMap(); + } + } + public function configure(array $configuration): void { parent::configure($configuration); @@ -169,6 +108,29 @@ public function configure(array $configuration): void 'str_contains', ]); } + + if (PhpUnitTargetVersion::fulfills($this->configuration['target'], PhpUnitTargetVersion::VERSION_9_1)) { + self::$fixMap = array_merge(self::$fixMap, [ + 'is_readable' => [ + ...self::$fixMap['is_readable'] ?? [], + 'negative' => 'assertIsNotReadable', + ], + 'is_writable' => [ + ...self::$fixMap['is_writable'] ?? [], + 'negative' => 'assertIsNotWritable', + ], + 'file_exists' => [ + ...self::$fixMap['file_exists'] ?? [], + 'negative' => 'assertFileDoesNotExist', + ], + 'is_dir' => [ + ...self::$fixMap['is_dir'] ?? [], + 'negative' => 'assertDirectoryDoesNotExist', + ], + ]); + } else { + self::$fixMap = $this->getFixMap(); + } } public function isRisky(): bool @@ -243,8 +205,6 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en || 'assertnotequals' === $assertCall['loweredName'] ) { $this->fixAssertSameEquals($tokens, $assertCall); - - continue; } } } @@ -259,6 +219,7 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn PhpUnitTargetVersion::VERSION_3_5, PhpUnitTargetVersion::VERSION_5_0, PhpUnitTargetVersion::VERSION_5_6, + PhpUnitTargetVersion::VERSION_9_1, PhpUnitTargetVersion::VERSION_NEWEST, ]) ->setDefault(PhpUnitTargetVersion::VERSION_NEWEST) @@ -625,4 +586,79 @@ private function cloneAndClearTokens(Tokens $tokens, int $start, int $end): arra return $clone; } + + private function getFixMap(): array + { + return [ + 'array_key_exists' => [ + 'positive' => 'assertArrayHasKey', + 'negative' => 'assertArrayNotHasKey', + 'argument_count' => 2, + ], + 'empty' => [ + 'positive' => 'assertEmpty', + 'negative' => 'assertNotEmpty', + ], + 'file_exists' => [ + 'positive' => 'assertFileExists', + 'negative' => 'assertFileNotExists', + ], + 'is_array' => true, + 'is_bool' => true, + 'is_callable' => true, + 'is_dir' => [ + 'positive' => 'assertDirectoryExists', + 'negative' => 'assertDirectoryNotExists', + ], + 'is_double' => true, + 'is_float' => true, + 'is_infinite' => [ + 'positive' => 'assertInfinite', + 'negative' => 'assertFinite', + ], + 'is_int' => true, + 'is_integer' => true, + 'is_long' => true, + 'is_nan' => [ + 'positive' => 'assertNan', + 'negative' => false, + ], + 'is_null' => [ + 'positive' => 'assertNull', + 'negative' => 'assertNotNull', + ], + 'is_numeric' => true, + 'is_object' => true, + 'is_readable' => [ + 'positive' => 'assertIsReadable', + 'negative' => 'assertNotIsReadable', + ], + 'is_real' => true, + 'is_resource' => true, + 'is_scalar' => true, + 'is_string' => true, + 'is_writable' => [ + 'positive' => 'assertIsWritable', + 'negative' => 'assertNotIsWritable', + ], + 'str_contains' => [ // since 7.5 + 'positive' => 'assertStringContainsString', + 'negative' => 'assertStringNotContainsString', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + 'str_ends_with' => [ // since 3.4 + 'positive' => 'assertStringEndsWith', + 'negative' => 'assertStringEndsNotWith', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + 'str_starts_with' => [ // since 3.4 + 'positive' => 'assertStringStartsWith', + 'negative' => 'assertStringStartsNotWith', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + ]; + } } diff --git a/src/Fixer/PhpUnit/PhpUnitTargetVersion.php b/src/Fixer/PhpUnit/PhpUnitTargetVersion.php index 4bd1800a693..bd3475c6b93 100644 --- a/src/Fixer/PhpUnit/PhpUnitTargetVersion.php +++ b/src/Fixer/PhpUnit/PhpUnitTargetVersion.php @@ -37,6 +37,7 @@ final class PhpUnitTargetVersion public const VERSION_6_0 = '6.0'; public const VERSION_7_5 = '7.5'; public const VERSION_8_4 = '8.4'; + public const VERSION_9_1 = '9.1'; public const VERSION_NEWEST = 'newest'; private function __construct() {} diff --git a/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php b/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php index 64035400e4d..bfca5f9104a 100644 --- a/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php +++ b/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php @@ -56,9 +56,9 @@ public static function provideFixCases(): iterable yield [ self::generateTest(' $this->assertFileExists($a); - $this->assertFileNotExists($a); + $this->assertFileDoesNotExist($a); $this->assertFileExists($a); - $this->assertFileNotExists($a); + $this->assertFileDoesNotExist($a); '), self::generateTest(' $this->assertTrue(file_exists($a)); @@ -325,6 +325,68 @@ public static function provideFixCases(): iterable $this->assertTrue(!$y instanceof SomeClass, $message); '), ]; + + yield [ + self::generateTest( + ' + $this->assertFileDoesNotExist($a); + $this->assertFileDoesNotExist($a); + ' + ), + self::generateTest( + ' + $this->assertFalse(file_exists($a)); + $this->assertFalse(\file_exists($a)); + ' + ), + ['target' => PhpUnitTargetVersion::VERSION_9_1], + ]; + + yield [ + self::generateTest( + ' + $this->assertIsNotReadable($a); + $this->assertIsNotReadable($a); + ' + ), + self::generateTest( + ' + $this->assertFalse(is_readable($a)); + $this->assertFalse(\is_readable($a)); + ' + ), + ['target' => PhpUnitTargetVersion::VERSION_9_1], + ]; + yield [ + self::generateTest( + ' + $this->assertIsNotWritable($a); + $this->assertIsNotWritable($a); + ' + ), + self::generateTest( + ' + $this->assertFalse(is_writable($a)); + $this->assertFalse(\is_writable($a)); + ' + ), + ['target' => PhpUnitTargetVersion::VERSION_9_1], + ]; + yield [ + self::generateTest( + ' + $this->assertDirectoryDoesNotExist($a); + $this->assertDirectoryDoesNotExist($a); + ' + ), + self::generateTest( + ' + $this->assertFalse(is_dir($a)); + $this->assertFalse(\is_dir($a)); + ' + ), + ['target' => PhpUnitTargetVersion::VERSION_9_1], + ]; } /** From 2c8ca76b8b6b77aa949fec38649d4b1d35ae3b2f Mon Sep 17 00:00:00 2001 From: Krzysztof Ciszewski Date: Wed, 8 May 2024 20:51:19 +0200 Subject: [PATCH 2/8] php-cs-fixer#7968 fix cs --- .../PhpUnit/PhpUnitDedicateAssertFixer.php | 42 +++++++++++-------- .../PhpUnitDedicateAssertFixerTest.php | 2 + 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php index a0d5c64cfe1..981d9cbe6e0 100644 --- a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php @@ -47,7 +47,7 @@ public function __construct() { parent::__construct(); - if (self::$fixMap === []) { + if ([] === self::$fixMap) { self::$fixMap = $this->getFixMap(); } } @@ -111,22 +111,30 @@ public function configure(array $configuration): void if (PhpUnitTargetVersion::fulfills($this->configuration['target'], PhpUnitTargetVersion::VERSION_9_1)) { self::$fixMap = array_merge(self::$fixMap, [ - 'is_readable' => [ - ...self::$fixMap['is_readable'] ?? [], - 'negative' => 'assertIsNotReadable', - ], - 'is_writable' => [ - ...self::$fixMap['is_writable'] ?? [], - 'negative' => 'assertIsNotWritable', - ], - 'file_exists' => [ - ...self::$fixMap['file_exists'] ?? [], - 'negative' => 'assertFileDoesNotExist', - ], - 'is_dir' => [ - ...self::$fixMap['is_dir'] ?? [], - 'negative' => 'assertDirectoryDoesNotExist', - ], + 'is_readable' => array_merge( + self::$fixMap['is_readable'] ?? [], + [ + 'negative' => 'assertIsNotReadable', + ] + ), + 'is_writable' => array_merge( + self::$fixMap['is_writable'] ?? [], + [ + 'negative' => 'assertIsNotWritable', + ] + ), + 'file_exists' => array_merge( + self::$fixMap['file_exists'] ?? [], + [ + 'negative' => 'assertFileDoesNotExist', + ] + ), + 'is_dir' => array_merge( + self::$fixMap['is_dir'] ?? [], + [ + 'negative' => 'assertDirectoryDoesNotExist', + ] + ), ]); } else { self::$fixMap = $this->getFixMap(); diff --git a/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php b/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php index bfca5f9104a..3378c0d5d3a 100644 --- a/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php +++ b/tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php @@ -357,6 +357,7 @@ public static function provideFixCases(): iterable ), ['target' => PhpUnitTargetVersion::VERSION_9_1], ]; + yield [ self::generateTest( ' @@ -372,6 +373,7 @@ public static function provideFixCases(): iterable ), ['target' => PhpUnitTargetVersion::VERSION_9_1], ]; + yield [ self::generateTest( ' From 01939555162f8a9474299199a9bd2087f08fa554 Mon Sep 17 00:00:00 2001 From: Krzysztof Ciszewski Date: Thu, 9 May 2024 16:12:22 +0200 Subject: [PATCH 3/8] php-cs-fixer#7968 added new ruleset, fixed tests --- doc/ruleSets/PHPUnit100MigrationRisky.rst | 2 +- doc/ruleSets/PHPUnit91MigrationRisky.rst | 22 ++++ doc/ruleSets/index.rst | 1 + .../php_unit/php_unit_dedicate_assert.rst | 8 +- ...php_unit_dedicate_assert_internal_type.rst | 4 + doc/rules/php_unit/php_unit_expectation.rst | 4 + doc/rules/php_unit/php_unit_mock.rst | 4 + doc/rules/php_unit/php_unit_namespaced.rst | 4 + .../php_unit_no_expectation_annotation.rst | 4 + .../PhpUnit/PhpUnitDedicateAssertFixer.php | 105 ++++++++++++++---- .../Sets/PHPUnit100MigrationRiskySet.php | 2 +- .../Sets/PHPUnit91MigrationRiskySet.php | 34 ++++++ .../set/@PHPUnit91Migration-risky.test | 4 + .../set/@PHPUnit91Migration-risky.test-in.php | 19 ++++ .../@PHPUnit91Migration-risky.test-out.php | 19 ++++ tests/RuleSet/RuleSetsTest.php | 1 + .../Sets/PHPUnit91MigrationRiskySetTest.php | 22 ++++ 17 files changed, 233 insertions(+), 26 deletions(-) create mode 100644 doc/ruleSets/PHPUnit91MigrationRisky.rst create mode 100644 src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php create mode 100644 tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test create mode 100644 tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test-in.php create mode 100644 tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test-out.php create mode 100644 tests/RuleSet/Sets/PHPUnit91MigrationRiskySetTest.php diff --git a/doc/ruleSets/PHPUnit100MigrationRisky.rst b/doc/ruleSets/PHPUnit100MigrationRisky.rst index ea7ef946b98..ae2134d872f 100644 --- a/doc/ruleSets/PHPUnit100MigrationRisky.rst +++ b/doc/ruleSets/PHPUnit100MigrationRisky.rst @@ -15,7 +15,7 @@ Using this rule set may lead to changes in your code's logic and behaviour. Use Rules ----- -- `@PHPUnit84Migration:risky <./PHPUnit84MigrationRisky.rst>`_ +- `@PHPUnit91Migration:risky <./PHPUnit91MigrationRisky.rst>`_ - `php_unit_data_provider_static <./../rules/php_unit/php_unit_data_provider_static.rst>`_ with config: ``['force' => true]`` diff --git a/doc/ruleSets/PHPUnit91MigrationRisky.rst b/doc/ruleSets/PHPUnit91MigrationRisky.rst new file mode 100644 index 00000000000..3521d97b298 --- /dev/null +++ b/doc/ruleSets/PHPUnit91MigrationRisky.rst @@ -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']`` + diff --git a/doc/ruleSets/index.rst b/doc/ruleSets/index.rst index afe3dc738c9..b4473e3e57c 100644 --- a/doc/ruleSets/index.rst +++ b/doc/ruleSets/index.rst @@ -39,6 +39,7 @@ List of Available Rule sets - `@PHPUnit60Migration:risky <./PHPUnit60MigrationRisky.rst>`_ - `@PHPUnit75Migration:risky <./PHPUnit75MigrationRisky.rst>`_ - `@PHPUnit84Migration:risky <./PHPUnit84MigrationRisky.rst>`_ +- `@PHPUnit91Migration:risky <./PHPUnit91MigrationRisky.rst>`_ - `@PHPUnit100Migration:risky <./PHPUnit100MigrationRisky.rst>`_ - `@PSR1 <./PSR1.rst>`_ - `@PSR2 <./PSR2.rst>`_ diff --git a/doc/rules/php_unit/php_unit_dedicate_assert.rst b/doc/rules/php_unit/php_unit_dedicate_assert.rst index c0b254c8120..2aa03d4a2da 100644 --- a/doc/rules/php_unit/php_unit_dedicate_assert.rst +++ b/doc/rules/php_unit/php_unit_dedicate_assert.rst @@ -21,7 +21,7 @@ Configuration Target version of PHPUnit. -Allowed values: ``'3.0'``, ``'3.5'``, ``'5.0'``, ``'5.6'`` and ``'newest'`` +Allowed values: ``'3.0'``, ``'3.5'``, ``'5.0'``, ``'5.6'``, ``'9.1'`` and ``'newest'`` Default value: ``'newest'`` @@ -133,9 +133,13 @@ The rule is part of the following rule sets: ``['target' => '5.6']`` +- `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config: + + ``['target' => '9.1']`` + - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config: - ``['target' => '5.6']`` + ``['target' => '9.1']`` References diff --git a/doc/rules/php_unit/php_unit_dedicate_assert_internal_type.rst b/doc/rules/php_unit/php_unit_dedicate_assert_internal_type.rst index 92ce289be63..23c37b304c2 100644 --- a/doc/rules/php_unit/php_unit_dedicate_assert_internal_type.rst +++ b/doc/rules/php_unit/php_unit_dedicate_assert_internal_type.rst @@ -84,6 +84,10 @@ The rule is part of the following rule sets: ``['target' => '7.5']`` +- `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config: + + ``['target' => '7.5']`` + - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config: ``['target' => '7.5']`` diff --git a/doc/rules/php_unit/php_unit_expectation.rst b/doc/rules/php_unit/php_unit_expectation.rst index dc339529f14..cb8a139d498 100644 --- a/doc/rules/php_unit/php_unit_expectation.rst +++ b/doc/rules/php_unit/php_unit_expectation.rst @@ -185,6 +185,10 @@ The rule is part of the following rule sets: ``['target' => '8.4']`` +- `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config: + + ``['target' => '8.4']`` + - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config: ``['target' => '8.4']`` diff --git a/doc/rules/php_unit/php_unit_mock.rst b/doc/rules/php_unit/php_unit_mock.rst index 9cc1e13d8ed..4263e5bf99e 100644 --- a/doc/rules/php_unit/php_unit_mock.rst +++ b/doc/rules/php_unit/php_unit_mock.rst @@ -106,6 +106,10 @@ The rule is part of the following rule sets: ``['target' => '5.5']`` +- `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config: + + ``['target' => '5.5']`` + - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config: ``['target' => '5.5']`` diff --git a/doc/rules/php_unit/php_unit_namespaced.rst b/doc/rules/php_unit/php_unit_namespaced.rst index 98b47d58278..9f28dedc84b 100644 --- a/doc/rules/php_unit/php_unit_namespaced.rst +++ b/doc/rules/php_unit/php_unit_namespaced.rst @@ -126,6 +126,10 @@ The rule is part of the following rule sets: ``['target' => '6.0']`` +- `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config: + + ``['target' => '6.0']`` + - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config: ``['target' => '6.0']`` diff --git a/doc/rules/php_unit/php_unit_no_expectation_annotation.rst b/doc/rules/php_unit/php_unit_no_expectation_annotation.rst index cf73e00c477..728063e7ad1 100644 --- a/doc/rules/php_unit/php_unit_no_expectation_annotation.rst +++ b/doc/rules/php_unit/php_unit_no_expectation_annotation.rst @@ -153,6 +153,10 @@ The rule is part of the following rule sets: ``['target' => '4.3']`` +- `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config: + + ``['target' => '4.3']`` + - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config: ``['target' => '4.3']`` diff --git a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php index 981d9cbe6e0..e53f23ebeee 100644 --- a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php @@ -36,26 +36,89 @@ final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements C /** * @var array|true> */ - private static array $fixMap = []; + private array $fixMap = [ + 'array_key_exists' => [ + 'positive' => 'assertArrayHasKey', + 'negative' => 'assertArrayNotHasKey', + 'argument_count' => 2, + ], + 'empty' => [ + 'positive' => 'assertEmpty', + 'negative' => 'assertNotEmpty', + ], + 'file_exists' => [ + 'positive' => 'assertFileExists', + 'negative' => 'assertFileNotExists', + ], + 'is_array' => true, + 'is_bool' => true, + 'is_callable' => true, + 'is_dir' => [ + 'positive' => 'assertDirectoryExists', + 'negative' => 'assertDirectoryNotExists', + ], + 'is_double' => true, + 'is_float' => true, + 'is_infinite' => [ + 'positive' => 'assertInfinite', + 'negative' => 'assertFinite', + ], + 'is_int' => true, + 'is_integer' => true, + 'is_long' => true, + 'is_nan' => [ + 'positive' => 'assertNan', + 'negative' => false, + ], + 'is_null' => [ + 'positive' => 'assertNull', + 'negative' => 'assertNotNull', + ], + 'is_numeric' => true, + 'is_object' => true, + 'is_readable' => [ + 'positive' => 'assertIsReadable', + 'negative' => 'assertNotIsReadable', + ], + 'is_real' => true, + 'is_resource' => true, + 'is_scalar' => true, + 'is_string' => true, + 'is_writable' => [ + 'positive' => 'assertIsWritable', + 'negative' => 'assertNotIsWritable', + ], + 'str_contains' => [ // since 7.5 + 'positive' => 'assertStringContainsString', + 'negative' => 'assertStringNotContainsString', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + 'str_ends_with' => [ // since 3.4 + 'positive' => 'assertStringEndsWith', + 'negative' => 'assertStringEndsNotWith', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + 'str_starts_with' => [ // since 3.4 + 'positive' => 'assertStringStartsWith', + 'negative' => 'assertStringStartsNotWith', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + ]; /** * @var list */ private array $functions = []; - public function __construct() - { - parent::__construct(); - - if ([] === self::$fixMap) { - self::$fixMap = $this->getFixMap(); - } - } - public function configure(array $configuration): void { parent::configure($configuration); + $this->fixMap = $this->getFixMap(); + // assertions added in 3.0: assertArrayNotHasKey assertArrayHasKey assertFileNotExists assertFileExists assertNotNull, assertNull $this->functions = [ 'array_key_exists', @@ -110,34 +173,32 @@ public function configure(array $configuration): void } if (PhpUnitTargetVersion::fulfills($this->configuration['target'], PhpUnitTargetVersion::VERSION_9_1)) { - self::$fixMap = array_merge(self::$fixMap, [ + $this->fixMap = array_merge($this->fixMap, [ 'is_readable' => array_merge( - self::$fixMap['is_readable'] ?? [], + $this->fixMap['is_readable'] ?? [], [ 'negative' => 'assertIsNotReadable', ] ), 'is_writable' => array_merge( - self::$fixMap['is_writable'] ?? [], + $this->fixMap['is_writable'] ?? [], [ 'negative' => 'assertIsNotWritable', ] ), 'file_exists' => array_merge( - self::$fixMap['file_exists'] ?? [], + $this->fixMap['file_exists'] ?? [], [ 'negative' => 'assertFileDoesNotExist', ] ), 'is_dir' => array_merge( - self::$fixMap['is_dir'] ?? [], + $this->fixMap['is_dir'] ?? [], [ 'negative' => 'assertDirectoryDoesNotExist', ] ), ]); - } else { - self::$fixMap = $this->getFixMap(); } } @@ -283,8 +344,8 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments $arguments = $argumentsAnalyzer->getArguments($tokens, $testOpenIndex, $testCloseIndex); $isPositive = 'asserttrue' === $assertCall['loweredName']; - if (\is_array(self::$fixMap[$content])) { - $expectedCount = self::$fixMap[$content]['argument_count'] ?? 1; + if (\is_array($this->fixMap[$content])) { + $expectedCount = $this->fixMap[$content]['argument_count'] ?? 1; if ($expectedCount !== \count($arguments)) { return; @@ -292,14 +353,14 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments $isPositive = $isPositive ? 'positive' : 'negative'; - if (false === self::$fixMap[$content][$isPositive]) { + if (false === $this->fixMap[$content][$isPositive]) { return; } - $tokens[$assertCall['index']] = new Token([T_STRING, self::$fixMap[$content][$isPositive]]); + $tokens[$assertCall['index']] = new Token([T_STRING, $this->fixMap[$content][$isPositive]]); $this->removeFunctionCall($tokens, $testDefaultNamespaceTokenIndex, $testIndex, $testOpenIndex, $testCloseIndex); - if (self::$fixMap[$content]['swap_arguments'] ?? false) { + if ($this->fixMap[$content]['swap_arguments'] ?? false) { if (2 !== $expectedCount) { throw new \RuntimeException('Can only swap two arguments, please update map or logic.'); } diff --git a/src/RuleSet/Sets/PHPUnit100MigrationRiskySet.php b/src/RuleSet/Sets/PHPUnit100MigrationRiskySet.php index 433fa7acbc0..f2092648f5c 100644 --- a/src/RuleSet/Sets/PHPUnit100MigrationRiskySet.php +++ b/src/RuleSet/Sets/PHPUnit100MigrationRiskySet.php @@ -24,7 +24,7 @@ final class PHPUnit100MigrationRiskySet extends AbstractMigrationSetDescription public function getRules(): array { return [ - '@PHPUnit84Migration:risky' => true, + '@PHPUnit91Migration:risky' => true, 'php_unit_data_provider_static' => ['force' => true], ]; } diff --git a/src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php b/src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php new file mode 100644 index 00000000000..ca6a9f0efb4 --- /dev/null +++ b/src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php @@ -0,0 +1,34 @@ + + * Dariusz Rumiński + * + * 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 [ + '@PHPUnit84Migration:risky' => true, + 'php_unit_dedicate_assert' => [ + 'target' => PhpUnitTargetVersion::VERSION_9_1, + ], + ]; + } +} diff --git a/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test b/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test new file mode 100644 index 00000000000..0a1758bfe7a --- /dev/null +++ b/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test @@ -0,0 +1,4 @@ +--TEST-- +Integration of @PHPUnit91Migration:risky. +--RULESET-- +{"@PHPUnit91Migration:risky": true} diff --git a/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test-in.php b/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test-in.php new file mode 100644 index 00000000000..872ff408837 --- /dev/null +++ b/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test-in.php @@ -0,0 +1,19 @@ +assertFalse(is_dir($foo)); + $this->assertFalse(is_writable($foo)); + $this->assertFalse(file_exists($foo)); + $this->assertFalse(is_readable($foo)); + } +} diff --git a/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test-out.php b/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test-out.php new file mode 100644 index 00000000000..c45240dd18b --- /dev/null +++ b/tests/Fixtures/Integration/set/@PHPUnit91Migration-risky.test-out.php @@ -0,0 +1,19 @@ +assertDirectoryDoesNotExist($foo); + $this->assertIsNotWritable($foo); + $this->assertFileDoesNotExist($foo); + $this->assertIsNotReadable($foo); + } +} diff --git a/tests/RuleSet/RuleSetsTest.php b/tests/RuleSet/RuleSetsTest.php index 7458187ff83..bed20f523f0 100644 --- a/tests/RuleSet/RuleSetsTest.php +++ b/tests/RuleSet/RuleSetsTest.php @@ -83,6 +83,7 @@ public function testHasIntegrationTest(string $setDefinitionName): void '@PHPUnit55Migration:risky', '@PHPUnit75Migration:risky', '@PHPUnit84Migration:risky', + '@PHPUnit91Migration:risky', '@PHPUnit100Migration:risky', '@PSR1', ]; diff --git a/tests/RuleSet/Sets/PHPUnit91MigrationRiskySetTest.php b/tests/RuleSet/Sets/PHPUnit91MigrationRiskySetTest.php new file mode 100644 index 00000000000..ce2b74e64b4 --- /dev/null +++ b/tests/RuleSet/Sets/PHPUnit91MigrationRiskySetTest.php @@ -0,0 +1,22 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\RuleSet\Sets; + +/** + * @internal + * + * @covers \PhpCsFixer\RuleSet\Sets\PHPUnit91MigrationRiskySet + */ +final class PHPUnit91MigrationRiskySetTest extends AbstractSetTestCase {} From ae5c56feb66f3ac27d285dddd4a8279ff0871303 Mon Sep 17 00:00:00 2001 From: Krzysztof Ciszewski Date: Thu, 9 May 2024 16:27:50 +0200 Subject: [PATCH 4/8] php-cs-fixer#7968 fix phpstan --- .../PhpUnit/PhpUnitDedicateAssertFixer.php | 77 ++----------------- 1 file changed, 5 insertions(+), 72 deletions(-) diff --git a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php index abb95769be5..5a3b8633513 100644 --- a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php @@ -34,79 +34,9 @@ final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements ConfigurableFixerInterface { /** - * @var array + * @var array|array */ - private array $fixMap = [ - 'array_key_exists' => [ - 'positive' => 'assertArrayHasKey', - 'negative' => 'assertArrayNotHasKey', - 'argument_count' => 2, - ], - 'empty' => [ - 'positive' => 'assertEmpty', - 'negative' => 'assertNotEmpty', - ], - 'file_exists' => [ - 'positive' => 'assertFileExists', - 'negative' => 'assertFileNotExists', - ], - 'is_array' => true, - 'is_bool' => true, - 'is_callable' => true, - 'is_dir' => [ - 'positive' => 'assertDirectoryExists', - 'negative' => 'assertDirectoryNotExists', - ], - 'is_double' => true, - 'is_float' => true, - 'is_infinite' => [ - 'positive' => 'assertInfinite', - 'negative' => 'assertFinite', - ], - 'is_int' => true, - 'is_integer' => true, - 'is_long' => true, - 'is_nan' => [ - 'positive' => 'assertNan', - 'negative' => false, - ], - 'is_null' => [ - 'positive' => 'assertNull', - 'negative' => 'assertNotNull', - ], - 'is_numeric' => true, - 'is_object' => true, - 'is_readable' => [ - 'positive' => 'assertIsReadable', - 'negative' => 'assertNotIsReadable', - ], - 'is_real' => true, - 'is_resource' => true, - 'is_scalar' => true, - 'is_string' => true, - 'is_writable' => [ - 'positive' => 'assertIsWritable', - 'negative' => 'assertNotIsWritable', - ], - 'str_contains' => [ // since 7.5 - 'positive' => 'assertStringContainsString', - 'negative' => 'assertStringNotContainsString', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - 'str_ends_with' => [ // since 3.4 - 'positive' => 'assertStringEndsWith', - 'negative' => 'assertStringEndsNotWith', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - 'str_starts_with' => [ // since 3.4 - 'positive' => 'assertStringStartsWith', - 'negative' => 'assertStringStartsNotWith', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - ]; + private array $fixMap = []; /** * @var list @@ -656,6 +586,9 @@ private function cloneAndClearTokens(Tokens $tokens, int $start, int $end): arra return $clone; } + /** + * @return array|array + */ private function getFixMap(): array { return [ From e9c40874f73f2589d58201ca3b33073b4b5b7f43 Mon Sep 17 00:00:00 2001 From: Krzysztof Ciszewski Date: Thu, 9 May 2024 17:20:22 +0200 Subject: [PATCH 5/8] php-cs-fixer#7968 fix phpstan --- .../PhpUnit/PhpUnitDedicateAssertFixer.php | 153 +++++++++++++++++- 1 file changed, 147 insertions(+), 6 deletions(-) diff --git a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php index 5a3b8633513..574941bf2eb 100644 --- a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php @@ -34,9 +34,79 @@ final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements ConfigurableFixerInterface { /** - * @var array|array + * @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 array $fixMap = []; + private array $fixMap; /** * @var list @@ -283,11 +353,12 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments $isPositive = $isPositive ? 'positive' : 'negative'; - if (false === $this->fixMap[$content][$isPositive]) { + $replace = $this->fixMap[$content][$isPositive]; + if (false === $replace) { return; } - $tokens[$assertCall['index']] = new Token([T_STRING, $this->fixMap[$content][$isPositive]]); + $tokens[$assertCall['index']] = new Token([T_STRING, (string) $replace]); $this->removeFunctionCall($tokens, $testDefaultNamespaceTokenIndex, $testIndex, $testOpenIndex, $testCloseIndex); if ($this->fixMap[$content]['swap_arguments'] ?? false) { @@ -444,7 +515,7 @@ private function fixAssertSameEquals(Tokens $tokens, array $assertCall): void $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" } @@ -587,7 +658,77 @@ private function cloneAndClearTokens(Tokens $tokens, int $start, int $end): arra } /** - * @return array|array + * @return 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 function getFixMap(): array { From 9850e4100392c96cf7117e5e6299480e1784ecf9 Mon Sep 17 00:00:00 2001 From: Krzysztof Ciszewski Date: Sat, 11 May 2024 09:47:52 +0200 Subject: [PATCH 6/8] php-cs-fixer#7968 refactor into proper rule --- .../PhpUnit/PhpUnitDedicateAssertFixer.php | 317 ++++++++---------- 1 file changed, 135 insertions(+), 182 deletions(-) diff --git a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php index 574941bf2eb..a9d23458797 100644 --- a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php @@ -106,7 +106,78 @@ final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements C * } * } */ - private array $fixMap; + private static array $fixMap = [ + 'array_key_exists' => [ + 'positive' => 'assertArrayHasKey', + 'negative' => 'assertArrayNotHasKey', + 'argument_count' => 2, + ], + 'empty' => [ + 'positive' => 'assertEmpty', + 'negative' => 'assertNotEmpty', + ], + 'file_exists' => [ + 'positive' => 'assertFileExists', + 'negative' => 'assertFileNotExists', + ], + 'is_array' => true, + 'is_bool' => true, + 'is_callable' => true, + 'is_dir' => [ + 'positive' => 'assertDirectoryExists', + 'negative' => 'assertDirectoryNotExists', + ], + 'is_double' => true, + 'is_float' => true, + 'is_infinite' => [ + 'positive' => 'assertInfinite', + 'negative' => 'assertFinite', + ], + 'is_int' => true, + 'is_integer' => true, + 'is_long' => true, + 'is_nan' => [ + 'positive' => 'assertNan', + 'negative' => false, + ], + 'is_null' => [ + 'positive' => 'assertNull', + 'negative' => 'assertNotNull', + ], + 'is_numeric' => true, + 'is_object' => true, + 'is_readable' => [ + 'positive' => 'assertIsReadable', + 'negative' => 'assertNotIsReadable', + ], + 'is_real' => true, + 'is_resource' => true, + 'is_scalar' => true, + 'is_string' => true, + 'is_writable' => [ + 'positive' => 'assertIsWritable', + 'negative' => 'assertNotIsWritable', + ], + 'str_contains' => [ // since 7.5 + 'positive' => 'assertStringContainsString', + 'negative' => 'assertStringNotContainsString', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + 'str_ends_with' => [ // since 3.4 + 'positive' => 'assertStringEndsWith', + 'negative' => 'assertStringEndsNotWith', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + 'str_starts_with' => [ // since 3.4 + 'positive' => 'assertStringStartsWith', + 'negative' => 'assertStringStartsNotWith', + 'argument_count' => 2, + 'swap_arguments' => true, + ], + 'assertNotIsReadable' => true, + ]; /** * @var list @@ -117,8 +188,6 @@ public function configure(array $configuration): void { parent::configure($configuration); - $this->fixMap = $this->getFixMap(); - // assertions added in 3.0: assertArrayNotHasKey assertArrayHasKey assertFileNotExists assertFileExists assertNotNull, assertNull $this->functions = [ 'array_key_exists', @@ -173,31 +242,11 @@ public function configure(array $configuration): void } if (PhpUnitTargetVersion::fulfills($this->configuration['target'], PhpUnitTargetVersion::VERSION_9_1)) { - $this->fixMap = array_merge($this->fixMap, [ - 'is_readable' => array_merge( - $this->fixMap['is_readable'] ?? [], - [ - 'negative' => 'assertIsNotReadable', - ] - ), - 'is_writable' => array_merge( - $this->fixMap['is_writable'] ?? [], - [ - 'negative' => 'assertIsNotWritable', - ] - ), - 'file_exists' => array_merge( - $this->fixMap['file_exists'] ?? [], - [ - 'negative' => 'assertFileDoesNotExist', - ] - ), - 'is_dir' => array_merge( - $this->fixMap['is_dir'] ?? [], - [ - 'negative' => 'assertDirectoryDoesNotExist', - ] - ), + $this->functions = array_merge($this->functions, [ + 'assertdirectorynotexists', + 'assertnotisreadable', + 'assertnotiswritable', + 'assertfilenotexists', ]); } } @@ -261,21 +310,24 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en 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); } } + + foreach ($this->getPreviousAssertCall($tokens, $startIndex, $endIndex) as $assertCall) { + $this->fixAssertNewNames($tokens, $assertCall); + } } protected function createConfigurationDefinition(): FixerConfigurationResolverInterface @@ -344,8 +396,8 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments $arguments = $argumentsAnalyzer->getArguments($tokens, $testOpenIndex, $testCloseIndex); $isPositive = 'asserttrue' === $assertCall['loweredName']; - if (\is_array($this->fixMap[$content])) { - $expectedCount = $this->fixMap[$content]['argument_count'] ?? 1; + if (\is_array(self::$fixMap[$content])) { + $expectedCount = self::$fixMap[$content]['argument_count'] ?? 1; if ($expectedCount !== \count($arguments)) { return; @@ -353,7 +405,7 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments $isPositive = $isPositive ? 'positive' : 'negative'; - $replace = $this->fixMap[$content][$isPositive]; + $replace = self::$fixMap[$content][$isPositive]; if (false === $replace) { return; } @@ -361,7 +413,7 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments $tokens[$assertCall['index']] = new Token([T_STRING, (string) $replace]); $this->removeFunctionCall($tokens, $testDefaultNamespaceTokenIndex, $testIndex, $testOpenIndex, $testCloseIndex); - if ($this->fixMap[$content]['swap_arguments'] ?? false) { + if (self::$fixMap[$content]['swap_arguments'] ?? false) { if (2 !== $expectedCount) { throw new \RuntimeException('Can only swap two arguments, please update map or logic.'); } @@ -658,150 +710,51 @@ private function cloneAndClearTokens(Tokens $tokens, int $start, int $end): arra } /** - * @return 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 - * } - * } + * @param array{ + * index: int, + * loweredName: string, + * openBraceIndex: int, + * closeBraceIndex: int, + * } $assertCall */ - private function getFixMap(): array + private function fixAssertNewNames(Tokens $tokens, array $assertCall): void { - return [ - 'array_key_exists' => [ - 'positive' => 'assertArrayHasKey', - 'negative' => 'assertArrayNotHasKey', - 'argument_count' => 2, - ], - 'empty' => [ - 'positive' => 'assertEmpty', - 'negative' => 'assertNotEmpty', - ], - 'file_exists' => [ - 'positive' => 'assertFileExists', - 'negative' => 'assertFileNotExists', - ], - 'is_array' => true, - 'is_bool' => true, - 'is_callable' => true, - 'is_dir' => [ - 'positive' => 'assertDirectoryExists', - 'negative' => 'assertDirectoryNotExists', - ], - 'is_double' => true, - 'is_float' => true, - 'is_infinite' => [ - 'positive' => 'assertInfinite', - 'negative' => 'assertFinite', - ], - 'is_int' => true, - 'is_integer' => true, - 'is_long' => true, - 'is_nan' => [ - 'positive' => 'assertNan', - 'negative' => false, - ], - 'is_null' => [ - 'positive' => 'assertNull', - 'negative' => 'assertNotNull', - ], - 'is_numeric' => true, - 'is_object' => true, - 'is_readable' => [ - 'positive' => 'assertIsReadable', - 'negative' => 'assertNotIsReadable', - ], - 'is_real' => true, - 'is_resource' => true, - 'is_scalar' => true, - 'is_string' => true, - 'is_writable' => [ - 'positive' => 'assertIsWritable', - 'negative' => 'assertNotIsWritable', - ], - 'str_contains' => [ // since 7.5 - 'positive' => 'assertStringContainsString', - 'negative' => 'assertStringNotContainsString', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - 'str_ends_with' => [ // since 3.4 - 'positive' => 'assertStringEndsWith', - 'negative' => 'assertStringEndsNotWith', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - 'str_starts_with' => [ // since 3.4 - 'positive' => 'assertStringStartsWith', - 'negative' => 'assertStringStartsNotWith', - 'argument_count' => 2, - 'swap_arguments' => true, - ], - ]; + if (!\in_array($assertCall['loweredName'], $this->functions, true)) { + return; + } + + switch ($assertCall['loweredName']) { + case 'assertnotisreadable': + $tokens[$assertCall['index']] = new Token([ + T_STRING, + 'assertIsNotReadable', + ]); + + break; + + case 'assertnotiswritable': + $tokens[$assertCall['index']] = new Token([ + T_STRING, + 'assertIsNotWritable', + ]); + + break; + + case 'assertdirectorynotexists': + $tokens[$assertCall['index']] = new Token([ + T_STRING, + 'assertDirectoryDoesNotExist', + ]); + + break; + + case 'assertfilenotexists': + $tokens[$assertCall['index']] = new Token([ + T_STRING, + 'assertFileDoesNotExist', + ]); + + break; + } } } From f84f505a847133d7cb6a8a6df26aa256cac03267 Mon Sep 17 00:00:00 2001 From: Krzysztof Ciszewski Date: Sat, 11 May 2024 09:55:44 +0200 Subject: [PATCH 7/8] php-cs-fixer#7968 remove switch statement --- .../PhpUnit/PhpUnitDedicateAssertFixer.php | 50 +++++++------------ 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php index a9d23458797..a968327464d 100644 --- a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php @@ -723,38 +723,24 @@ private function fixAssertNewNames(Tokens $tokens, array $assertCall): void return; } - switch ($assertCall['loweredName']) { - case 'assertnotisreadable': - $tokens[$assertCall['index']] = new Token([ - T_STRING, - 'assertIsNotReadable', - ]); - - break; - - case 'assertnotiswritable': - $tokens[$assertCall['index']] = new Token([ - T_STRING, - 'assertIsNotWritable', - ]); - - break; - - case 'assertdirectorynotexists': - $tokens[$assertCall['index']] = new Token([ - T_STRING, - 'assertDirectoryDoesNotExist', - ]); - - break; - - case 'assertfilenotexists': - $tokens[$assertCall['index']] = new Token([ - T_STRING, - 'assertFileDoesNotExist', - ]); - - break; + $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'; + } + + if (null === $replacement) { + return; } + + $tokens[$assertCall['index']] = new Token([ + T_STRING, + $replacement, + ]); } } From 60730e389e20a6e77ff603680ce2ef88d4d8b32f Mon Sep 17 00:00:00 2001 From: Krzysztof Ciszewski Date: Tue, 25 Jun 2024 21:45:40 +0200 Subject: [PATCH 8/8] refactored into a new rule --- doc/ruleSets/PHPUnit91MigrationRisky.rst | 5 +- doc/rules/index.rst | 3 + .../php_unit/php_unit_assert_new_names.rst | 52 ++++++ .../php_unit/php_unit_dedicate_assert.rst | 6 +- src/Fixer/AbstractPhpUnitFixer.php | 47 +++++ .../PhpUnit/PhpUnitAssertNewNamesFixer.php | 108 +++++++++++ .../PhpUnit/PhpUnitDedicateAssertFixer.php | 173 +----------------- .../Sets/PHPUnit91MigrationRiskySet.php | 4 +- .../PhpUnitAssertNewNamesFixerTest.php | 74 ++++++++ .../PhpUnitDedicateAssertFixerTest.php | 68 +------ 10 files changed, 294 insertions(+), 246 deletions(-) create mode 100644 doc/rules/php_unit/php_unit_assert_new_names.rst create mode 100644 src/Fixer/PhpUnit/PhpUnitAssertNewNamesFixer.php create mode 100644 tests/Fixer/PhpUnit/PhpUnitAssertNewNamesFixerTest.php diff --git a/doc/ruleSets/PHPUnit91MigrationRisky.rst b/doc/ruleSets/PHPUnit91MigrationRisky.rst index 3521d97b298..4a599ad0e81 100644 --- a/doc/ruleSets/PHPUnit91MigrationRisky.rst +++ b/doc/ruleSets/PHPUnit91MigrationRisky.rst @@ -16,7 +16,4 @@ Rules ----- - `@PHPUnit84Migration:risky <./PHPUnit84MigrationRisky.rst>`_ -- `php_unit_dedicate_assert <./../rules/php_unit/php_unit_dedicate_assert.rst>`_ with config: - - ``['target' => '9.1']`` - +- `php_unit_assert_new_names <./../rules/php_unit/php_unit_assert_new_names.rst>`_ diff --git a/doc/rules/index.rst b/doc/rules/index.rst index 231e0c478d4..c7343131f11 100644 --- a/doc/rules/index.rst +++ b/doc/rules/index.rst @@ -640,6 +640,9 @@ PHP Tag PHPUnit ------- +- `php_unit_assert_new_names <./php_unit/php_unit_assert_new_names.rst>`_ *(risky)* + + Rename deprecated PHPUnit assertions like ``assertFileNotExists`` to new methods like ``assertFileDoesNotExist``. - `php_unit_attributes <./php_unit/php_unit_attributes.rst>`_ PHPUnit attributes must be used over their respective PHPDoc-based annotations. diff --git a/doc/rules/php_unit/php_unit_assert_new_names.rst b/doc/rules/php_unit/php_unit_assert_new_names.rst new file mode 100644 index 00000000000..aa7085713ec --- /dev/null +++ b/doc/rules/php_unit/php_unit_assert_new_names.rst @@ -0,0 +1,52 @@ +================================== +Rule ``php_unit_assert_new_names`` +================================== + +Rename deprecated PHPUnit assertions like ``assertFileNotExists`` to new methods +like ``assertFileDoesNotExist``. + +Warning +------- + +Using this rule is risky +~~~~~~~~~~~~~~~~~~~~~~~~ + +Fixer could be risky if one is overriding PHPUnit's native methods. + +Examples +-------- + +Example #1 +~~~~~~~~~~ + +.. code-block:: diff + + --- Original + +++ New + assertFileNotExists("test.php"); + - $this->assertNotIsWritable("path.php"); + + $this->assertFileDoesNotExist("test.php"); + + $this->assertIsNotWritable("path.php"); + } + } + +Rule sets +--------- + +The rule is part of the following rule sets: + +- `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ +- `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ + +References +---------- + +- Fixer class: `PhpCsFixer\\Fixer\\PhpUnit\\PhpUnitAssertNewNamesFixer <./../../../src/Fixer/PhpUnit/PhpUnitAssertNewNamesFixer.php>`_ +- Test class: `PhpCsFixer\\Tests\\Fixer\\PhpUnit\\PhpUnitAssertNewNamesFixerTest <./../../../tests/Fixer/PhpUnit/PhpUnitAssertNewNamesFixerTest.php>`_ + +The test class defines officially supported behaviour. Each test case is a part of our backward compatibility promise. diff --git a/doc/rules/php_unit/php_unit_dedicate_assert.rst b/doc/rules/php_unit/php_unit_dedicate_assert.rst index 2aa03d4a2da..ee605bca2de 100644 --- a/doc/rules/php_unit/php_unit_dedicate_assert.rst +++ b/doc/rules/php_unit/php_unit_dedicate_assert.rst @@ -21,7 +21,7 @@ Configuration Target version of PHPUnit. -Allowed values: ``'3.0'``, ``'3.5'``, ``'5.0'``, ``'5.6'``, ``'9.1'`` and ``'newest'`` +Allowed values: ``'3.0'``, ``'3.5'``, ``'5.0'``, ``'5.6'`` and ``'newest'`` Default value: ``'newest'`` @@ -135,11 +135,11 @@ The rule is part of the following rule sets: - `@PHPUnit91Migration:risky <./../../ruleSets/PHPUnit91MigrationRisky.rst>`_ with config: - ``['target' => '9.1']`` + ``['target' => '5.6']`` - `@PHPUnit100Migration:risky <./../../ruleSets/PHPUnit100MigrationRisky.rst>`_ with config: - ``['target' => '9.1']`` + ``['target' => '5.6']`` References diff --git a/src/Fixer/AbstractPhpUnitFixer.php b/src/Fixer/AbstractPhpUnitFixer.php index 0b4c4b2f5c0..55a167e38e5 100644 --- a/src/Fixer/AbstractPhpUnitFixer.php +++ b/src/Fixer/AbstractPhpUnitFixer.php @@ -19,6 +19,7 @@ use PhpCsFixer\DocBlock\Line; use PhpCsFixer\Indicator\PhpUnitTestCaseIndicator; use PhpCsFixer\Tokenizer\Analyzer\AttributeAnalyzer; +use PhpCsFixer\Tokenizer\Analyzer\FunctionsAnalyzer; use PhpCsFixer\Tokenizer\Analyzer\NamespaceUsesAnalyzer; use PhpCsFixer\Tokenizer\Analyzer\WhitespacesAnalyzer; use PhpCsFixer\Tokenizer\CT; @@ -227,4 +228,50 @@ private function makeDocBlockMultiLineIfNeeded(DocBlock $doc, Tokens $tokens, in return $doc; } + + /** + * @return iterable + */ + protected function getPreviousAssertCall(Tokens $tokens, int $startIndex, int $endIndex): iterable + { + $functionsAnalyzer = new FunctionsAnalyzer(); + + for ($index = $endIndex; $index > $startIndex; --$index) { + $index = $tokens->getPrevTokenOfKind($index, [[T_STRING]]); + + if (null === $index) { + return; + } + + // test if "assert" something call + $loweredContent = strtolower($tokens[$index]->getContent()); + + if (! str_starts_with($loweredContent, 'assert')) { + continue; + } + + // test candidate for simple calls like: ([\]+'some fixable call'(...)) + $openBraceIndex = $tokens->getNextMeaningfulToken($index); + + if (! $tokens[$openBraceIndex]->equals('(')) { + continue; + } + + if (! $functionsAnalyzer->isTheSameClassCall($tokens, $index)) { + continue; + } + + yield [ + 'index' => $index, + 'loweredName' => $loweredContent, + 'openBraceIndex' => $openBraceIndex, + 'closeBraceIndex' => $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openBraceIndex), + ]; + } + } } diff --git a/src/Fixer/PhpUnit/PhpUnitAssertNewNamesFixer.php b/src/Fixer/PhpUnit/PhpUnitAssertNewNamesFixer.php new file mode 100644 index 00000000000..37a3af82add --- /dev/null +++ b/src/Fixer/PhpUnit/PhpUnitAssertNewNamesFixer.php @@ -0,0 +1,108 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Fixer\PhpUnit; + +use PhpCsFixer\Fixer\AbstractPhpUnitFixer; +use PhpCsFixer\FixerDefinition\CodeSample; +use PhpCsFixer\FixerDefinition\FixerDefinition; +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; +use PhpCsFixer\Tokenizer\Token; +use PhpCsFixer\Tokenizer\Tokens; + +/** + * @author Krzysztof Ciszewski + */ +final class PhpUnitAssertNewNamesFixer extends AbstractPhpUnitFixer +{ + + public function isRisky(): bool + { + return true; + } + + public function getDefinition(): FixerDefinitionInterface + { + return new FixerDefinition( + 'Rename deprecated PHPUnit assertions like `assertFileNotExists` to new methods like `assertFileDoesNotExist`.', + [ + new CodeSample( + 'assertFileNotExists("test.php"); + $this->assertNotIsWritable("path.php"); + } +} +' + ), + ], + null, + 'Fixer could be risky if one is overriding PHPUnit\'s native methods.' + ); + } + + /** + * {@inheritdoc} + * + * Must run after PhpUnitDedicateAssertFixer. + */ + public function getPriority(): int + { + return -10; + } + + protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $endIndex): void + { + foreach ($this->getPreviousAssertCall($tokens, $startIndex, $endIndex) as $assertCall) { + $this->fixAssertNewNames($tokens, $assertCall); + } + } + + /** + * @param array{ + * index: int, + * loweredName: string, + * openBraceIndex: int, + * closeBraceIndex: int, + * } $assertCall + */ + private function fixAssertNewNames(Tokens $tokens, array $assertCall): void + { + $replacements = [ + 'assertnotisreadable' => 'assertIsNotReadable', + 'assertnotiswritable' => 'assertIsNotWritable', + 'assertdirectorynotexists' => 'assertDirectoryDoesNotExist', + 'assertfilenotexists' => 'assertFileDoesNotExist', + 'assertdirectorynotisreadable' => 'assertDirectoryIsNotReadable', + 'assertdirectorynotiswritable' => 'assertDirectoryIsNotWriteable', + 'assertfilenotisreadable' => 'assertFileIsNotReadable', + 'assertfilenotiswritable' => 'assertFileIsNotWriteable', + 'assertregexp' => 'assertMatchesRegularExpression', + 'assertnotregexp' => 'assertDoesNotMatchRegularExpression', + ]; + $replacement = $replacements[$assertCall['loweredName']] ?? null; + + if ($replacement === null) { + return; + } + + $tokens[$assertCall['index']] = new Token([ + T_STRING, + $replacement, + ]); + } +} diff --git a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php index 6e4d18bc912..14fc827d683 100644 --- a/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php +++ b/src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php @@ -47,77 +47,7 @@ final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements C use ConfigurableFixerTrait; /** - * @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 - * } - * } + * @var array */ private static array $fixMap = [ 'array_key_exists' => [ @@ -189,7 +119,6 @@ final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements C 'argument_count' => 2, 'swap_arguments' => true, ], - 'assertNotIsReadable' => true, ]; /** @@ -304,15 +233,6 @@ protected function configurePostNormalisation(): void 'str_contains', ]); } - - if (PhpUnitTargetVersion::fulfills($this->configuration['target'], PhpUnitTargetVersion::VERSION_9_1)) { - $this->functions = array_merge($this->functions, [ - 'assertdirectorynotexists', - 'assertnotisreadable', - 'assertnotiswritable', - 'assertfilenotexists', - ]); - } } protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $endIndex): void @@ -335,10 +255,6 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en $this->fixAssertSameEquals($tokens, $assertCall); } } - - foreach ($this->getPreviousAssertCall($tokens, $startIndex, $endIndex) as $assertCall) { - $this->fixAssertNewNames($tokens, $assertCall); - } } protected function createConfigurationDefinition(): FixerConfigurationResolverInterface @@ -351,7 +267,6 @@ protected function createConfigurationDefinition(): FixerConfigurationResolverIn PhpUnitTargetVersion::VERSION_3_5, PhpUnitTargetVersion::VERSION_5_0, PhpUnitTargetVersion::VERSION_5_6, - PhpUnitTargetVersion::VERSION_9_1, PhpUnitTargetVersion::VERSION_NEWEST, ]) ->setDefault(PhpUnitTargetVersion::VERSION_NEWEST) @@ -416,12 +331,11 @@ private function fixAssertTrueFalse(Tokens $tokens, ArgumentsAnalyzer $arguments $isPositive = $isPositive ? 'positive' : 'negative'; - $replace = self::$fixMap[$content][$isPositive]; - if (false === $replace) { + if (false === self::$fixMap[$content][$isPositive]) { return; } - $tokens[$assertCall['index']] = new Token([T_STRING, (string) $replace]); + $tokens[$assertCall['index']] = new Token([T_STRING, self::$fixMap[$content][$isPositive]]); $this->removeFunctionCall($tokens, $testDefaultNamespaceTokenIndex, $testIndex, $testOpenIndex, $testCloseIndex); if (self::$fixMap[$content]['swap_arguments'] ?? false) { @@ -610,52 +524,6 @@ private function fixAssertSameEquals(Tokens $tokens, array $assertCall): void ]); } - /** - * @return iterable - */ - private function getPreviousAssertCall(Tokens $tokens, int $startIndex, int $endIndex): iterable - { - $functionsAnalyzer = new FunctionsAnalyzer(); - - for ($index = $endIndex; $index > $startIndex; --$index) { - $index = $tokens->getPrevTokenOfKind($index, [[T_STRING]]); - - if (null === $index) { - return; - } - - // test if "assert" something call - $loweredContent = strtolower($tokens[$index]->getContent()); - - if (!str_starts_with($loweredContent, 'assert')) { - continue; - } - - // test candidate for simple calls like: ([\]+'some fixable call'(...)) - $openBraceIndex = $tokens->getNextMeaningfulToken($index); - - if (!$tokens[$openBraceIndex]->equals('(')) { - continue; - } - - if (!$functionsAnalyzer->isTheSameClassCall($tokens, $index)) { - continue; - } - - yield [ - 'index' => $index, - 'loweredName' => $loweredContent, - 'openBraceIndex' => $openBraceIndex, - 'closeBraceIndex' => $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openBraceIndex), - ]; - } - } - private function removeFunctionCall(Tokens $tokens, ?int $callNSIndex, int $callIndex, int $openIndex, int $closeIndex): void { $tokens->clearTokenAndMergeSurroundingWhitespace($callIndex); @@ -719,39 +587,4 @@ private function cloneAndClearTokens(Tokens $tokens, int $start, int $end): arra 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'; - } - - if (null === $replacement) { - return; - } - - $tokens[$assertCall['index']] = new Token([ - T_STRING, - $replacement, - ]); - } } diff --git a/src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php b/src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php index ca6a9f0efb4..e8874e682b1 100644 --- a/src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php +++ b/src/RuleSet/Sets/PHPUnit91MigrationRiskySet.php @@ -26,9 +26,7 @@ public function getRules(): array { return [ '@PHPUnit84Migration:risky' => true, - 'php_unit_dedicate_assert' => [ - 'target' => PhpUnitTargetVersion::VERSION_9_1, - ], + 'php_unit_assert_new_names' => true ]; } } diff --git a/tests/Fixer/PhpUnit/PhpUnitAssertNewNamesFixerTest.php b/tests/Fixer/PhpUnit/PhpUnitAssertNewNamesFixerTest.php new file mode 100644 index 00000000000..231d53b8ecb --- /dev/null +++ b/tests/Fixer/PhpUnit/PhpUnitAssertNewNamesFixerTest.php @@ -0,0 +1,74 @@ + + * Dariusz Rumiński + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace PhpCsFixer\Tests\Fixer\PhpUnit; + +use PhpCsFixer\Tests\Test\AbstractFixerTestCase; + +/** + * @internal + * + * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitAssertNewNamesFixer + * + * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitAssertNewNamesFixer> + */ +final class PhpUnitAssertNewNamesFixerTest extends AbstractFixerTestCase +{ + /** + * @dataProvider provideFixCases + */ + public function testFix(string $expected, ?string $input = null): void + { + $this->doTest($expected, $input); + } + + public static function provideFixCases(): iterable + { + yield [ + self::generateTest( + ' + $this->assertFileDoesNotExist($a); + $this->assertIsNotReadable($a); + $this->assertIsNotWritable($a); + $this->assertDirectoryDoesNotExist($a); + $this->assertDirectoryIsNotReadable($a); + $this->assertDirectoryIsNotWriteable($a); + $this->assertFileIsNotReadable($a); + $this->assertFileIsNotWriteable($a); + $this->assertMatchesRegularExpression($a); + $this->assertDoesNotMatchRegularExpression($a); + ' + ), + self::generateTest( + ' + $this->assertFileNotExists($a); + $this->assertNotIsReadable($a); + $this->assertNotIsWritable($a); + $this->assertDirectoryNotExists($a); + $this->assertDirectoryNotIsReadable($a); + $this->assertDirectoryNotIsWritable($a); + $this->assertFileNotIsReadable($a); + $this->assertFileNotIsWritable($a); + $this->assertRegExp($a); + $this->assertNotRegExp($a); + ' + ), + ]; + } + + private static function generateTest(string $content): string + { + return "assertFileExists($a); - $this->assertFileDoesNotExist($a); + $this->assertFileNotExists($a); $this->assertFileExists($a); - $this->assertFileDoesNotExist($a); + $this->assertFileNotExists($a); '), self::generateTest(' $this->assertTrue(file_exists($a)); @@ -329,70 +329,6 @@ public static function provideFixCases(): iterable $this->assertTrue(!$y instanceof SomeClass, $message); '), ]; - - yield [ - self::generateTest( - ' - $this->assertFileDoesNotExist($a); - $this->assertFileDoesNotExist($a); - ' - ), - self::generateTest( - ' - $this->assertFalse(file_exists($a)); - $this->assertFalse(\file_exists($a)); - ' - ), - ['target' => PhpUnitTargetVersion::VERSION_9_1], - ]; - - yield [ - self::generateTest( - ' - $this->assertIsNotReadable($a); - $this->assertIsNotReadable($a); - ' - ), - self::generateTest( - ' - $this->assertFalse(is_readable($a)); - $this->assertFalse(\is_readable($a)); - ' - ), - ['target' => PhpUnitTargetVersion::VERSION_9_1], - ]; - - yield [ - self::generateTest( - ' - $this->assertIsNotWritable($a); - $this->assertIsNotWritable($a); - ' - ), - self::generateTest( - ' - $this->assertFalse(is_writable($a)); - $this->assertFalse(\is_writable($a)); - ' - ), - ['target' => PhpUnitTargetVersion::VERSION_9_1], - ]; - - yield [ - self::generateTest( - ' - $this->assertDirectoryDoesNotExist($a); - $this->assertDirectoryDoesNotExist($a); - ' - ), - self::generateTest( - ' - $this->assertFalse(is_dir($a)); - $this->assertFalse(\is_dir($a)); - ' - ), - ['target' => PhpUnitTargetVersion::VERSION_9_1], - ]; } /**