|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Infection; |
| 4 | + |
| 5 | +use Infection\Testing\BaseMutatorTestCase; |
| 6 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 7 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 8 | + |
| 9 | +#[CoversClass(LooseBooleanMutator::class)] |
| 10 | +final class LooseBooleanMutatorTest extends BaseMutatorTestCase |
| 11 | +{ |
| 12 | + |
| 13 | + /** |
| 14 | + * @param string|string[] $expected |
| 15 | + */ |
| 16 | + #[DataProvider('mutationsProvider')] |
| 17 | + public function testMutator(string $input, $expected = []): void |
| 18 | + { |
| 19 | + $this->assertMutatesInput($input, $expected); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * @return iterable<string, array{0: string, 1?: string}> |
| 24 | + */ |
| 25 | + public static function mutationsProvider(): iterable |
| 26 | + { |
| 27 | + yield 'It mutates isFalse() into loose comparison' => [ |
| 28 | + <<<'PHP' |
| 29 | + <?php |
| 30 | +
|
| 31 | + $type->isFalse()->yes(); |
| 32 | + PHP |
| 33 | +, |
| 34 | + <<<'PHP' |
| 35 | + <?php |
| 36 | +
|
| 37 | + $type->toBoolean()->isFalse()->yes(); |
| 38 | + PHP |
| 39 | +, |
| 40 | + ]; |
| 41 | + |
| 42 | + yield 'It mutates isTrue() into loose comparison' => [ |
| 43 | + <<<'PHP' |
| 44 | + <?php |
| 45 | +
|
| 46 | + $type->isTrue()->yes(); |
| 47 | + PHP |
| 48 | +, |
| 49 | + <<<'PHP' |
| 50 | + <?php |
| 51 | +
|
| 52 | + $type->toBoolean()->isTrue()->yes(); |
| 53 | + PHP |
| 54 | +, |
| 55 | + ]; |
| 56 | + |
| 57 | + yield 'It skips already toBoolean() calls to prevent double repetition' => [ |
| 58 | + <<<'PHP' |
| 59 | + <?php |
| 60 | +
|
| 61 | + $type->toBoolean()->isTrue()->yes(); |
| 62 | + PHP |
| 63 | +, |
| 64 | + ]; |
| 65 | + |
| 66 | + yield 'It skips non boolean Type calls' => [ |
| 67 | + <<<'PHP' |
| 68 | + <?php |
| 69 | +
|
| 70 | + $type->isObject()->yes(); |
| 71 | + PHP |
| 72 | +, |
| 73 | + ]; |
| 74 | + |
| 75 | + yield 'skip isTrue() with arguments' => [ |
| 76 | + <<<'PHP' |
| 77 | + <?php |
| 78 | +
|
| 79 | + $a->isTrue($b); |
| 80 | + PHP |
| 81 | +, |
| 82 | + ]; |
| 83 | + |
| 84 | + yield 'skip isFalse() with arguments' => [ |
| 85 | + <<<'PHP' |
| 86 | + <?php |
| 87 | +
|
| 88 | + $a->isFalse($b, $c); |
| 89 | + PHP |
| 90 | +, |
| 91 | + ]; |
| 92 | + } |
| 93 | + |
| 94 | + protected function getTestedMutatorClassName(): string |
| 95 | + { |
| 96 | + return LooseBooleanMutator::class; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments