forked from phpstan/phpstan-phpunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert-same.php
More file actions
89 lines (73 loc) · 2.11 KB
/
Copy pathassert-same.php
File metadata and controls
89 lines (73 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php declare(strict_types = 1);
namespace ExampleTestCaseAsserts;
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());
$this->assertSame(['a', 'b'], [1, 2]);
self::assertSame('1', 2); // test self
static::assertSame('1', 2); // test static
parent::assertSame('1', 2); // test parent
}
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]);
self::assertSame(1, 2); // test self
static::assertSame(1, 2); // test static
parent::assertSame(1, 2); // test parent
}
public function testOther()
{
// assertEquals is not checked
$this->assertEquals('1', 1);
// only calls on \PHPUnit\Framework\TestCase are analyzed
$foo = new \Dummy\Foo();
$foo->assertSame();
}
public function testAssertContains()
{
$this->assertContains('not in the list', new \ArrayObject([1]));
$this->assertContainsEquals('not in the list', new \ArrayObject([1]));
$this->assertNotContains('not in the list', new \ArrayObject([1]));
}
public function testStaticMethodReturnWithSameTypeIsNotReported()
{
$this->assertSame(self::createSomething('foo'), self::createSomething('foo'));
$this->assertNotSame(self::createSomething('bar'), self::createSomething('bar'));
}
/**
* @return object
*/
private static function createSomething(string $what)
{
return new \stdClass();
}
}