1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jasny\PHPUnit; |
6
|
|
|
|
7
|
|
|
use Jasny\PHPUnit\Util\ExpectedWarnings; |
8
|
|
|
use PHPUnit\Framework\AssertionFailedError; |
9
|
|
|
use PHPUnit\Framework\Error\Deprecated; |
10
|
|
|
use PHPUnit\Framework\Error\Notice; |
11
|
|
|
use PHPUnit\Framework\Error\Warning as WarningError; |
12
|
|
|
use PHPUnit\Runner\BaseTestRunner; |
13
|
|
|
|
14
|
|
|
trait ExpectWarningTrait |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ExpectedWarnings |
18
|
|
|
*/ |
19
|
|
|
private $expectedWarnings; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @before |
23
|
|
|
* @internal |
24
|
|
|
*/ |
25
|
14 |
|
protected function registerExpectedWarnings(): void |
26
|
|
|
{ |
27
|
14 |
|
$this->expectedWarnings = new ExpectedWarnings(); |
28
|
14 |
|
$this->expectedWarnings->register(); |
29
|
14 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @after |
33
|
|
|
* @internal |
34
|
|
|
*/ |
35
|
14 |
|
protected function unregisterExpectedWarnings(): void |
36
|
|
|
{ |
37
|
14 |
|
if ($this->expectedWarnings === null) { |
38
|
2 |
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
12 |
|
$this->expectedWarnings->unregister(); |
42
|
12 |
|
$this->expectedWarnings = null; |
43
|
12 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Performs assertions shared by all tests of a test cas. |
47
|
|
|
*/ |
48
|
14 |
|
protected function assertPostConditions(): void |
49
|
|
|
{ |
50
|
14 |
|
$okStatus = [BaseTestRunner::STATUS_PASSED, BaseTestRunner::STATUS_RISKY, BaseTestRunner::STATUS_UNKNOWN]; |
51
|
14 |
|
if ($this->expectedWarnings === null || !in_array($this->getStatus(), $okStatus, true)) { |
|
|
|
|
52
|
2 |
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
$notTriggered = $this->expectedWarnings->getNotTriggered(); |
56
|
|
|
|
57
|
12 |
|
if ($notTriggered !== []) { |
58
|
|
|
// @codeCoverageIgnoreStart |
59
|
|
|
$desc = ExpectedWarnings::describeExpected(reset($notTriggered)); |
60
|
|
|
throw new AssertionFailedError(\sprintf('Failed asserting that %s is triggered', $desc)); |
61
|
|
|
// @codeCoverageIgnoreEnd |
62
|
|
|
} |
63
|
12 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function expectDeprecation(): void |
66
|
|
|
{ |
67
|
1 |
|
$this->expectedWarnings->add('deprecation'); |
68
|
1 |
|
$this->addToAssertionCount(1); |
|
|
|
|
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
public function expectDeprecationMessage(string $message): void |
72
|
|
|
{ |
73
|
1 |
|
$this->expectedWarnings->add('deprecation', $message); |
74
|
1 |
|
$this->addToAssertionCount(1); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
3 |
|
public function expectDeprecationMessageMatches(string $regularExpression): void |
78
|
|
|
{ |
79
|
3 |
|
$this->expectedWarnings->addRegExp('deprecation', $regularExpression); |
80
|
3 |
|
$this->addToAssertionCount(1); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
3 |
|
public function expectNotice(): void |
84
|
|
|
{ |
85
|
3 |
|
$this->expectedWarnings->add('notice'); |
86
|
3 |
|
$this->addToAssertionCount(1); |
87
|
3 |
|
} |
88
|
|
|
|
89
|
1 |
|
public function expectNoticeMessage(string $message): void |
90
|
|
|
{ |
91
|
1 |
|
$this->expectedWarnings->add('notice', $message); |
92
|
1 |
|
$this->addToAssertionCount(1); |
93
|
1 |
|
} |
94
|
|
|
|
95
|
1 |
|
public function expectNoticeMessageMatches(string $regularExpression): void |
96
|
|
|
{ |
97
|
1 |
|
$this->expectedWarnings->addRegExp('notice', $regularExpression); |
98
|
1 |
|
$this->addToAssertionCount(1); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
1 |
|
public function expectWarning(): void |
102
|
|
|
{ |
103
|
1 |
|
$this->expectedWarnings->add('warning'); |
104
|
1 |
|
$this->addToAssertionCount(1); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
3 |
|
public function expectWarningMessage(string $message): void |
108
|
|
|
{ |
109
|
3 |
|
$this->expectedWarnings->add('warning', $message); |
110
|
3 |
|
$this->addToAssertionCount(1); |
111
|
3 |
|
} |
112
|
|
|
|
113
|
1 |
|
public function expectWarningMessageMatches(string $regularExpression): void |
114
|
|
|
{ |
115
|
1 |
|
$this->expectedWarnings->addRegExp('warning', $regularExpression); |
116
|
1 |
|
$this->addToAssertionCount(1); |
117
|
1 |
|
} |
118
|
|
|
} |
119
|
|
|
|