@@ -335,23 +335,41 @@ Write Assertions about Deprecations
335
335
336
336
When adding deprecations to your code, you might like writing tests that verify
337
337
that they are triggered as required. To do so, the bridge provides the
338
- ``@expectedDeprecation `` annotation that you can use on your test methods.
338
+ ``expectDeprecation() `` method that you can use on your test methods.
339
339
It requires you to pass the expected message, given in the same format as for
340
340
the `PHPUnit's assertStringMatchesFormat() `_ method. If you expect more than one
341
- deprecation message for a given test method, you can use the annotation several
341
+ deprecation message for a given test method, you can use the method several
342
342
times (order matters)::
343
343
344
- /**
345
- * @group legacy
346
- * @expectedDeprecation This "%s" method is deprecated.
347
- * @expectedDeprecation The second argument of the "%s" method is deprecated.
348
- */
349
- public function testDeprecatedCode()
344
+ use PHPUnit\Framework\TestCase;
345
+ use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
346
+
347
+ class MyTest extends TestCase
350
348
{
351
- @trigger_error('This "Foo" method is deprecated.', E_USER_DEPRECATED);
352
- @trigger_error('The second argument of the "Bar" method is deprecated.', E_USER_DEPRECATED);
349
+ use ExpectDeprecationTrait;
350
+
351
+ /**
352
+ * @group legacy
353
+ */
354
+ public function testDeprecatedCode()
355
+ {
356
+ // test some code that triggers the following deprecation:
357
+ // @trigger_error('This "Foo" method is deprecated.', E_USER_DEPRECATED);
358
+ $this->expectDeprecation('This "%s" method is deprecated');
359
+
360
+ // ...
361
+
362
+ // test some code that triggers the following deprecation:
363
+ // @trigger_error('The second argument of the "Bar" method is deprecated.', E_USER_DEPRECATED);
364
+ $this->expectDeprecation('The second argument of the "%s" method is deprecated.');
365
+ }
353
366
}
354
367
368
+ .. deprecated :: 5.1
369
+
370
+ Symfony versions previous to 5.1 also included a ``@expectedDeprecation ``
371
+ annotation to test deprecations, but it was deprecated in favor of the method.
372
+
355
373
Display the Full Stack Trace
356
374
----------------------------
357
375
0 commit comments