|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer: custom fixers. |
| 5 | + * |
| 6 | + * (c) 2018 Kuba Werłos |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Tests\Fixer; |
| 15 | + |
| 16 | +use PhpCsFixerCustomFixers\Fixer\PhpUnitDedicatedAssertFixer; |
| 17 | + |
| 18 | +/** |
| 19 | + * @internal |
| 20 | + * |
| 21 | + * @covers \PhpCsFixerCustomFixers\Fixer\PhpUnitDedicatedAssertFixer |
| 22 | + */ |
| 23 | +final class PhpUnitDedicatedAssertFixerTest extends AbstractFixerTestCase |
| 24 | +{ |
| 25 | + public function testIsRisky(): void |
| 26 | + { |
| 27 | + self::assertTrue($this->fixer->isRisky()); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @dataProvider provideFixCases |
| 32 | + */ |
| 33 | + public function testFix(string $expected, ?string $input = null): void |
| 34 | + { |
| 35 | + $this->doTest($expected, $input); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return iterable<array{0: string, 1?: string}> |
| 40 | + */ |
| 41 | + public static function provideFixCases(): iterable |
| 42 | + { |
| 43 | + foreach (self::getFixCases() as $name => $fixCase) { |
| 44 | + yield $name => \array_map( |
| 45 | + static function (string $case): string { |
| 46 | + return \sprintf('<?php |
| 47 | +class FooTest extends TestCase { |
| 48 | + public function testFoo() { |
| 49 | + %s |
| 50 | + } |
| 51 | +}', $case); |
| 52 | + }, |
| 53 | + $fixCase |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return iterable<array{0: string, 1?: string}> |
| 60 | + */ |
| 61 | + private static function getFixCases(): iterable |
| 62 | + { |
| 63 | + yield 'ignore class on not $this' => ['$notThis->assertSame(3, count($array));']; |
| 64 | + yield 'ignore property' => ['self::assertSame;']; |
| 65 | + yield 'ignore assertion with no arguments' => ['self::assertSame();']; |
| 66 | + yield 'ignore assertion with single argument' => ['self::assertSame(count($array));']; |
| 67 | + yield 'ignore other assertions' => ['self::assertGreaterThan(2, count($array));']; |
| 68 | + yield 'ignore other functions' => ['self::assertSame(2, countIncorrectly($array));']; |
| 69 | + yield 'ignore function in first argument' => ['self::assertSame(count($array), 2);']; |
| 70 | + yield 'ignore function from namespace' => ['self::assertSame(2, count\better_count($array));']; |
| 71 | + yield 'ignore function used with 2 arguments' => ['self::assertSame(3, count($array, COUNT_RECURSIVE));']; |
| 72 | + yield 'ignore assertion with code after function' => ['self::assertSame(3, count($array) + 1);']; |
| 73 | + |
| 74 | + yield 'fix count' => [ |
| 75 | + '$this->assertCount(3, $array);', |
| 76 | + '$this->assertSame(3, count($array));', |
| 77 | + ]; |
| 78 | + |
| 79 | + yield 'fix sizeof' => [ |
| 80 | + 'static::assertCount(3, $array);', |
| 81 | + 'static::assertSame(3, sizeof($array));', |
| 82 | + ]; |
| 83 | + |
| 84 | + yield 'fix instanceof' => [ |
| 85 | + 'self::assertInstanceOf("stdClass", $object);', |
| 86 | + 'self::assertSame("stdClass", get_class($object));', |
| 87 | + ]; |
| 88 | + |
| 89 | + yield 'fix not instanceof' => [ |
| 90 | + 'self::assertNotInstanceOf("Closure", $object);', |
| 91 | + 'self::assertNotSame("Closure", get_class($object));', |
| 92 | + ]; |
| 93 | + |
| 94 | + yield 'fix different casing' => [ |
| 95 | + 'self::assertCount(3, $array);', |
| 96 | + 'self::assertSame(3, COUNT($array));', |
| 97 | + ]; |
| 98 | + |
| 99 | + yield 'fix expected being variable' => [ |
| 100 | + 'self::assertCount($arrayCount, $array);', |
| 101 | + 'self::assertSame($arrayCount, count($array));', |
| 102 | + ]; |
| 103 | + |
| 104 | + yield 'fix with leading slash' => [ |
| 105 | + 'self::assertCount(3, $array);', |
| 106 | + 'self::assertSame(3, \count($array));', |
| 107 | + ]; |
| 108 | + |
| 109 | + yield 'fix with many spaces' => [ |
| 110 | + '$this->assertCount ( 3 , $array ) ;', |
| 111 | + '$this->assertSame ( 3 , \count ( $array ) ) ;', |
| 112 | + ]; |
| 113 | + |
| 114 | + $reflection = new \ReflectionClass(PhpUnitDedicatedAssertFixer::class); |
| 115 | + foreach ($reflection->getConstant('ASSERTIONS') as $assertion) { |
| 116 | + $expected = 'self::assertCount(3, $array);'; |
| 117 | + $input = \sprintf('self::%s(3, count($array));', $assertion); |
| 118 | + |
| 119 | + if (\stripos($assertion, 'Not', 6) !== false) { |
| 120 | + $expected = \str_replace('assert', 'assertNot', $expected); |
| 121 | + $expected = \str_replace('3', '4', $expected); |
| 122 | + $input = \str_replace('3', '4', $input); |
| 123 | + } |
| 124 | + |
| 125 | + yield \sprintf('Test assertion "%s"', $assertion) => [$expected, $input]; |
| 126 | + } |
| 127 | + |
| 128 | + yield 'fix multiple assertions' => [ |
| 129 | + ' |
| 130 | + if (false) self::assertSame(1); |
| 131 | + self::assertSame(3, $arrayCount); |
| 132 | + self::assertCount(3, $array); |
| 133 | + if (false) self::assertSame(4); |
| 134 | + ', |
| 135 | + ' |
| 136 | + if (false) self::assertSame(1); |
| 137 | + self::assertSame(3, $arrayCount); |
| 138 | + self::assertSame(3, count($array)); |
| 139 | + if (false) self::assertSame(4); |
| 140 | + ', |
| 141 | + ]; |
| 142 | + } |
| 143 | +} |
0 commit comments