Skip to content

Commit 6501e8e

Browse files
mhujerondrejmirtes
authored andcommitted
add AssertSameDifferentTypesRuleTest
1 parent 96d2421 commit 6501e8e

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ includes:
22
- extension.neon
33
- rules.neon
44
- vendor/phpstan/phpstan-strict-rules/rules.neon
5+
6+
parameters:
7+
excludes_analyse:
8+
- */tests/*/data/*
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Rules\PHPUnit;
4+
5+
use PHPStan\Rules\Rule;
6+
7+
class AssertSameDifferentTypesRuleTest extends \PHPStan\Testing\RuleTestCase
8+
{
9+
10+
protected function getRule(): Rule
11+
{
12+
return new AssertSameDifferentTypesRule();
13+
}
14+
15+
public function testRule()
16+
{
17+
$this->analyse([__DIR__ . '/data/assert-same.php'], [
18+
[
19+
'Call to assertSame() with different types string and int will always result in test failure.',
20+
10,
21+
],
22+
[
23+
'Call to assertSame() with different types string and stdClass will always result in test failure.',
24+
11,
25+
],
26+
[
27+
'Call to assertSame() with different types int and string will always result in test failure.',
28+
12,
29+
],
30+
[
31+
'Call to assertSame() with different types string and int will always result in test failure.',
32+
13,
33+
],
34+
[
35+
'Call to assertSame() with different types array<int, string> and array<int, int> will always result in test failure.',
36+
14,
37+
],
38+
[
39+
'Call to assertSame() with different types array<string> and array<int> will always result in test failure.',
40+
35,
41+
],
42+
]);
43+
}
44+
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace ExampleTestCase;
4+
5+
class FooTestCase extends \PHPUnit\Framework\TestCase
6+
{
7+
8+
public function testObviouslyNotSameAssertSame()
9+
{
10+
$this->assertSame('1', 1);
11+
$this->assertSame('1', new \stdClass());
12+
$this->assertSame(1, $this->returnsString());
13+
$this->assertSame('1', self::returnsInt());
14+
$this->assertSame(['a', 'b'], [1, 2]);
15+
}
16+
17+
private function returnsString(): string
18+
{
19+
return 'foo';
20+
}
21+
22+
private static function returnsInt(): int
23+
{
24+
return 1;
25+
}
26+
27+
public function testArrays()
28+
{
29+
/** @var string[] $a */
30+
$a = ['x'];
31+
32+
/** @var int[] $b */
33+
$b = [1, 2];
34+
35+
$this->assertSame($a, $b);
36+
}
37+
38+
public function testLogicallyCorrectAssertSame()
39+
{
40+
$this->assertSame(1, 1);
41+
$this->assertSame(['a'], ['a', 'b']);
42+
$this->assertSame('1', '1');
43+
$this->assertSame('1', '2');
44+
$this->assertSame(new \stdClass(), new \stdClass());
45+
$this->assertSame('1', $this->returnsString());
46+
$this->assertSame(1, self::returnsInt());
47+
$this->assertSame(['a'], ['a', 1]);
48+
$this->assertSame(['a', 2, 3.0], ['a', 1]);
49+
}
50+
51+
}

tests/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
<?php declare(strict_types = 1);
22

3+
use PHPStan\Type\TypeCombinator;
4+
35
require_once __DIR__ . '/../vendor/autoload.php';
6+
7+
TypeCombinator::setUnionTypesEnabled(true);

0 commit comments

Comments
 (0)