13
13
14
14
use Doctrine \Common \Annotations \AnnotationRegistry ;
15
15
use PHPUnit \Framework \AssertionFailedError ;
16
+ use PHPUnit \Framework \RiskyTestError ;
16
17
use PHPUnit \Framework \TestCase ;
17
18
use PHPUnit \Framework \TestSuite ;
18
19
use PHPUnit \Runner \BaseTestRunner ;
19
20
use PHPUnit \Util \Blacklist ;
20
21
use PHPUnit \Util \Test ;
21
22
use Symfony \Bridge \PhpUnit \ClockMock ;
22
23
use Symfony \Bridge \PhpUnit \DnsMock ;
24
+ use Symfony \Bridge \PhpUnit \ExpectDeprecationTrait ;
23
25
use Symfony \Component \Debug \DebugClassLoader as LegacyDebugClassLoader ;
24
26
use Symfony \Component \ErrorHandler \DebugClassLoader ;
25
27
32
34
*/
33
35
class SymfonyTestsListenerTrait
34
36
{
37
+ public static $ expectedDeprecations = [];
38
+ public static $ previousErrorHandler ;
39
+ private static $ gatheredDeprecations = [];
35
40
private static $ globallyEnabled = false ;
36
41
private $ state = -1 ;
37
42
private $ skippedFile = false ;
38
43
private $ wasSkipped = [];
39
44
private $ isSkipped = [];
40
- private $ expectedDeprecations = [];
41
- private $ gatheredDeprecations = [];
42
- private $ previousErrorHandler ;
43
- private $ error ;
44
45
private $ runsInSeparateProcess = false ;
46
+ private $ checkNumAssertions = false ;
45
47
46
48
/**
47
49
* @param array $mockedNamespaces List of namespaces, indexed by mocked features (time-sensitive or dns-sensitive)
@@ -220,15 +222,17 @@ public function startTest($test)
220
222
if (isset ($ annotations ['class ' ]['expectedDeprecation ' ])) {
221
223
$ test ->getTestResultObject ()->addError ($ test , new AssertionFailedError ('`@expectedDeprecation` annotations are not allowed at the class level. ' ), 0 );
222
224
}
223
- if (isset ($ annotations ['method ' ]['expectedDeprecation ' ])) {
224
- if (!\in_array ('legacy ' , $ groups , true )) {
225
- $ this ->error = new AssertionFailedError ('Only tests with the `@group legacy` annotation can have `@expectedDeprecation`. ' );
225
+ if (isset ($ annotations ['method ' ]['expectedDeprecation ' ]) || $ this ->checkNumAssertions = \in_array (ExpectDeprecationTrait::class, class_uses ($ test ), true )) {
226
+ if (isset ($ annotations ['method ' ]['expectedDeprecation ' ])) {
227
+ self ::$ expectedDeprecations = $ annotations ['method ' ]['expectedDeprecation ' ];
228
+ self ::$ previousErrorHandler = set_error_handler ([self ::class, 'handleError ' ]);
226
229
}
227
230
228
- $ test ->getTestResultObject ()->beStrictAboutTestsThatDoNotTestAnything (false );
231
+ if ($ this ->checkNumAssertions ) {
232
+ $ this ->checkNumAssertions = $ test ->getTestResultObject ()->isStrictAboutTestsThatDoNotTestAnything () && !$ test ->doesNotPerformAssertions ();
233
+ }
229
234
230
- $ this ->expectedDeprecations = $ annotations ['method ' ]['expectedDeprecation ' ];
231
- $ this ->previousErrorHandler = set_error_handler ([$ this , 'handleError ' ]);
235
+ $ test ->getTestResultObject ()->beStrictAboutTestsThatDoNotTestAnything (false );
232
236
}
233
237
}
234
238
}
@@ -242,9 +246,12 @@ public function endTest($test, $time)
242
246
$ className = \get_class ($ test );
243
247
$ groups = Test::getGroups ($ className , $ test ->getName (false ));
244
248
245
- if ($ errored = null !== $ this ->error ) {
246
- $ test ->getTestResultObject ()->addError ($ test , $ this ->error , 0 );
247
- $ this ->error = null ;
249
+ if ($ this ->checkNumAssertions ) {
250
+ if (!self ::$ expectedDeprecations && !$ test ->getNumAssertions ()) {
251
+ $ test ->getTestResultObject ()->addFailure ($ test , new RiskyTestError ('This test did not perform any assertions ' ), $ time );
252
+ }
253
+
254
+ $ this ->checkNumAssertions = false ;
248
255
}
249
256
250
257
if ($ this ->runsInSeparateProcess ) {
@@ -263,24 +270,26 @@ public function endTest($test, $time)
263
270
$ this ->runsInSeparateProcess = false ;
264
271
}
265
272
266
- if ($ this -> expectedDeprecations ) {
273
+ if (self :: $ expectedDeprecations ) {
267
274
if (!\in_array ($ test ->getStatus (), [BaseTestRunner::STATUS_SKIPPED , BaseTestRunner::STATUS_INCOMPLETE ], true )) {
268
- $ test ->addToAssertionCount (\count ($ this -> expectedDeprecations ));
275
+ $ test ->addToAssertionCount (\count (self :: $ expectedDeprecations ));
269
276
}
270
277
271
278
restore_error_handler ();
272
279
273
- if (!$ errored && !\in_array ($ test ->getStatus (), [BaseTestRunner::STATUS_SKIPPED , BaseTestRunner::STATUS_INCOMPLETE , BaseTestRunner::STATUS_FAILURE , BaseTestRunner::STATUS_ERROR ], true )) {
280
+ if (!\in_array ('legacy ' , $ groups , true )) {
281
+ $ test ->getTestResultObject ()->addError ($ test , new AssertionFailedError ('Only tests with the `@group legacy` annotation can expect a deprecation. ' ), 0 );
282
+ } elseif (!\in_array ($ test ->getStatus (), [BaseTestRunner::STATUS_SKIPPED , BaseTestRunner::STATUS_INCOMPLETE , BaseTestRunner::STATUS_FAILURE , BaseTestRunner::STATUS_ERROR ], true )) {
274
283
try {
275
284
$ prefix = "@expectedDeprecation: \n" ;
276
- $ test ->assertStringMatchesFormat ($ prefix .'%A ' .implode ("\n%A " , $ this -> expectedDeprecations )."\n%A " , $ prefix .' ' .implode ("\n " , $ this -> gatheredDeprecations )."\n" );
285
+ $ test ->assertStringMatchesFormat ($ prefix .'%A ' .implode ("\n%A " , self :: $ expectedDeprecations )."\n%A " , $ prefix .' ' .implode ("\n " , self :: $ gatheredDeprecations )."\n" );
277
286
} catch (AssertionFailedError $ e ) {
278
287
$ test ->getTestResultObject ()->addFailure ($ test , $ e , $ time );
279
288
}
280
289
}
281
290
282
- $ this -> expectedDeprecations = $ this -> gatheredDeprecations = [];
283
- $ this -> previousErrorHandler = null ;
291
+ self :: $ expectedDeprecations = self :: $ gatheredDeprecations = [];
292
+ self :: $ previousErrorHandler = null ;
284
293
}
285
294
if (!$ this ->runsInSeparateProcess && -2 < $ this ->state && ($ test instanceof \PHPUnit \Framework \TestCase || $ test instanceof TestCase)) {
286
295
if (\in_array ('time-sensitive ' , $ groups , true )) {
@@ -292,10 +301,10 @@ public function endTest($test, $time)
292
301
}
293
302
}
294
303
295
- public function handleError ($ type , $ msg , $ file , $ line , $ context = [])
304
+ public static function handleError ($ type , $ msg , $ file , $ line , $ context = [])
296
305
{
297
306
if (E_USER_DEPRECATED !== $ type && E_DEPRECATED !== $ type ) {
298
- $ h = $ this -> previousErrorHandler ;
307
+ $ h = self :: $ previousErrorHandler ;
299
308
300
309
return $ h ? $ h ($ type , $ msg , $ file , $ line , $ context ) : false ;
301
310
}
@@ -308,7 +317,7 @@ public function handleError($type, $msg, $file, $line, $context = [])
308
317
if (error_reporting ()) {
309
318
$ msg = 'Unsilenced deprecation: ' .$ msg ;
310
319
}
311
- $ this -> gatheredDeprecations [] = $ msg ;
320
+ self :: $ gatheredDeprecations [] = $ msg ;
312
321
313
322
return null ;
314
323
}
0 commit comments