123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * 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\ConfigurationException\InvalidFixerConfigurationException;
- use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitDedicateAssertFixer
- */
- final class PhpUnitDedicateAssertFixerTest extends AbstractFixerTestCase
- {
- /**
- * @param _AutogeneratedInputConfiguration $config
- *
- * @dataProvider provideFixCases
- */
- public function testFix(string $expected, ?string $input = null, array $config = []): void
- {
- $this->fixer->configure($config);
- $this->doTest($expected, $input);
- }
- public static function provideFixCases(): iterable
- {
- yield [
- self::generateTest('
- $this->assertNan($a);
- $this->assertNan($a);
- $this->assertTrue(test\is_nan($a));
- $this->assertTrue(test\a\is_nan($a));
- '),
- self::generateTest('
- $this->assertTrue(is_nan($a));
- $this->assertTrue(\is_nan($a));
- $this->assertTrue(test\is_nan($a));
- $this->assertTrue(test\a\is_nan($a));
- '),
- ];
- yield [
- self::generateTest('
- $this->assertFileExists($a);
- $this->assertFileNotExists($a);
- $this->assertFileExists($a);
- $this->assertFileNotExists($a);
- '),
- self::generateTest('
- $this->assertTrue(file_exists($a));
- $this->assertFalse(file_exists($a));
- $this->assertTrue(\file_exists($a));
- $this->assertFalse(\file_exists($a));
- '),
- ];
- yield [
- self::generateTest('
- $this->assertNull($a);
- $this->assertNotNull($a);
- $this->assertNull($a);
- $this->assertNotNull($a, "my message");
- '),
- self::generateTest('
- $this->assertTrue(is_null($a));
- $this->assertFalse(is_null($a));
- $this->assertTrue(\is_null($a));
- $this->assertFalse(\is_null($a), "my message");
- '),
- ];
- yield [
- self::generateTest('
- $this->assertEmpty($a);
- $this->assertNotEmpty($a);
- '),
- self::generateTest('
- $this->assertTrue(empty($a));
- $this->ASSERTFALSE(empty($a));
- '),
- ];
- yield [
- self::generateTest('
- $this->assertInfinite($a);
- $this->assertFinite($a, "my message");
- $this->assertInfinite($a);
- $this->assertFinite($a, b"my message");
- '),
- self::generateTest('
- $this->assertTrue(is_infinite($a));
- $this->assertFalse(is_infinite($a), "my message");
- $this->assertTrue(\is_infinite($a));
- $this->assertFalse(\is_infinite($a), b"my message");
- '),
- ];
- yield [
- self::generateTest('
- $this->assertArrayHasKey("test", $a);
- $this->assertArrayNotHasKey($b, $a, $c);
- '),
- self::generateTest('
- $this->assertTrue(\array_key_exists("test", $a));
- $this->ASSERTFALSE(array_key_exists($b, $a), $c);
- '),
- ];
- yield [
- self::generateTest('
- $this->assertTrue(is_dir($a));
- $this->assertTrue(is_writable($a));
- $this->assertTrue(is_readable($a));
- '),
- null,
- ['target' => PhpUnitTargetVersion::VERSION_5_0],
- ];
- yield [
- self::generateTest('
- $this->assertTrue(is_dir($a));
- $this->assertTrue(is_writable($a));
- $this->assertTrue(is_readable($a));
- '),
- null,
- ['target' => PhpUnitTargetVersion::VERSION_3_0],
- ];
- yield [
- self::generateTest('
- $this->assertDirectoryNotExists($a);
- $this->assertNotIsWritable($a);
- $this->assertNotIsReadable($a);
- '),
- self::generateTest('
- $this->assertFalse(is_dir($a));
- $this->assertFalse(is_writable($a));
- $this->assertFalse(is_readable($a));
- '),
- ['target' => PhpUnitTargetVersion::VERSION_5_6],
- ];
- yield [
- self::generateTest('
- $this->assertDirectoryExists($a);
- $this->assertIsWritable($a);
- $this->assertIsReadable($a);
- '),
- self::generateTest('
- $this->assertTrue(is_dir($a));
- $this->assertTrue(is_writable($a));
- $this->assertTrue(is_readable($a));
- '),
- ['target' => PhpUnitTargetVersion::VERSION_NEWEST],
- ];
- foreach (['array', 'bool', 'callable', 'double', 'float', 'int', 'integer', 'long', 'numeric', 'object', 'real', 'scalar', 'string'] as $type) {
- yield [
- self::generateTest(\sprintf('$this->assertInternalType(\'%s\', $a);', $type)),
- self::generateTest(\sprintf('$this->assertTrue(is_%s($a));', $type)),
- ];
- yield [
- self::generateTest(\sprintf('$this->assertNotInternalType(\'%s\', $a);', $type)),
- self::generateTest(\sprintf('$this->assertFalse(is_%s($a));', $type)),
- ];
- }
- yield [
- self::generateTest('$this->assertInternalType(\'float\', $a, "my message");'),
- self::generateTest('$this->assertTrue(is_float( $a), "my message");'),
- ];
- yield [
- self::generateTest('$this->assertInternalType(\'float\', $a);'),
- self::generateTest('$this->assertTrue(\IS_FLOAT($a));'),
- ];
- yield [
- self::generateTest('$this->assertInternalType(#
- \'float\'#
- , #
- $a#
- #
- )#
- ;'),
- self::generateTest('$this->assertTrue(#
- \IS_FLOAT#
- (#
- $a#
- )#
- )#
- ;'),
- ];
- yield [
- self::generateTest('static::assertInternalType(\'float\', $a);'),
- self::generateTest('static::assertTrue(is_float( $a));'),
- ];
- yield [
- self::generateTest('self::assertInternalType(\'float\', $a);'),
- self::generateTest('self::assertTrue(is_float( $a));'),
- ];
- yield [
- self::generateTest('static::assertNull($a);'),
- self::generateTest('static::assertTrue(is_null($a));'),
- ];
- yield [
- self::generateTest('self::assertNull($a);'),
- self::generateTest('self::assertTrue(is_null($a));'),
- ];
- yield [
- self::generateTest('SELF::assertNull($a);'),
- self::generateTest('SELF::assertTrue(is_null($a));'),
- ];
- yield [
- self::generateTest('self::assertStringContainsString($needle, $haystack);'),
- self::generateTest('self::assertTrue(str_contains($haystack, $needle));'),
- ['target' => PhpUnitTargetVersion::VERSION_NEWEST],
- ];
- yield [
- self::generateTest('self::assertStringNotContainsString($needle, $a[$haystack.""](123)[foo()]);'),
- self::generateTest('self::assertFalse(str_contains($a[$haystack.""](123)[foo()], $needle));'),
- ['target' => PhpUnitTargetVersion::VERSION_NEWEST],
- ];
- yield [
- self::generateTest('self::assertStringStartsWith($needle, $haystack);'),
- self::generateTest('self::assertTrue(str_starts_with($haystack, $needle));'),
- ];
- yield [
- self::generateTest('self::assertStringStartsNotWith($needle, $haystack);'),
- self::generateTest('self::assertFalse(str_starts_with($haystack, $needle));'),
- ];
- yield [
- self::generateTest('self::assertStringStartsNotWith( #3
- $needle#4
- , #1
- $haystack#2
- );'),
- self::generateTest('self::assertFalse(str_starts_with( #1
- $haystack#2
- ,#3
- $needle#4
- ));'),
- ];
- yield [
- self::generateTest('self::assertStringEndsWith($needle, $haystack);'),
- self::generateTest('self::assertTrue(str_ends_with($haystack, $needle));'),
- ];
- yield [
- self::generateTest('self::assertStringEndsNotWith($needle, $haystack);'),
- self::generateTest('self::assertFalse(str_ends_with($haystack, $needle));'),
- ];
- yield '$a instanceof class' => [
- self::generateTest('
- $this->assertInstanceOf(SomeClass::class, $x);
- $this->assertInstanceOf(SomeClass::class, $y, $message);
- '),
- self::generateTest('
- $this->assertTrue($x instanceof SomeClass);
- $this->assertTrue($y instanceof SomeClass, $message);
- '),
- ];
- yield '$a instanceof class\a\b' => [
- self::generateTest('
- $this->assertInstanceOf(\PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest\SimpleFixer::class, $ii);
- '),
- self::generateTest('
- $this->assertTrue($ii instanceof \PhpCsFixer\Tests\Fixtures\Test\AbstractFixerTest\SimpleFixer);
- '),
- ];
- yield '$a instanceof $b' => [
- self::generateTest('
- $this->assertInstanceOf($tty, $abc/* 1 *//* 2 */);
- $this->assertInstanceOf($oo, $def, $message);
- '),
- self::generateTest('
- $this->assertTrue($abc instanceof /* 1 */$tty /* 2 */);
- $this->assertTrue($def instanceof $oo, $message);
- '),
- ];
- yield 'do not fix instance of' => [
- self::generateTest('
- $this->assertTrue($gg instanceof $ijl . "X", $something);
- $this->assertTrue($ff instanceof $klh . $b(1,2,$message), $noMsg);
- '),
- ];
- yield '!$a instanceof class' => [
- self::generateTest('
- $this->assertNotInstanceOf(SomeClass::class, $x);
- $this->assertNotInstanceOf(SomeClass::class, $y, $message);
- '),
- self::generateTest('
- $this->assertTrue(!$x instanceof SomeClass);
- $this->assertTrue(!$y instanceof SomeClass, $message);
- '),
- ];
- }
- /**
- * @dataProvider provideNotFixCases
- */
- public function testNotFix(string $expected): void
- {
- $this->fixer->configure(['target' => PhpUnitTargetVersion::VERSION_NEWEST]);
- $this->doTest($expected);
- }
- /**
- * @return iterable<int|string, array{string}>
- */
- public static function provideNotFixCases(): iterable
- {
- yield 'not a method call' => [
- self::generateTest('echo $this->assertTrue;'),
- ];
- yield 'wrong argument count 1' => [
- self::generateTest('static::assertTrue(is_null($a, $b));'),
- ];
- yield 'wrong argument count 2' => [
- self::generateTest('static::assertTrue(is_int($a, $b));'),
- ];
- yield [
- self::generateTest('
- $this->assertTrue(is_null);
- $this->assertTrue(is_int($a) && $b);
- $this->assertFalse(is_nan($a));
- $this->assertTrue(is_int($a) || \is_bool($b));
- $this->assertTrue($a&&is_int($a));
- static::assertTrue(is_null);
- self::assertTrue(is_null);
- '),
- ];
- yield 'not in class' => [
- '<?php self::assertTrue(is_null($a));',
- ];
- // Do not replace is_resource() by assertIsResource().
- // is_resource() also checks if the resource is open or closed,
- // while assertIsResource() does not.
- yield 'Do not replace is_resource' => [
- self::generateTest('self::assertTrue(is_resource($resource));'),
- ];
- }
- public function testInvalidConfig(): void
- {
- $this->expectException(InvalidFixerConfigurationException::class);
- $this->expectExceptionMessageMatches('/^\[php_unit_dedicate_assert\] Invalid configuration: The option "target" .*\.$/');
- $this->fixer->configure(['target' => '_unknown_']); // @phpstan-ignore-line
- }
- /**
- * @dataProvider provideTestAssertCountCases
- */
- public function testAssertCount(string $expected, ?string $input = null): void
- {
- if (null === $input) {
- $expected = \sprintf($expected, 'count');
- } else {
- $input = \sprintf($input, 'count');
- }
- $this->doTest($expected, $input);
- }
- /**
- * @dataProvider provideTestAssertCountCases
- */
- public function testAssertCountFromSizeOf(string $expected, ?string $input = null): void
- {
- if (null === $input) {
- $expected = \sprintf($expected, 'sizeof');
- } else {
- $input = \sprintf($input, 'sizeof');
- }
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideTestAssertCountCases(): iterable
- {
- // positive fixing
- yield 'assert same' => [
- self::generateTest('$this->assertCount(1, $a);'),
- self::generateTest('$this->assertSame(1, %s($a));'),
- ];
- yield 'assert equals' => [
- self::generateTest('$this->assertCount(2, $b);'),
- self::generateTest('$this->assertEquals(2, %s($b));'),
- ];
- // negative fixing
- yield 'assert not same' => [
- self::generateTest('$this->assertNotCount(11, $c);'),
- self::generateTest('$this->assertNotSame(11, %s($c));'),
- ];
- yield 'assert not equals' => [
- self::generateTest('$this->assertNotCount(122, $d);'),
- self::generateTest('$this->assertNotEquals(122, %s($d));'),
- ];
- // other cases
- yield 'assert same with namespace' => [
- self::generateTest('$this->assertCount(1, $a);'),
- self::generateTest('$this->assertSame(1, \%s($a));'),
- ];
- yield 'no spacing' => [
- self::generateTest('$this->assertCount(1,$a);'),
- self::generateTest('$this->assertSame(1,%s($a));'),
- ];
- yield 'lot of spacing' => [
- self::generateTest('$this->assertCount(
- 1
- ,
- '.'
- '.'
- $a
- '.'
- )
- ;'),
- self::generateTest('$this->assertSame(
- 1
- ,
- %s
- (
- $a
- )
- )
- ;'),
- ];
- yield 'lot of fix cases' => [
- self::generateTest('
- $this->assertCount(1, $a);
- $this->assertCount(2, $a);
- $this->assertCount(3, $a);
- $this->assertNotCount(4, $a);
- $this->assertCount(5, $a, "abc");
- $this->assertCount(6, $a, "def");
- '),
- self::generateTest('
- $this->assertSame(1, %1$s($a));
- $this->assertSame(2, %1$s($a));
- $this->assertEquals(3, %1$s($a));
- $this->assertNotSame(4, %1$s($a));
- $this->assertEquals(5, %1$s($a), "abc");
- $this->assertSame(6, \%1$s($a), "def");
- '),
- ];
- yield 'comment handling' => [
- self::generateTest('$this->assertCount(# 0
- 1# 1
- ,# 2
- # 3
- # 4
- $a# 5
- # 6
- )# 7
- ;# 8'),
- self::generateTest('$this->assertSame(# 0
- 1# 1
- ,# 2
- %s# 3
- (# 4
- $a# 5
- )# 6
- )# 7
- ;# 8'),
- ];
- yield [
- self::generateTest('$this->assertCount($b, $a);'),
- self::generateTest('$this->assertSame($b, %s($a));'),
- ];
- yield 'do not fix 1' => [
- self::generateTest('$this->assertSame($b[1], %s($a));'),
- ];
- yield 'do not fix 2' => [
- self::generateTest('$this->assertSame(b(), %s($a));'),
- ];
- yield 'do not fix 3' => [
- self::generateTest('$this->assertSame(1.0, %s($a));'),
- ];
- yield 'do not fix 4' => [
- self::generateTest('$this->assertSame(1);'),
- ];
- yield 'do not fix 5' => [
- self::generateTest('$this->assertSame(1, "%s");'),
- ];
- yield 'do not fix 6' => [
- self::generateTest('$this->test(); // $this->assertSame($b, %s($a));'),
- ];
- yield 'do not fix 7' => [
- self::generateTest('$this->assertSame(2, count($array) - 1);'),
- ];
- yield 'do not fix 8' => [
- self::generateTest('
- Foo::assertSame(1, sizeof($a));
- $this->assertSame(1, sizeof2(2));
- $this->assertSame(1, sizeof::foo);
- '),
- ];
- }
- /**
- * @dataProvider provideTestAssertCountCasingCases
- */
- public function testAssertCountCasing(string $expected, string $input): void
- {
- $expected = \sprintf($expected, 'count');
- $input = \sprintf($input, 'COUNT');
- $this->doTest($expected, $input);
- }
- /**
- * @dataProvider provideTestAssertCountCasingCases
- */
- public function testAssertCountFromSizeOfCasing(string $expected, string $input): void
- {
- $expected = \sprintf($expected, 'sizeof');
- $input = \sprintf($input, 'SIZEOF');
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideTestAssertCountCasingCases(): iterable
- {
- yield [
- self::generateTest('$this->assertCount(1, $a);'),
- self::generateTest('$this->assertSame(1, %s($a));'),
- ];
- yield [
- self::generateTest('$this->assertCount(1, $a);'),
- self::generateTest('$this->assertSame(1, \%s($a));'),
- ];
- }
- /**
- * @dataProvider provideFix73Cases
- */
- public function testFix73(string $expected, string $input): void
- {
- $this->fixer->configure(['target' => PhpUnitTargetVersion::VERSION_NEWEST]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{string, string}>
- */
- public static function provideFix73Cases(): iterable
- {
- yield [
- self::generateTest('$this->assertNan($a, );'),
- self::generateTest('$this->assertTrue(is_nan($a), );'),
- ];
- yield [
- self::generateTest('$this->assertNan($a);'),
- self::generateTest('$this->assertTrue(is_nan($a, ));'),
- ];
- yield [
- self::generateTest('$this->assertNan($a, );'),
- self::generateTest('$this->assertTrue(is_nan($a, ), );'),
- ];
- yield [
- self::generateTest('$this->assertInternalType(\'array\', $a,);'),
- self::generateTest('$this->assertTrue(is_array($a,),);'),
- ];
- yield [
- self::generateTest('$this->assertNan($b);'),
- self::generateTest('$this->assertTrue(\is_nan($b,));'),
- ];
- yield [
- self::generateTest('$this->assertFileExists($f, \'message\',);'),
- self::generateTest('$this->assertTrue(file_exists($f,), \'message\',);'),
- ];
- yield [
- self::generateTest('$this->assertNan($y , );'),
- self::generateTest('$this->assertTrue(is_nan($y) , );'),
- ];
- yield 'str_starts_with with trailing ","' => [
- self::generateTest('self::assertStringStartsWith($needle, $haystack);'),
- self::generateTest('self::assertTrue(str_starts_with($haystack, $needle,));'),
- ];
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['target' => PhpUnitTargetVersion::VERSION_NEWEST]);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield [
- self::generateTest('$a = $this->assertTrue(...);'),
- ];
- }
- private static function generateTest(string $content): string
- {
- return "<?php final class FooTest extends \\PHPUnit_Framework_TestCase {\n public function testSomething() {\n ".$content."\n }\n}\n";
- }
- }
|