Skip to content

Commit 81365f3

Browse files
committed
[PHPUnitBridge] Added docs for the CoverageListener
1 parent 103bdc2 commit 81365f3

File tree

1 file changed

+110
-2
lines changed

1 file changed

+110
-2
lines changed

components/phpunit_bridge.rst

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ Modified PHPUnit script
415415
-----------------------
416416

417417
.. 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
419419
this component.
420420

421421
This bridge provides a modified version of PHPUnit that you can call by using
@@ -448,9 +448,117 @@ If you have installed the bridge through Composer, you can run it by calling e.g
448448

449449
.. tip::
450450

451-
If you still need to use ``prophecy`` (but not ``symfony/yaml``),
451+
If you still need to use ``prophecy`` (but not ``symfony/yaml``),
452452
then set the ``SYMFONY_PHPUNIT_REMOVE`` env var to ``symfony/yaml``.
453453

454+
455+
Code coverage listener
456+
----------------------
457+
458+
Use case
459+
~~~~~~~~
460+
461+
By default the code coverage is computed with the following rule: If a line of
462+
code is executed then it is marked as covered. And the test who executes a line
463+
of code is therefore marked as "covering the line of code".
464+
465+
This can be misleading. Considering the following example::
466+
467+
class Bar
468+
{
469+
public function barZ()
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 fooZ()
485+
{
486+
$this->bar->barZ();
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->barZ());
500+
}
501+
}
502+
503+
504+
Here the ``FooTest::test`` will execute every lines of code so the code coverage
505+
will be 100%. But the ``Bar`` class is not tested.
506+
507+
The ``CoverageListener`` aim to fix this behavior by adding the ``@covers``
508+
annotation on the test class. If an annotation already exist then the listener
509+
do nothing.
510+
511+
By default the listener try to find the tested class by removing the ``Test`` part
512+
of the classname: ``My\Namespace\Tests\FooTest`` -> ``My\Namespace\Foo``.
513+
514+
515+
Installation
516+
~~~~~~~~~~~~
517+
518+
Add the following configuration to the ``phpunit.xml.dist`` file
519+
520+
.. code-block:: xml
521+
522+
<!-- http://phpunit.de/manual/6.0/en/appendixes.configuration.html -->
523+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
524+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
525+
>
526+
527+
<!-- ... -->
528+
529+
<listeners>
530+
<listener class="Symfony\Bridge\PhpUnit\CoverageListener" />
531+
</listeners>
532+
</phpunit>
533+
534+
You can also configure a new 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`` should be a callable and will receive the
547+
current test classname as first argument.
548+
549+
Finally the listener can add warning when the SUT solver does not find the SUT:
550+
551+
.. code-block:: xml
552+
553+
<listeners>
554+
<listener class="Symfony\Bridge\PhpUnit\CoverageListener">
555+
<arguments>
556+
<null/>
557+
<boolean>true</boolean>
558+
</arguments>
559+
</listener>
560+
</listeners>
561+
454562
.. _PHPUnit: https://phpunit.de
455563
.. _`PHPUnit event listener`: https://phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestListener
456564
.. _`PHPUnit's assertStringMatchesFormat()`: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertStringMatchesFormat

0 commit comments

Comments
 (0)