Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ includes:
- extension.neon
- rules.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon

parameters:
excludes_analyse:
- */tests/*/data/*
45 changes: 45 additions & 0 deletions tests/Rules/PHPUnit/AssertSameDifferentTypesRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\PHPUnit;

use PHPStan\Rules\Rule;

class AssertSameDifferentTypesRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): Rule
{
return new AssertSameDifferentTypesRule();
}

public function testRule()
{
$this->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<int, string> and array<int, int> will always result in test failure.',
14,
],
[
'Call to assertSame() with different types array<string> and array<int> will always result in test failure.',
35,
],
]);
}

}
51 changes: 51 additions & 0 deletions tests/Rules/PHPUnit/data/assert-same.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php declare(strict_types = 1);

namespace ExampleTestCase;

class FooTestCase extends \PHPUnit\Framework\TestCase
{

public function testObviouslyNotSameAssertSame()
{
$this->assertSame('1', 1);
$this->assertSame('1', new \stdClass());
$this->assertSame(1, $this->returnsString());
$this->assertSame('1', self::returnsInt());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should also be a few examples of positive cases - comparing int/int and probably also comparing some unions against simple type and unions against unions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added bellow

$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]);
}

}
4 changes: 4 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
<?php declare(strict_types = 1);

use PHPStan\Type\TypeCombinator;

require_once __DIR__ . '/../vendor/autoload.php';

TypeCombinator::setUnionTypesEnabled(true);