diff --git a/phpstan.neon b/phpstan.neon index 5ec89139..7e41decb 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,3 +2,7 @@ includes: - extension.neon - rules.neon - vendor/phpstan/phpstan-strict-rules/rules.neon + +parameters: + excludes_analyse: + - */tests/*/data/* diff --git a/tests/Rules/PHPUnit/AssertSameDifferentTypesRuleTest.php b/tests/Rules/PHPUnit/AssertSameDifferentTypesRuleTest.php new file mode 100644 index 00000000..ac66d1c0 --- /dev/null +++ b/tests/Rules/PHPUnit/AssertSameDifferentTypesRuleTest.php @@ -0,0 +1,45 @@ +analyse([__DIR__ . '/data/assert-same.php'], [ + [ + 'Call to assertSame() with different types string and int will always result in test failure.', + 10, + ], + [ + 'Call to assertSame() with different types string and stdClass will always result in test failure.', + 11, + ], + [ + 'Call to assertSame() with different types int and string will always result in test failure.', + 12, + ], + [ + 'Call to assertSame() with different types string and int will always result in test failure.', + 13, + ], + [ + 'Call to assertSame() with different types array and array will always result in test failure.', + 14, + ], + [ + 'Call to assertSame() with different types array and array will always result in test failure.', + 35, + ], + ]); + } + +} diff --git a/tests/Rules/PHPUnit/data/assert-same.php b/tests/Rules/PHPUnit/data/assert-same.php new file mode 100644 index 00000000..8d3a18c6 --- /dev/null +++ b/tests/Rules/PHPUnit/data/assert-same.php @@ -0,0 +1,51 @@ +assertSame('1', 1); + $this->assertSame('1', new \stdClass()); + $this->assertSame(1, $this->returnsString()); + $this->assertSame('1', self::returnsInt()); + $this->assertSame(['a', 'b'], [1, 2]); + } + + private function returnsString(): string + { + return 'foo'; + } + + private static function returnsInt(): int + { + return 1; + } + + public function testArrays() + { + /** @var string[] $a */ + $a = ['x']; + + /** @var int[] $b */ + $b = [1, 2]; + + $this->assertSame($a, $b); + } + + public function testLogicallyCorrectAssertSame() + { + $this->assertSame(1, 1); + $this->assertSame(['a'], ['a', 'b']); + $this->assertSame('1', '1'); + $this->assertSame('1', '2'); + $this->assertSame(new \stdClass(), new \stdClass()); + $this->assertSame('1', $this->returnsString()); + $this->assertSame(1, self::returnsInt()); + $this->assertSame(['a'], ['a', 1]); + $this->assertSame(['a', 2, 3.0], ['a', 1]); + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 22e480dd..9dfa9f56 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,3 +1,7 @@