-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
DeprecationTest.php
129 lines (114 loc) · 4.56 KB
/
DeprecationTest.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
class DeprecationTest extends TestCase
{
public function testItCanDetermineTheClassWhereTheDeprecationHappened()
{
$deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
$this->assertTrue($deprecation->originatesFromAnObject());
$this->assertSame(self::class, $deprecation->originatingClass());
$this->assertSame(__FUNCTION__, $deprecation->originatingMethod());
}
public function testItCanTellWhetherItIsInternal()
{
$r = new \ReflectionClass(Deprecation::class);
if (dirname($r->getFileName(), 2) !== dirname(__DIR__, 2)) {
$this->markTestSkipped('Test case is not compatible with having the bridge in vendor/');
}
$deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
$this->assertSame(Deprecation::TYPE_SELF, $deprecation->getType());
}
public function testLegacyTestMethodIsDetectedAsSuch()
{
$deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
$this->assertTrue($deprecation->isLegacy('whatever'));
}
public function testItCanBeConvertedToAString()
{
$deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
$this->assertStringContainsString('💩', $deprecation->toString());
$this->assertStringContainsString(__FUNCTION__, $deprecation->toString());
}
public function testItRulesOutFilesOutsideVendorsAsIndirect()
{
$deprecation = new Deprecation('💩', $this->debugBacktrace(), __FILE__);
$this->assertNotSame(Deprecation::TYPE_INDIRECT, $deprecation->getType());
}
/**
* @dataProvider mutedProvider
*/
public function testItMutesOnlySpecificErrorMessagesWhenTheCallingCodeIsInPhpunit($muted, $callingClass, $message)
{
$trace = $this->debugBacktrace();
array_unshift($trace, ['class' => $callingClass]);
array_unshift($trace, ['class' => DeprecationErrorHandler::class]);
$deprecation = new Deprecation($message, $trace, 'should_not_matter.php');
$this->assertSame($muted, $deprecation->isMuted());
}
public function mutedProvider()
{
yield 'not from phpunit, and not a whitelisted message' => [
false,
\My\Source\Code::class,
'Self deprecating humor is deprecated by itself'
];
yield 'from phpunit, but not a whitelisted message' => [
false,
\PHPUnit\Random\Piece\Of\Code::class,
'Self deprecating humor is deprecated by itself'
];
yield 'whitelisted message, but not from phpunit' => [
false,
\My\Source\Code::class,
'Function ReflectionType::__toString() is deprecated',
];
yield 'from phpunit and whitelisted message' => [
true,
\PHPUnit\Random\Piece\Of\Code::class,
'Function ReflectionType::__toString() is deprecated',
];
}
public function testNotMutedIfNotCalledFromAClassButARandomFile()
{
$deprecation = new Deprecation(
'Function ReflectionType::__toString() is deprecated',
[
['file' => 'should_not_matter.php'],
['file' => 'should_not_matter_either.php'],
],
'my-procedural-controller.php'
);
$this->assertFalse($deprecation->isMuted());
}
public function testItTakesMutesDeprecationFromPhpUnitFiles()
{
$deprecation = new Deprecation(
'Function ReflectionType::__toString() is deprecated',
[
['file' => 'should_not_matter.php'],
['file' => 'should_not_matter_either.php'],
],
'random_path' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'phpunit' . \DIRECTORY_SEPARATOR . 'whatever.php'
);
$this->assertTrue($deprecation->isMuted());
}
/**
* This method is here to simulate the extra level from the piece of code
* triggering an error to the error handler
*/
public function debugBacktrace(): array
{
return debug_backtrace();
}
}