@@ -415,7 +415,7 @@ Modified PHPUnit script
415
415
-----------------------
416
416
417
417
.. versionadded :: 3.2
418
- This modified PHPUnit script was introduced in the 3.2 version of
418
+ This modified PHPUnit script was introduced in the 3.2 version of
419
419
this component.
420
420
421
421
This bridge provides a modified version of PHPUnit that you can call by using
@@ -448,9 +448,118 @@ If you have installed the bridge through Composer, you can run it by calling e.g
448
448
449
449
.. tip ::
450
450
451
- If you still need to use ``prophecy `` (but not ``symfony/yaml ``),
451
+ If you still need to use ``prophecy `` (but not ``symfony/yaml ``),
452
452
then set the ``SYMFONY_PHPUNIT_REMOVE `` env var to ``symfony/yaml ``.
453
453
454
+ Code coverage listener
455
+ ----------------------
456
+
457
+ Use case
458
+ ~~~~~~~~
459
+
460
+ By default the code coverage is computed with the following rule: if a line of
461
+ code is executed, then it is marked as covered. And the test which executes a
462
+ line of code is therefore marked as "covering the line of code". This can be
463
+ misleading.
464
+
465
+ Consider the following example::
466
+
467
+ class Bar
468
+ {
469
+ public function barMethod()
470
+ {
471
+ return 'bar';
472
+ }
473
+ }
474
+
475
+ class Foo
476
+ {
477
+ private $bar;
478
+
479
+ public function __construct(Bar $bar)
480
+ {
481
+ $this->bar = $bar;
482
+ }
483
+
484
+ public function fooMethod()
485
+ {
486
+ $this->bar->barMethod();
487
+
488
+ return 'bar';
489
+ }
490
+ }
491
+
492
+ class FooTest extends PHPUnit\Framework\TestCase
493
+ {
494
+ public function test()
495
+ {
496
+ $bar = new Bar();
497
+ $foo = new Foo($bar);
498
+
499
+ $this->assertSame('bar', $foo->fooMethod());
500
+ }
501
+ }
502
+
503
+
504
+ The ``FooTest::test `` method executes every single line of code of both ``Foo ``
505
+ and ``Bar `` classes, but ``Bar `` is not truly tested. The ``CoverageListener ``
506
+ aims to fix this behavior by adding the appropriate ``@covers `` annotation on
507
+ each test class.
508
+
509
+ If a test class already defines the ``@covers `` annotation, this listener does
510
+ nothing. Otherwise, it tries to find the code related to the test by removing
511
+ the ``Test `` part of the classname: ``My\Namespace\Tests\FooTest `` ->
512
+ ``My\Namespace\Foo ``.
513
+
514
+ Installation
515
+ ~~~~~~~~~~~~
516
+
517
+ Add the following configuration to the ``phpunit.xml.dist `` file
518
+
519
+ .. code-block :: xml
520
+
521
+ <!-- http://phpunit.de/manual/6.0/en/appendixes.configuration.html -->
522
+ <phpunit xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
523
+ xsi : noNamespaceSchemaLocation =" http://schema.phpunit.de/6.0/phpunit.xsd"
524
+ >
525
+
526
+ <!-- ... -->
527
+
528
+ <listeners >
529
+ <listener class =" Symfony\Bridge\PhpUnit\CoverageListener" />
530
+ </listeners >
531
+ </phpunit >
532
+
533
+ If the logic followed to find the related code is too simple or doesn't work for
534
+ your application, you can use your own SUT (System Under Test) solver:
535
+
536
+ .. code-block :: xml
537
+
538
+ <listeners >
539
+ <listener class =" Symfony\Bridge\PhpUnit\CoverageListener" >
540
+ <arguments >
541
+ <string >My\Namespace\SutSolver::solve</string >
542
+ </arguments >
543
+ </listener >
544
+ </listeners >
545
+
546
+ The ``My\Namespace\SutSolver::solve `` can be any PHP callable and receives the
547
+ current test classname as its first argument.
548
+
549
+ Finally, the listener can also display warning messages when the SUT solver does
550
+ not find the SUT:
551
+
552
+ .. code-block :: xml
553
+
554
+ <listeners >
555
+ <listener class =" Symfony\Bridge\PhpUnit\CoverageListener" >
556
+ <arguments >
557
+ <null />
558
+ <boolean >true</boolean >
559
+ </arguments >
560
+ </listener >
561
+ </listeners >
562
+
454
563
.. _PHPUnit : https://phpunit.de
455
564
.. _`PHPUnit event listener` : https://phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestListener
456
565
.. _`PHPUnit's assertStringMatchesFormat()` : https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertStringMatchesFormat
0 commit comments