From e4ac64f17e081ebc109a33b3df786548f016af35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Wed, 13 Dec 2023 19:35:46 +0100 Subject: [PATCH 1/3] DX: unify tests --- phpstan.dist.neon | 2 +- .../Fixer/Alias/NoAliasFunctionsFixerTest.php | 19 +-- .../Fixer/Alias/NoMixedEchoPrintFixerTest.php | 129 ++++++++++-------- .../Alias/PowToExponentiationFixerTest.php | 43 +++--- .../Alias/RandomApiMigrationFixerTest.php | 24 +++- .../ArrayNotation/ArraySyntaxFixerTest.php | 54 +++++--- ...FunctionTypeDeclarationCasingFixerTest.php | 16 +++ tests/Test/AbstractFixerTestCase.php | 78 +++++++++++ 8 files changed, 237 insertions(+), 128 deletions(-) diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 15cd9324da3..4709294b6ee 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -21,7 +21,7 @@ parameters: - message: '#^Method PhpCsFixer\\Tests\\.+::provide.+Cases\(\) return type has no value type specified in iterable type iterable\.$#' path: tests - count: 1113 + count: 1112 - message: '#Call to static method .+ with .+ will always evaluate to true.$#' diff --git a/tests/Fixer/Alias/NoAliasFunctionsFixerTest.php b/tests/Fixer/Alias/NoAliasFunctionsFixerTest.php index 25729d53afb..a0255086a0e 100644 --- a/tests/Fixer/Alias/NoAliasFunctionsFixerTest.php +++ b/tests/Fixer/Alias/NoAliasFunctionsFixerTest.php @@ -27,10 +27,13 @@ final class NoAliasFunctionsFixerTest extends AbstractFixerTestCase { /** + * @param array $configuration + * * @dataProvider provideFixCases */ - public function testFix(string $expected, ?string $input = null): void + public function testFix(string $expected, ?string $input = null, array $configuration = []): void { + $this->fixer->configure($configuration); $this->doTest($expected, $input); } @@ -98,21 +101,7 @@ public function is_integer($a) } }', ]; - } - - /** - * @param array $configuration - * - * @dataProvider provideFixWithConfigurationCases - */ - public function testFixWithConfiguration(string $expected, ?string $input, array $configuration): void - { - $this->fixer->configure($configuration); - $this->doTest($expected, $input); - } - public static function provideFixWithConfigurationCases(): iterable - { yield '@internal' => [ ' $configuration + * + * @dataProvider provideFixCases */ - public function testFixEchoToPrint(string $expected, ?string $input = null): void + public function testFix(string $expected, ?string $input = null, array $configuration = []): void { - $this->fixer->configure(['use' => 'print']); + $this->fixer->configure($configuration); $this->doTest($expected, $input); } - public static function provideFixEchoToPrintCases(): iterable + /** + * @return iterable}> + */ + public static function provideFixCases(): iterable + { + foreach (self::provideFixEchoToPrintCases() as $case) { + yield [ + $case[0], + $case[1] ?? null, + ['use' => 'print'], + ]; + } + + foreach (self::provideFixPrintToEchoCases() as $case) { + yield [ + $case[0], + $case[1] ?? null, + ['use' => 'echo'], + ]; + } + } + + public function testConfigure(): void + { + $this->fixer->configure([]); + + self::assertCandidateTokenType(T_PRINT, $this->fixer); + } + + /** + * @param array $wrongConfig + * + * @dataProvider provideInvalidConfigurationCases + */ + public function testInvalidConfiguration(array $wrongConfig, string $expectedMessage): void + { + $this->expectException(InvalidFixerConfigurationException::class); + $this->expectExceptionMessageMatches($expectedMessage); + + $this->fixer->configure($wrongConfig); + } + + public static function provideInvalidConfigurationCases(): iterable + { + yield [ + ['a' => 'b'], + '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#', + ]; + + yield [ + ['a' => 'b', 'b' => 'c'], + '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#', + ]; + + yield [ + [1], + '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#', + ]; + + yield [ + ['use' => '_invalid_'], + '#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#', + ]; + } + + private static function provideFixEchoToPrintCases(): iterable { yield [ 'fixer->configure(['use' => 'echo']); - $this->doTest($expected, $input); - } - - public static function provideFixPrintToEchoCases(): iterable + private static function provideFixPrintToEchoCases(): iterable { yield [ 'fixer->configure([]); - - self::assertCandidateTokenType(T_PRINT, $this->fixer); - } - - /** - * @param array $wrongConfig - * - * @dataProvider provideWrongConfigCases - */ - public function testWrongConfig(array $wrongConfig, string $expectedMessage): void - { - $this->expectException(InvalidFixerConfigurationException::class); - $this->expectExceptionMessageMatches($expectedMessage); - - $this->fixer->configure($wrongConfig); - } - - public static function provideWrongConfigCases(): iterable - { - yield [ - ['a' => 'b'], - '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#', - ]; - - yield [ - ['a' => 'b', 'b' => 'c'], - '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#', - ]; - - yield [ - [1], - '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#', - ]; - - yield [ - ['use' => '_invalid_'], - '#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#', - ]; - } - private static function assertCandidateTokenType(int $expected, AbstractFixer $fixer): void { $reflectionProperty = new \ReflectionProperty($fixer, 'candidateTokenType'); diff --git a/tests/Fixer/Alias/PowToExponentiationFixerTest.php b/tests/Fixer/Alias/PowToExponentiationFixerTest.php index 2572fe7cda7..ed5be8eeda7 100644 --- a/tests/Fixer/Alias/PowToExponentiationFixerTest.php +++ b/tests/Fixer/Alias/PowToExponentiationFixerTest.php @@ -274,6 +274,22 @@ public function &pow($a, $b); 'doTest($expected); - } - - public static function provideNotFixCases(): iterable - { - yield [ - ' $configuration + * + * @dataProvider provideInvalidConfigurationCases + */ + public function testInvalidConfiguration(string $message, array $configuration): void { $this->expectException(InvalidFixerConfigurationException::class); - $this->expectExceptionMessageMatches('#^\[random_api_migration\] Invalid configuration: Function "is_null" is not handled by the fixer\.$#'); + $this->expectExceptionMessage($message); - $this->fixer->configure(['replacements' => ['is_null' => 'random_int']]); + $this->fixer->configure($configuration); } - public function testConfigureCheckReplacementType(): void + public static function provideInvalidConfigurationCases(): iterable { - $this->expectException(InvalidFixerConfigurationException::class); - $this->expectExceptionMessageMatches('#^\[random_api_migration\] Invalid configuration: Replacement for function "rand" must be a string, "null" given\.$#'); + yield 'not supported function' => [ + '[random_api_migration] Invalid configuration: Function "is_null" is not handled by the fixer.', + ['replacements' => ['is_null' => 'random_int']], + ]; - $this->fixer->configure(['replacements' => ['rand' => null]]); + yield 'wrong replacement' => [ + '[random_api_migration] Invalid configuration: Replacement for function "rand" must be a string, "null" given.', + ['replacements' => ['rand' => null]], + ]; } public function testConfigure(): void diff --git a/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php b/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php index 20eb5aef752..4b70339c8ac 100644 --- a/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php +++ b/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php @@ -35,25 +35,46 @@ public function testInvalidConfiguration(): void $this->fixer->configure(['a' => 1]); } - public function testFixWithDefaultConfiguration(): void + /** + * @param array $configuration + * + * @dataProvider provideFixCases + */ + public function testFix(string $expected, ?string $input = null, array $configuration = []): void { - $this->fixer->configure([]); - $this->doTest( - 'fixer->configure($configuration); + $this->doTest($expected, $input); } /** - * @dataProvider provideFixLongSyntaxCases + * @return iterable}> */ - public function testFixLongSyntax(string $expected, ?string $input = null): void + public static function provideFixCases(): iterable { - $this->fixer->configure(['syntax' => 'long']); - $this->doTest($expected, $input); + yield 'default configuration' => [ + ' 'long'], + ]; + } + + foreach (self::provideFixShortSyntaxCases() as $case) { + yield [ + $case[0], + $case[1] ?? null, + ['syntax' => 'short'], + ]; + } } - public static function provideFixLongSyntaxCases(): iterable + private static function provideFixLongSyntaxCases(): iterable { yield [' [$x, $y]) {}']; } - /** - * @dataProvider provideFixShortSyntaxCases - */ - public function testFixShortSyntax(string $expected, ?string $input = null): void - { - $this->fixer->configure(['syntax' => 'short']); - $this->doTest($expected, $input); - } - - public static function provideFixShortSyntaxCases(): iterable + private static function provideFixShortSyntaxCases(): iterable { yield ['getSuccessorsNames(), ); } + + /** + * @dataProvider provideFixCases + */ + public function testFix(string $expected, ?string $input = null): void + { + $this->doTest($expected, $input); + } + + /** + * @return iterable + */ + public static function provideFixCases(): iterable + { + yield from NativeTypeDeclarationCasingFixerTest::provideFixCases(); + } } diff --git a/tests/Test/AbstractFixerTestCase.php b/tests/Test/AbstractFixerTestCase.php index 72b9f41c89e..7cefa07fd26 100644 --- a/tests/Test/AbstractFixerTestCase.php +++ b/tests/Test/AbstractFixerTestCase.php @@ -371,6 +371,84 @@ final public function testFixerConfigurationDefinitions(): void } } + final public function testProperMethodNaming(): void + { + // THIS GROUP MAP WILL BE GONE AFTER ALL MIGRATION IS FINISHED + // + // group is the namespace part right before fixer's class name + // 0 - ignores + // 1 - check if expected methods exist + // 2 - check if an extra methods exist + $groupMap = [ + 'Alias' => 2, + 'ArrayNotation' => 2, + 'AttributeNotation' => 2, + 'Basic' => 1, + 'Casing' => 1, + 'CastNotation' => 0, + 'ClassNotation' => 0, + 'ClassUsage' => 0, + 'Comment' => 0, + 'ConstantNotation' => 0, + 'ControlStructure' => 0, + 'DoctrineAnnotation' => 0, + 'FunctionNotation' => 0, + 'Import' => 0, + 'LanguageConstruct' => 0, + 'ListNotation' => 0, + 'NamespaceNotation' => 0, + 'Naming' => 0, + 'Operator' => 0, + 'PhpTag' => 0, + 'PhpUnit' => 0, + 'Phpdoc' => 0, + 'ReturnNotation' => 0, + 'Semicolon' => 0, + 'Strict' => 0, + 'StringNotation' => 0, + 'Whitespace' => 0, + ]; + + $fixerGroup = $groupMap[explode('\\', static::class)[3]] ?? 0; + + if (0 === $fixerGroup) { + self::markTestSkipped('Not covered yet.'); + } + + self::assertTrue(method_exists($this, 'testFix'), 'Method testFix does not exist.'); + self::assertTrue(method_exists($this, 'provideFixCases'), 'Method provideFixCases does not exist.'); + + if (1 === $fixerGroup) { + return; // stop here + } + + $names = ['Fix', 'FixPre80', 'Fix80', 'Fix81', 'Fix82', 'Fix83', 'InvalidConfiguration']; + $methodNames = ['testConfigure']; + foreach ($names as $name) { + $methodNames[] = 'test'.$name; + $methodNames[] = 'provide'.$name.'Cases'; + } + + $class = new \ReflectionObject($this); + + $extraMethods = []; + foreach ($class->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { + if ($method->getDeclaringClass()->getName() !== $class->getName()) { + continue; + } + if (\in_array($method->getName(), $methodNames, true)) { + continue; + } + $extraMethods[] = $method->getName(); + } + + self::assertSame( + [], + $extraMethods, + sprintf('Methods "%s" should not be present.', implode('". "', $extraMethods)), + ); + } + protected function createFixer(): AbstractFixer { $fixerClassName = preg_replace('/^(PhpCsFixer)\\\\Tests(\\\\.+)Test$/', '$1$2', static::class); From 90a3f12833025bd9cb6ba88b9eaf2cbe0a427438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Wed, 13 Dec 2023 20:06:20 +0100 Subject: [PATCH 2/3] Update --- phpstan.dist.neon | 2 +- .../NoTrailingCommaInSinglelineFixerTest.php | 93 ++++++++++--------- .../Basic/NonPrintableCharacterFixerTest.php | 49 +++++----- tests/Test/AbstractFixerTestCase.php | 70 ++++++-------- 4 files changed, 104 insertions(+), 110 deletions(-) diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 4709294b6ee..8f49891df68 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -21,7 +21,7 @@ parameters: - message: '#^Method PhpCsFixer\\Tests\\.+::provide.+Cases\(\) return type has no value type specified in iterable type iterable\.$#' path: tests - count: 1112 + count: 1111 - message: '#Call to static method .+ with .+ will always evaluate to true.$#' diff --git a/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php b/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php index 8d6d8ee23a6..c171411866d 100644 --- a/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php +++ b/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php @@ -24,10 +24,13 @@ final class NoTrailingCommaInSinglelineFixerTest extends AbstractFixerTestCase { /** + * @param array $configuration + * * @dataProvider provideFixCases */ - public function testFix(string $expected, ?string $input = null): void + public function testFix(string $expected, ?string $input = null, array $configuration = []): void { + $this->fixer->configure($configuration); $this->doTest($expected, $input); } @@ -47,93 +50,95 @@ public static function provideFixCases(): iterable 'fixer->configure(['elements' => ['arguments']]); - - $this->doTest($expected, $input); - } - - public static function provideFixNoTrailingCommaInSinglelineFunctionCallCases(): iterable - { yield 'simple var' => [ ' ['arguments']], ]; yield '&' => [ ' ['arguments']], ]; yield 'open' => [ ' ['arguments']], ]; yield '=' => [ ' ['arguments']], ]; yield '.' => [ ' ['arguments']], ]; yield '(' => [ ' ['arguments']], ]; yield '\\' => [ ' ['arguments']], ]; yield 'A\\' => [ ' ['arguments']], ]; yield '\A\\' => [ ' ['arguments']], ]; yield ';' => [ ' ['arguments']], ]; yield '}' => [ ' ['arguments']], ]; yield 'test method call' => [ 'abc($a);', 'abc($a,);', + ['elements' => ['arguments']], ]; yield 'nested call' => [ 'abc($a,foo(1));', 'abc($a,foo(1,));', + ['elements' => ['arguments']], ]; yield 'wrapped' => [ 'getOutput(1);', 'getOutput(1,);', + ['elements' => ['arguments']], ]; yield 'dynamic function and method calls' => [ '$a(1); $c("");', '$a(1,); $c("",);', + ['elements' => ['arguments']], ]; yield 'static function call' => [ @@ -145,16 +150,19 @@ public static function provideFixNoTrailingCommaInSinglelineFunctionCallCases(): unset($foo->bar,); $b = isset($foo->bar,); ', + ['elements' => ['arguments']], ]; yield 'unset' => [ ' ['arguments']], ]; yield 'anonymous_class construction' => [ ' ['arguments']], ]; yield 'array/property access call' => [ @@ -194,6 +202,7 @@ public static function provideFixNoTrailingCommaInSinglelineFunctionCallCases(): $$e(2,); $f(0,)(1,); $g["e"](1,); // foo', + ['elements' => ['arguments']], ]; yield 'do not fix' => [ @@ -221,20 +230,38 @@ function foo1(string $param = null ): void { } ;', - ]; + null, + ['elements' => ['arguments']], + ]; + + foreach (self::provideFixNoTrailingCommaInSinglelineArrayFixerCases() as $case) { + yield [ + $case[0], + $case[1] ?? null, + ['elements' => ['array']], + ]; + } + + foreach (self::provideFixNoTrailingCommaInListCallFixerCases() as $case) { + yield [ + $case[0], + $case[1] ?? null, + ['elements' => ['array_destructuring']], + ]; + } } /** - * @dataProvider provideFix80NoTrailingCommaInSinglelineFunctionCallFixerCases + * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ - public function testFix80NoTrailingCommaInSinglelineFunctionCallFixer(string $expected, string $input = null): void + public function testFix80(string $expected, string $input = null): void { $this->doTest($expected, $input); } - public static function provideFix80NoTrailingCommaInSinglelineFunctionCallFixerCases(): iterable + public static function provideFix80Cases(): iterable { yield [ 'doTest($expected, $input); } - public static function provideFix81NoTrailingCommaInSinglelineFunctionCallFixerCases(): iterable + public static function provideFix81Cases(): iterable { yield [ 'method(1); strlen(...);', @@ -264,17 +291,7 @@ public static function provideFix81NoTrailingCommaInSinglelineFunctionCallFixerC ]; } - /** - * @dataProvider provideFixNoTrailingCommaInSinglelineArrayFixerCases - */ - public function testFixNoTrailingCommaInSinglelineArrayFixer(string $expected, ?string $input = null): void - { - $this->fixer->configure(['elements' => ['array']]); - - $this->doTest($expected, $input); - } - - public static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): iterable + private static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): iterable { yield ['fixer->configure(['elements' => ['array_destructuring']]); - - $this->doTest($expected, $input); - } - - public static function provideFixNoTrailingCommaInListCallFixerCases(): iterable + private static function provideFixNoTrailingCommaInListCallFixerCases(): iterable { yield [ ' $configuration + * * @dataProvider provideFixCases */ - public function testFix(string $expected, ?string $input = null): void + public function testFix(string $expected, ?string $input = null, array $configuration = []): void { - $this->fixer->configure([ - 'use_escape_sequences_in_strings' => false, - ]); - + $this->fixer->configure($configuration); $this->doTest($expected, $input); } /** - * @dataProvider provideFixCases + * @return iterable}> */ - public function testFixWithoutEscapeSequences(string $expected, ?string $input = null): void + public static function provideFixCases(): iterable { - $this->fixer->configure([ - 'use_escape_sequences_in_strings' => false, - ]); - - $this->doTest($expected, $input); + foreach (self::provideFixWithoutEscapeSequencesInStringsCases() as $case) { + yield [ + $case[0], + $case[1] ?? null, + ['use_escape_sequences_in_strings' => false], + ]; + } + + foreach (self::provideFixWithEscapeSequencesInStringsCases() as $case) { + yield [ + $case[0], + $case[1] ?? null, + ['use_escape_sequences_in_strings' => true], + ]; + } } - public static function provideFixCases(): iterable + private static function provideFixWithoutEscapeSequencesInStringsCases(): iterable { yield [ 'fixer->configure([ - 'use_escape_sequences_in_strings' => true, - ]); - - $this->doTest($expected, $input); - } - - public static function provideFixWithEscapeSequencesInStringsCases(): iterable + private static function provideFixWithEscapeSequencesInStringsCases(): iterable { yield [ ' 2, - 'ArrayNotation' => 2, - 'AttributeNotation' => 2, - 'Basic' => 1, - 'Casing' => 1, - 'CastNotation' => 0, - 'ClassNotation' => 0, - 'ClassUsage' => 0, - 'Comment' => 0, - 'ConstantNotation' => 0, - 'ControlStructure' => 0, - 'DoctrineAnnotation' => 0, - 'FunctionNotation' => 0, - 'Import' => 0, - 'LanguageConstruct' => 0, - 'ListNotation' => 0, - 'NamespaceNotation' => 0, - 'Naming' => 0, - 'Operator' => 0, - 'PhpTag' => 0, - 'PhpUnit' => 0, - 'Phpdoc' => 0, - 'ReturnNotation' => 0, - 'Semicolon' => 0, - 'Strict' => 0, - 'StringNotation' => 0, - 'Whitespace' => 0, + if ($this->fixer instanceof DeprecatedFixerInterface) { + self::markTestSkipped('Not worth refactoring tests for deprecated fixers.'); + } + + $exceptionGroup = [ + 'Casing', + 'CastNotation', + 'ClassNotation', + 'ClassUsage', + 'Comment', + 'ConstantNotation', + 'ControlStructure', + 'DoctrineAnnotation', + 'FunctionNotation', + 'Import', + 'LanguageConstruct', + 'ListNotation', + 'NamespaceNotation', + 'Naming', + 'Operator', + 'PhpTag', + 'PhpUnit', + 'Phpdoc', + 'ReturnNotation', + 'Semicolon', + 'Strict', + 'StringNotation', + 'Whitespace', ]; - $fixerGroup = $groupMap[explode('\\', static::class)[3]] ?? 0; + $fixerGroup = explode('\\', static::class)[3]; - if (0 === $fixerGroup) { + if (\in_array($fixerGroup, $exceptionGroup, true)) { self::markTestSkipped('Not covered yet.'); } self::assertTrue(method_exists($this, 'testFix'), 'Method testFix does not exist.'); self::assertTrue(method_exists($this, 'provideFixCases'), 'Method provideFixCases does not exist.'); - if (1 === $fixerGroup) { - return; // stop here - } - $names = ['Fix', 'FixPre80', 'Fix80', 'Fix81', 'Fix82', 'Fix83', 'InvalidConfiguration']; $methodNames = ['testConfigure']; foreach ($names as $name) { From 02395c48f43b7faaf669ff19968f4e36a435ca98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Wer=C5=82os?= Date: Wed, 13 Dec 2023 21:13:37 +0100 Subject: [PATCH 3/3] Update --- phpstan.dist.neon | 2 +- .../Fixer/Alias/NoMixedEchoPrintFixerTest.php | 151 +++++----- .../ArrayNotation/ArraySyntaxFixerTest.php | 257 +++++++++++++----- .../NoTrailingCommaInSinglelineFixerTest.php | 210 +++++++++----- .../Basic/NonPrintableCharacterFixerTest.php | 62 +++-- 5 files changed, 457 insertions(+), 225 deletions(-) diff --git a/phpstan.dist.neon b/phpstan.dist.neon index 8f49891df68..97e0dc6e7c8 100644 --- a/phpstan.dist.neon +++ b/phpstan.dist.neon @@ -21,7 +21,7 @@ parameters: - message: '#^Method PhpCsFixer\\Tests\\.+::provide.+Cases\(\) return type has no value type specified in iterable type iterable\.$#' path: tests - count: 1111 + count: 1102 - message: '#Call to static method .+ with .+ will always evaluate to true.$#' diff --git a/tests/Fixer/Alias/NoMixedEchoPrintFixerTest.php b/tests/Fixer/Alias/NoMixedEchoPrintFixerTest.php index 011a9fdcd2a..01f18e2e47a 100644 --- a/tests/Fixer/Alias/NoMixedEchoPrintFixerTest.php +++ b/tests/Fixer/Alias/NoMixedEchoPrintFixerTest.php @@ -39,88 +39,32 @@ public function testFix(string $expected, ?string $input = null, array $configur } /** - * @return iterable}> + * @return iterable */ public static function provideFixCases(): iterable - { - foreach (self::provideFixEchoToPrintCases() as $case) { - yield [ - $case[0], - $case[1] ?? null, - ['use' => 'print'], - ]; - } - - foreach (self::provideFixPrintToEchoCases() as $case) { - yield [ - $case[0], - $case[1] ?? null, - ['use' => 'echo'], - ]; - } - } - - public function testConfigure(): void - { - $this->fixer->configure([]); - - self::assertCandidateTokenType(T_PRINT, $this->fixer); - } - - /** - * @param array $wrongConfig - * - * @dataProvider provideInvalidConfigurationCases - */ - public function testInvalidConfiguration(array $wrongConfig, string $expectedMessage): void - { - $this->expectException(InvalidFixerConfigurationException::class); - $this->expectExceptionMessageMatches($expectedMessage); - - $this->fixer->configure($wrongConfig); - } - - public static function provideInvalidConfigurationCases(): iterable - { - yield [ - ['a' => 'b'], - '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#', - ]; - - yield [ - ['a' => 'b', 'b' => 'c'], - '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#', - ]; - - yield [ - [1], - '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#', - ]; - - yield [ - ['use' => '_invalid_'], - '#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#', - ]; - } - - private static function provideFixEchoToPrintCases(): iterable { yield [ ' 'print'], ]; yield [ ' 'print'], ]; yield [ ' 'print'], ]; // `echo` can take multiple parameters (although such usage is rare) while `print` can take only one argument, @@ -129,6 +73,8 @@ private static function provideFixEchoToPrintCases(): iterable ' 'print'], ]; yield [ @@ -138,6 +84,7 @@ private static function provideFixEchoToPrintCases(): iterable ' 'print'], ]; yield [ @@ -147,6 +94,7 @@ private static function provideFixEchoToPrintCases(): iterable ' 'print'], ]; yield [ @@ -156,6 +104,7 @@ private static function provideFixEchoToPrintCases(): iterable ' 'print'], ]; yield [ @@ -165,6 +114,7 @@ private static function provideFixEchoToPrintCases(): iterable ' 'print'], ]; yield [ @@ -174,6 +124,7 @@ private static function provideFixEchoToPrintCases(): iterable ' 'print'], ]; yield [ @@ -183,11 +134,13 @@ private static function provideFixEchoToPrintCases(): iterable ' 'print'], ]; yield [ "...", "...", + ['use' => 'print'], ]; yield [ @@ -203,38 +156,45 @@ private static function provideFixEchoToPrintCases(): iterable } echo "bar"; ', + ['use' => 'print'], ]; yield [ '', + null, + ['use' => 'print'], ]; foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) { yield [ sprintf($codeSnippet, 'print'), sprintf($codeSnippet, 'echo'), + ['use' => 'print'], ]; } - } - private static function provideFixPrintToEchoCases(): iterable - { yield [ ' 'echo'], ]; yield [ ' 'echo'], ]; yield [ ' 'echo'], ]; // https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/1502#issuecomment-156436229 @@ -242,6 +202,8 @@ private static function provideFixPrintToEchoCases(): iterable ' 'echo'], ]; // echo has no return value while print has a return value of 1 so it can be used in expressions. @@ -250,12 +212,16 @@ private static function provideFixPrintToEchoCases(): iterable ' 'echo'], ]; yield [ ' 'echo'], ]; yield [ @@ -272,6 +238,8 @@ function testFunction() { switch(print(\'a\')) {} if (1 === print($a)) {} ', + null, + ['use' => 'echo'], ]; yield [ @@ -283,6 +251,7 @@ function testFunction() { some_function_call(); print "test"; ', + ['use' => 'echo'], ]; yield [ @@ -292,6 +261,7 @@ function testFunction() { ' 'echo'], ]; yield [ @@ -301,6 +271,7 @@ function testFunction() { ' 'echo'], ]; yield [ @@ -310,6 +281,7 @@ function testFunction() { ' 'echo'], ]; yield [ @@ -319,6 +291,7 @@ function testFunction() { ' 'echo'], ]; yield [ @@ -328,6 +301,7 @@ function testFunction() { ' 'echo'], ]; yield [ @@ -343,16 +317,61 @@ function testFunction() { } print "bar"; ', + ['use' => 'echo'], ]; foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) { yield [ sprintf($codeSnippet, 'echo'), sprintf($codeSnippet, 'print'), + ['use' => 'echo'], ]; } } + public function testConfigure(): void + { + $this->fixer->configure([]); + + self::assertCandidateTokenType(T_PRINT, $this->fixer); + } + + /** + * @param array $wrongConfig + * + * @dataProvider provideInvalidConfigurationCases + */ + public function testInvalidConfiguration(array $wrongConfig, string $expectedMessage): void + { + $this->expectException(InvalidFixerConfigurationException::class); + $this->expectExceptionMessageMatches($expectedMessage); + + $this->fixer->configure($wrongConfig); + } + + public static function provideInvalidConfigurationCases(): iterable + { + yield [ + ['a' => 'b'], + '#^\[no_mixed_echo_print\] Invalid configuration: The option "a" does not exist\. (Known|Defined) options are: "use"\.$#', + ]; + + yield [ + ['a' => 'b', 'b' => 'c'], + '#^\[no_mixed_echo_print\] Invalid configuration: The options "a", "b" do not exist\. (Known|Defined) options are: "use"\.$#', + ]; + + yield [ + [1], + '#^\[no_mixed_echo_print\] Invalid configuration: The option "0" does not exist\. (Known|Defined) options are: "use"\.$#', + ]; + + yield [ + ['use' => '_invalid_'], + '#^\[no_mixed_echo_print\] Invalid configuration: The option "use" with value "_invalid_" is invalid\. Accepted values are: "print", "echo"\.$#', + ]; + } + private static function assertCandidateTokenType(int $expected, AbstractFixer $fixer): void { $reflectionProperty = new \ReflectionProperty($fixer, 'candidateTokenType'); diff --git a/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php b/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php index 4b70339c8ac..b9f1e24ae9f 100644 --- a/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php +++ b/tests/Fixer/ArrayNotation/ArraySyntaxFixerTest.php @@ -47,7 +47,7 @@ public function testFix(string $expected, ?string $input = null, array $configur } /** - * @return iterable}> + * @return iterable */ public static function provideFixCases(): iterable { @@ -57,104 +57,237 @@ public static function provideFixCases(): iterable [], ]; - foreach (self::provideFixLongSyntaxCases() as $case) { - yield [ - $case[0], - $case[1] ?? null, - ['syntax' => 'long'], - ]; - } - - foreach (self::provideFixShortSyntaxCases() as $case) { - yield [ - $case[0], - $case[1] ?? null, - ['syntax' => 'short'], - ]; - } - } - - private static function provideFixLongSyntaxCases(): iterable - { - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' true) : array("f" => false)));', ' true] : ["f" => false])];']; + yield [ + ' true) : array("f" => false)));', + ' true] : ["f" => false])];', + ['syntax' => 'long'], ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' 'long'], + ]; - yield [' [$x, $y]) {}']; - } + yield [ + ' [$x, $y]) {}', + null, + ['syntax' => 'long'], + ]; - private static function provideFixShortSyntaxCases(): iterable - { - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' true] : ["f" => false])];', ' true) : array("f" => false)));']; + yield [ + ' true] : ["f" => false])];', + ' true) : array("f" => false)));', + ['syntax' => 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; - yield [' 'short'], + ]; } } diff --git a/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php b/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php index c171411866d..4155d4d59fe 100644 --- a/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php +++ b/tests/Fixer/Basic/NoTrailingCommaInSinglelineFixerTest.php @@ -34,6 +34,9 @@ public function testFix(string $expected, ?string $input = null, array $configur $this->doTest($expected, $input); } + /** + * @return iterable}}> + */ public static function provideFixCases(): iterable { yield [ @@ -234,81 +237,47 @@ function foo1(string $param = null ): void ['elements' => ['arguments']], ]; - foreach (self::provideFixNoTrailingCommaInSinglelineArrayFixerCases() as $case) { - yield [ - $case[0], - $case[1] ?? null, - ['elements' => ['array']], - ]; - } - - foreach (self::provideFixNoTrailingCommaInListCallFixerCases() as $case) { - yield [ - $case[0], - $case[1] ?? null, - ['elements' => ['array_destructuring']], - ]; - } - } - - /** - * @dataProvider provideFix80Cases - * - * @requires PHP 8.0 - */ - public function testFix80(string $expected, string $input = null): void - { - $this->doTest($expected, $input); - } - - public static function provideFix80Cases(): iterable - { yield [ - ' ['array']], ]; - } - - /** - * @dataProvider provideFix81Cases - * - * @requires PHP 8.1 - */ - public function testFix81(string $expected, ?string $input = null): void - { - $this->doTest($expected, $input); - } - public static function provideFix81Cases(): iterable - { yield [ - 'method(1); strlen(...);', - 'method(1,); strlen(...);', + ' ['array']], ]; - } - - private static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): iterable - { - yield [' ['array']], ]; - yield [" ['array']], + ]; - yield [" ['array']], + ]; - yield [" ['array']], + ]; - yield [" ['array']], + ]; yield [ ' ['array']], ]; yield [ @@ -325,6 +296,8 @@ private static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): foo TWIG , $twig, );', + null, + ['elements' => ['array']], ]; yield [ @@ -333,6 +306,8 @@ private static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): foo TWIG , $twig, );', + null, + ['elements' => ['array']], ]; yield [ @@ -342,26 +317,64 @@ private static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): foo TWIG , $twig, );', + null, + ['elements' => ['array']], ]; // Short syntax - yield [' ['array']], + ]; - yield [' ['array']], + ]; - yield [' ['array']], + ]; - yield [' ['array']], + ]; - yield [" ['array']], + ]; - yield [" ['array']], + ]; - yield [' ['array']], + ]; - yield [' ['array']], + ]; - yield [' ['array']], + ]; yield [ ' ['array']], ]; yield [ @@ -378,6 +393,8 @@ private static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): foo TWIG , $twig, ];', + null, + ['elements' => ['array']], ]; yield [ @@ -386,6 +403,8 @@ private static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): foo TWIG , $twig, ];', + null, + ['elements' => ['array']], ]; yield [ @@ -395,21 +414,22 @@ private static function provideFixNoTrailingCommaInSinglelineArrayFixerCases(): foo TWIG , $twig, ];', + null, + ['elements' => ['array']], ]; yield [ ' ['array']], ]; yield [ ' ['array']], ]; - } - private static function provideFixNoTrailingCommaInListCallFixerCases(): iterable - { yield [ ' ['array_destructuring']], ]; yield [ @@ -436,6 +457,8 @@ private static function provideFixNoTrailingCommaInListCallFixerCases(): iterabl ,# # ) = $a;', + null, + ['elements' => ['array_destructuring']], ]; yield [ @@ -455,6 +478,47 @@ private static function provideFixNoTrailingCommaInListCallFixerCases(): iterabl [$a11 , $b , ] = foo(); [$a12, /* $b */, $c, ] = foo(); ', + ['elements' => ['array_destructuring']], + ]; + } + + /** + * @dataProvider provideFix80Cases + * + * @requires PHP 8.0 + */ + public function testFix80(string $expected, string $input = null): void + { + $this->doTest($expected, $input); + } + + public static function provideFix80Cases(): iterable + { + yield [ + 'doTest($expected, $input); + } + + public static function provideFix81Cases(): iterable + { + yield [ + 'method(1); strlen(...);', + 'method(1,); strlen(...);', ]; } } diff --git a/tests/Fixer/Basic/NonPrintableCharacterFixerTest.php b/tests/Fixer/Basic/NonPrintableCharacterFixerTest.php index 29301467dd5..b5cf3fe880e 100644 --- a/tests/Fixer/Basic/NonPrintableCharacterFixerTest.php +++ b/tests/Fixer/Basic/NonPrintableCharacterFixerTest.php @@ -37,32 +37,14 @@ public function testFix(string $expected, ?string $input = null, array $configur } /** - * @return iterable}> + * @return iterable */ public static function provideFixCases(): iterable - { - foreach (self::provideFixWithoutEscapeSequencesInStringsCases() as $case) { - yield [ - $case[0], - $case[1] ?? null, - ['use_escape_sequences_in_strings' => false], - ]; - } - - foreach (self::provideFixWithEscapeSequencesInStringsCases() as $case) { - yield [ - $case[0], - $case[1] ?? null, - ['use_escape_sequences_in_strings' => true], - ]; - } - } - - private static function provideFixWithoutEscapeSequencesInStringsCases(): iterable { yield [ ' false], ]; yield [ @@ -75,6 +57,7 @@ private static function provideFixWithoutEscapeSequencesInStringsCases(): iterab pack('H*', 'e2808b'). pack('H*', 'e2808b'). 'Hello World !";', + ['use_escape_sequences_in_strings' => false], ]; yield [ @@ -84,6 +67,7 @@ private static function provideFixWithoutEscapeSequencesInStringsCases(): iterab ' false], ]; yield [ @@ -105,42 +89,51 @@ function f(string $p) { echo $p; }', + ['use_escape_sequences_in_strings' => false], ]; yield [ ' false], ]; yield [ 'abc', 'a'.pack('H*', 'e2808b').'bc', + ['use_escape_sequences_in_strings' => false], ]; yield [ ' false], ]; yield [ ' false], ]; yield [ ' false], ]; yield [ ' false], ]; yield [ ' false], ]; - } - private static function provideFixWithEscapeSequencesInStringsCases(): iterable - { yield [ ' true], ]; yield [ ' true], ]; yield [ ' true], ]; yield [ ' true], ]; yield [ ' true], ]; yield [ @@ -186,6 +185,8 @@ function f(string $p) FooBar\ TXT; ', + null, + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -197,6 +198,7 @@ function f(string $p) Foo'.pack('H*', 'e2808b').'Bar TXT; ', + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -208,6 +210,7 @@ function f(string $p) Foo'.pack('H*', 'e2808b').'Bar TXT; ', + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -219,10 +222,13 @@ function f(string $p) Foo'.pack('H*', 'e2808b').' Bar \n \ $variableToEscape TXT; ', + ['use_escape_sequences_in_strings' => true], ]; yield [ ' true], ]; yield [ @@ -237,6 +243,7 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -251,6 +258,7 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -271,6 +279,7 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -285,6 +294,7 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -299,6 +309,7 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -313,6 +324,7 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -327,6 +339,7 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -341,6 +354,7 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ @@ -355,11 +369,13 @@ function f(string $p) , pack('H*', 'e2808b') ), + ['use_escape_sequences_in_strings' => true], ]; yield [ " true], ]; } }