|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace VGirol\PhpunitException; |
|
6
|
|
|
|
|
7
|
|
|
use VGirol\PhpunitException\Exception\InvalidArgumentException; |
|
8
|
|
|
use VGirol\PhpunitException\Exception\InvalidArgumentHelper; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Some helpers for testing |
|
12
|
|
|
*/ |
|
13
|
|
|
trait SetExceptionsTrait |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Set the class name of the expected exception |
|
17
|
|
|
* |
|
18
|
|
|
* @see \PHPUnit\Framework\TestCase::expectException |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $exception |
|
21
|
|
|
* |
|
22
|
|
|
* @return void |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract public function expectException(string $exception): void; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Set the message of the expected exception |
|
28
|
|
|
* |
|
29
|
|
|
* @see \PHPUnit\Framework\TestCase::expectExceptionMessage |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $message |
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract public function expectExceptionMessage(string $message): void; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Set the a regular expression for the message of the expected exception |
|
39
|
|
|
* |
|
40
|
|
|
* @see \PHPUnit\Framework\TestCase::expectExceptionMessageMatches |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $messageRegExp |
|
43
|
|
|
* |
|
44
|
|
|
* @return void |
|
45
|
|
|
*/ |
|
46
|
|
|
abstract public function expectExceptionMessageMatches(string $messageRegExp): void; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Set the code of the expected exception |
|
50
|
|
|
* |
|
51
|
|
|
* @see \PHPUnit\Framework\TestCase::expectExceptionCode |
|
52
|
|
|
* |
|
53
|
|
|
* @param int|string $code |
|
54
|
|
|
* |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
|
|
abstract public function expectExceptionCode($code): void; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Set the expected exception and message when defining a test that will fail. |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $className |
|
63
|
|
|
* @param string|null $message The failure message could be either a string or a regular expression. |
|
64
|
|
|
* @param int|string|null $code |
|
65
|
|
|
* |
|
66
|
|
|
* @return void |
|
67
|
|
|
*/ |
|
68
|
6 |
|
public function setFailure(string $className, ?string $message = null, $code = null): void |
|
69
|
|
|
{ |
|
70
|
6 |
|
$this->expectException($className); |
|
71
|
6 |
|
if ($message !== null) { |
|
72
|
6 |
|
$this->setExpectedMessage($message); |
|
73
|
|
|
} |
|
74
|
6 |
|
if ($code !== null) { |
|
75
|
6 |
|
$this->expectExceptionCode($code); |
|
76
|
|
|
} |
|
77
|
6 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set the expected exception and message when defining a test that will fail. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $className |
|
83
|
|
|
* @param string|null $message |
|
84
|
|
|
* @param int|string|null $code |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
3 |
|
public function setFailureException(string $className, ?string $message = null, $code = null): void |
|
89
|
|
|
{ |
|
90
|
3 |
|
$this->setFailure($className, $message, $code); |
|
91
|
3 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set the expected exception and message when defining a test that will fail. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|null $message |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
3 |
|
public function setFailureExceptionRegex(string $className, ?string $message = null, $code = null): void |
|
101
|
|
|
{ |
|
102
|
3 |
|
$this->setFailure($className, $message, $code); |
|
103
|
3 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Set the expected exception and message when testing a call with invalid arguments to a method. |
|
107
|
|
|
* |
|
108
|
|
|
* @param integer $arg |
|
109
|
|
|
* @param string $type |
|
110
|
|
|
* @param mixed $value |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
* |
|
114
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
|
115
|
|
|
*/ |
|
116
|
3 |
|
public function setInvalidArgumentException(int $arg, string $type, $value = null): void |
|
117
|
|
|
{ |
|
118
|
3 |
|
$this->expectException(InvalidArgumentException::class); |
|
119
|
3 |
|
$this->expectExceptionMessageMatches(InvalidArgumentHelper::messageRegex($arg, $type, $value)); |
|
120
|
3 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Format the failure message as a regular expression. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $message A format string used by the sprintf function |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function formatAsRegex(string $message): string |
|
130
|
|
|
{ |
|
131
|
3 |
|
return '/' . preg_replace( |
|
132
|
3 |
|
"!\%(\+?)('.|[0 ]|)(-?)([1-9][0-9]*|)(\.[1-9][0-9]*|)([%a-zA-Z])!u", |
|
133
|
3 |
|
'.*', |
|
134
|
3 |
|
preg_quote($message) |
|
135
|
3 |
|
) . '/s'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set the message of the expected exception. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $message |
|
142
|
|
|
* |
|
143
|
|
|
* @return void |
|
144
|
|
|
*/ |
|
145
|
6 |
|
private function setExpectedMessage(string $message): void |
|
146
|
|
|
{ |
|
147
|
6 |
|
$method = (strpos($message, '/') === 0) ? 'expectExceptionMessageMatches' : 'expectExceptionMessage'; |
|
148
|
6 |
|
$this->{$method}($message); |
|
149
|
6 |
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|