Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f4b7922

Browse filesBrowse files
committed
feature #8416 [PHPUnitBridge] Added docs for the CoverageListener (lyrixx, javiereguiluz)
This PR was merged into the 3.4 branch. Discussion ---------- [PHPUnitBridge] Added docs for the CoverageListener For symfony/symfony#23149 Commits ------- c21ab16 Fixed code indentation e78804b Minor rewords 81365f3 [PHPUnitBridge] Added docs for the CoverageListener
2 parents 0bcf50b + c21ab16 commit f4b7922
Copy full SHA for f4b7922

File tree

1 file changed

+111
-2
lines changed
Filter options

1 file changed

+111
-2
lines changed

‎components/phpunit_bridge.rst

Copy file name to clipboardExpand all lines: components/phpunit_bridge.rst
+111-2Lines changed: 111 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,118 @@ 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+
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+
454563
.. _PHPUnit: https://phpunit.de
455564
.. _`PHPUnit event listener`: https://phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestListener
456565
.. _`PHPUnit's assertStringMatchesFormat()`: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertStringMatchesFormat

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.