@@ -25,6 +25,9 @@ class SymfonyTestsListener extends \PHPUnit_Framework_BaseTestListener
25
25
private $ skippedFile = false ;
26
26
private $ wasSkipped = array ();
27
27
private $ isSkipped = array ();
28
+ private $ expectedDeprecations ;
29
+ private $ gatheredDeprecations ;
30
+ private $ previousErrorHandler ;
28
31
29
32
/**
30
33
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive)
@@ -142,7 +145,7 @@ public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $ti
142
145
public function startTest (\PHPUnit_Framework_Test $ test )
143
146
{
144
147
if (-2 < $ this ->state && $ test instanceof \PHPUnit_Framework_TestCase) {
145
- $ groups = \PHPUnit_Util_Test::getGroups (get_class ($ test ), $ test ->getName ());
148
+ $ groups = \PHPUnit_Util_Test::getGroups (get_class ($ test ), $ test ->getName (false ));
146
149
147
150
if (in_array ('time-sensitive ' , $ groups , true )) {
148
151
ClockMock::register (get_class ($ test ));
@@ -151,13 +154,37 @@ public function startTest(\PHPUnit_Framework_Test $test)
151
154
if (in_array ('dns-sensitive ' , $ groups , true )) {
152
155
DnsMock::register (get_class ($ test ));
153
156
}
157
+
158
+ $ annotations = \PHPUnit_Util_Test::parseTestMethodAnnotations (get_class ($ test ), $ test ->getName (false ));
159
+
160
+ if (isset ($ annotations ['class ' ]['expectedDeprecation ' ])) {
161
+ $ test ->getTestResultObject ()->addError ($ test , new \PHPUnit_Framework_AssertionFailedError ('`@expectedDeprecation` annotations are not allowed at the class level. ' ), 0 );
162
+ }
163
+ if (isset ($ annotations ['method ' ]['expectedDeprecation ' ])) {
164
+ if (!in_array ('legacy ' , $ groups , true )) {
165
+ $ test ->getTestResultObject ()->addError ($ test , new \PHPUnit_Framework_AssertionFailedError ('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`. ' ), 0 );
166
+ }
167
+ $ this ->expectedDeprecations = $ annotations ['method ' ]['expectedDeprecation ' ];
168
+ $ this ->previousErrorHandler = set_error_handler (array ($ this , 'handleError ' ));
169
+ }
154
170
}
155
171
}
156
172
157
173
public function endTest (\PHPUnit_Framework_Test $ test , $ time )
158
174
{
175
+ if ($ this ->expectedDeprecations ) {
176
+ restore_error_handler ();
177
+ try {
178
+ $ prefix = "@expectedDeprecation: \n " ;
179
+ $ test ->assertStringMatchesFormat ($ prefix .implode ("\n " , $ this ->expectedDeprecations ), $ prefix .implode ("\n " , $ this ->gatheredDeprecations ));
180
+ } catch (\PHPUnit_Framework_AssertionFailedError $ e ) {
181
+ $ test ->getTestResultObject ()->addFailure ($ test , $ e , $ time );
182
+ }
183
+
184
+ $ this ->expectedDeprecations = $ this ->gatheredDeprecations = $ this ->previousErrorHandler = null ;
185
+ }
159
186
if (-2 < $ this ->state && $ test instanceof \PHPUnit_Framework_TestCase) {
160
- $ groups = \PHPUnit_Util_Test::getGroups (get_class ($ test ), $ test ->getName ());
187
+ $ groups = \PHPUnit_Util_Test::getGroups (get_class ($ test ), $ test ->getName (false ));
161
188
162
189
if (in_array ('time-sensitive ' , $ groups , true )) {
163
190
ClockMock::withClockMock (false );
@@ -167,4 +194,17 @@ public function endTest(\PHPUnit_Framework_Test $test, $time)
167
194
}
168
195
}
169
196
}
197
+
198
+ public function handleError ($ type , $ msg , $ file , $ line , $ context )
199
+ {
200
+ if (E_USER_DEPRECATED !== $ type && E_DEPRECATED !== $ type ) {
201
+ $ h = $ this ->previousErrorHandler ;
202
+
203
+ return $ h ? $ h ($ type , $ msg , $ file , $ line , $ context ) : false ;
204
+ }
205
+ if (error_reporting ()) {
206
+ $ msg = 'Unsilenced deprecation: ' .$ msg ;
207
+ }
208
+ $ this ->gatheredDeprecations [] = $ msg ;
209
+ }
170
210
}
0 commit comments