Skip to content

Commit

Permalink
#7968 use new naming for some asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysztof-ciszewski committed May 8, 2024
1 parent 61f5c8a commit 79316a6
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 75 deletions.
182 changes: 109 additions & 73 deletions src/Fixer/PhpUnit/PhpUnitDedicateAssertFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,83 +36,22 @@ final class PhpUnitDedicateAssertFixer extends AbstractPhpUnitFixer implements C
/**
* @var array<string, array<string, bool|int|string>|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<string>
*/
private array $functions = [];

public function __construct()
{
parent::__construct();

if (self::$fixMap === []) {
self::$fixMap = $this->getFixMap();
}
}

public function configure(array $configuration): void
{
parent::configure($configuration);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -243,8 +205,6 @@ protected function applyPhpUnitClassFix(Tokens $tokens, int $startIndex, int $en
|| 'assertnotequals' === $assertCall['loweredName']
) {
$this->fixAssertSameEquals($tokens, $assertCall);

continue;
}
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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,
],
];
}
}
1 change: 1 addition & 0 deletions src/Fixer/PhpUnit/PhpUnitTargetVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand Down
66 changes: 64 additions & 2 deletions tests/Fixer/PhpUnit/PhpUnitDedicateAssertFixerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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],
];
}

/**
Expand Down

0 comments on commit 79316a6

Please sign in to comment.