Skip to content

Commit

Permalink
DX: Enforce consistent naming in tests (PHP-CS-Fixer#7556)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubawerlos authored and danog committed Feb 2, 2024
1 parent 62f8dcb commit 8ab59fe
Show file tree
Hide file tree
Showing 10 changed files with 626 additions and 291 deletions.
2 changes: 1 addition & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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: 1102

-
message: '#Call to static method .+ with .+ will always evaluate to true.$#'
Expand Down
19 changes: 4 additions & 15 deletions tests/Fixer/Alias/NoAliasFunctionsFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@
final class NoAliasFunctionsFixerTest extends AbstractFixerTestCase
{
/**
* @param array<string, string[]> $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);
}

Expand Down Expand Up @@ -98,21 +101,7 @@ public function is_integer($a)
}
}',
];
}

/**
* @param array<string, string[]> $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' => [
'<?php
$a = rtrim($b);
Expand Down
74 changes: 54 additions & 20 deletions tests/Fixer/Alias/NoMixedEchoPrintFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,43 @@
final class NoMixedEchoPrintFixerTest extends AbstractFixerTestCase
{
/**
* @dataProvider provideFixEchoToPrintCases
* @param array<string, mixed> $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<array{string, null|string, array{use: string}}>
*/
public static function provideFixCases(): iterable
{
yield [
'<?php
print "test";
',
null,
['use' => 'print'],
];

yield [
'<?php
print ("test");
',
null,
['use' => 'print'],
];

yield [
'<?php
print("test");
',
null,
['use' => 'print'],
];

// `echo` can take multiple parameters (although such usage is rare) while `print` can take only one argument,
Expand All @@ -62,6 +73,8 @@ public static function provideFixEchoToPrintCases(): iterable
'<?php
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
',
null,
['use' => 'print'],
];

yield [
Expand All @@ -71,6 +84,7 @@ public static function provideFixEchoToPrintCases(): iterable
'<?php
echo "test";
',
['use' => 'print'],
];

yield [
Expand All @@ -80,6 +94,7 @@ public static function provideFixEchoToPrintCases(): iterable
'<?php
echo ("test");
',
['use' => 'print'],
];

yield [
Expand All @@ -89,6 +104,7 @@ public static function provideFixEchoToPrintCases(): iterable
'<?php
echo("test");
',
['use' => 'print'],
];

yield [
Expand All @@ -98,6 +114,7 @@ public static function provideFixEchoToPrintCases(): iterable
'<?php
echo foo(1, 2);
',
['use' => 'print'],
];

yield [
Expand All @@ -107,6 +124,7 @@ public static function provideFixEchoToPrintCases(): iterable
'<?php
echo ["foo", "bar", "baz"][$x];
',
['use' => 'print'],
];

yield [
Expand All @@ -116,11 +134,13 @@ public static function provideFixEchoToPrintCases(): iterable
'<?php
echo $foo ? "foo" : "bar";
',
['use' => 'print'],
];

yield [
"<?php print 'foo' ?>...<?php echo 'bar', 'baz' ?>",
"<?php echo 'foo' ?>...<?php echo 'bar', 'baz' ?>",
['use' => 'print'],
];

yield [
Expand All @@ -136,54 +156,54 @@ public static function provideFixEchoToPrintCases(): iterable
}
echo "bar";
',
['use' => 'print'],
];

yield [
'<?=$foo?>',
null,
['use' => 'print'],
];

foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) {
yield [
sprintf($codeSnippet, 'print'),
sprintf($codeSnippet, 'echo'),
['use' => 'print'],
];
}
}

/**
* @dataProvider provideFixPrintToEchoCases
*/
public function testFixPrintToEcho(string $expected, ?string $input = null): void
{
$this->fixer->configure(['use' => 'echo']);
$this->doTest($expected, $input);
}

public static function provideFixPrintToEchoCases(): iterable
{
yield [
'<?php
echo "test";
',
null,
['use' => 'echo'],
];

yield [
'<?php
echo ("test");
',
null,
['use' => 'echo'],
];

yield [
'<?php
echo("test");
',
null,
['use' => 'echo'],
];

// https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/issues/1502#issuecomment-156436229
yield [
'<?php
($some_var) ? print "true" : print "false";
',
null,
['use' => 'echo'],
];

// echo has no return value while print has a return value of 1 so it can be used in expressions.
Expand All @@ -192,12 +212,16 @@ public static function provideFixPrintToEchoCases(): iterable
'<?php
$ret = print "test";
',
null,
['use' => 'echo'],
];

yield [
'<?php
@print foo();
',
null,
['use' => 'echo'],
];

yield [
Expand All @@ -214,6 +238,8 @@ function testFunction() {
switch(print(\'a\')) {}
if (1 === print($a)) {}
',
null,
['use' => 'echo'],
];

yield [
Expand All @@ -225,6 +251,7 @@ function testFunction() {
some_function_call();
print "test";
',
['use' => 'echo'],
];

yield [
Expand All @@ -234,6 +261,7 @@ function testFunction() {
'<?php
print "test";
',
['use' => 'echo'],
];

yield [
Expand All @@ -243,6 +271,7 @@ function testFunction() {
'<?php
print ("test");
',
['use' => 'echo'],
];

yield [
Expand All @@ -252,6 +281,7 @@ function testFunction() {
'<?php
print("test");
',
['use' => 'echo'],
];

yield [
Expand All @@ -261,6 +291,7 @@ function testFunction() {
'<?php
print foo(1, 2);
',
['use' => 'echo'],
];

yield [
Expand All @@ -270,6 +301,7 @@ function testFunction() {
'<?php
print $foo ? "foo" : "bar";
',
['use' => 'echo'],
];

yield [
Expand All @@ -285,17 +317,19 @@ function testFunction() {
}
print "bar";
',
['use' => 'echo'],
];

foreach (self::getCodeSnippetsToConvertBothWays() as $codeSnippet) {
yield [
sprintf($codeSnippet, 'echo'),
sprintf($codeSnippet, 'print'),
['use' => 'echo'],
];
}
}

public function testDefaultConfig(): void
public function testConfigure(): void
{
$this->fixer->configure([]);

Expand All @@ -305,17 +339,17 @@ public function testDefaultConfig(): void
/**
* @param array<mixed> $wrongConfig
*
* @dataProvider provideWrongConfigCases
* @dataProvider provideInvalidConfigurationCases
*/
public function testWrongConfig(array $wrongConfig, string $expectedMessage): void
public function testInvalidConfiguration(array $wrongConfig, string $expectedMessage): void
{
$this->expectException(InvalidFixerConfigurationException::class);
$this->expectExceptionMessageMatches($expectedMessage);

$this->fixer->configure($wrongConfig);
}

public static function provideWrongConfigCases(): iterable
public static function provideInvalidConfigurationCases(): iterable
{
yield [
['a' => 'b'],
Expand Down
43 changes: 16 additions & 27 deletions tests/Fixer/Alias/PowToExponentiationFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,22 @@ public function &pow($a, $b);
'<?php echo 10_0** 2;',
'<?php echo pow(10_0, 2);',
];

yield [
'<?php pow(); ++$a;++$a;++$a;++$a;++$a;++$a;// pow(1,2);',
];

yield [
'<?php pow(5); ++$a;++$a;++$a;++$a;++$a;++$a;# pow(1,2);',
];

yield [
'<?php pow(5,1,1); ++$a;++$a;++$a;++$a;++$a;++$a;/* pow(1,2); */',
];

yield [
'<?php \a\pow(4,3); ++$a;++$a;++$a;++$a;++$a;++$a;/** pow(1,2); */',
];
}

/**
Expand All @@ -294,33 +310,6 @@ public static function provideFixPre80Cases(): iterable
];
}

/**
* @dataProvider provideNotFixCases
*/
public function testNotFix(string $expected): void
{
$this->doTest($expected);
}

public static function provideNotFixCases(): iterable
{
yield [
'<?php pow(); ++$a;++$a;++$a;++$a;++$a;++$a;// pow(1,2);',
];

yield [
'<?php pow(5); ++$a;++$a;++$a;++$a;++$a;++$a;# pow(1,2);',
];

yield [
'<?php pow(5,1,1); ++$a;++$a;++$a;++$a;++$a;++$a;/* pow(1,2); */',
];

yield [
'<?php \a\pow(4,3); ++$a;++$a;++$a;++$a;++$a;++$a;/** pow(1,2); */',
];
}

/**
* @requires PHP 8.0
*
Expand Down

0 comments on commit 8ab59fe

Please sign in to comment.