Skip to content

[PHPUnitBridge] Added docs for the CoverageListener #8416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 29, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 110 additions & 2 deletions components/phpunit_bridge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ Modified PHPUnit script
-----------------------

.. versionadded:: 3.2
This modified PHPUnit script was introduced in the 3.2 version of
This modified PHPUnit script was introduced in the 3.2 version of
this component.

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

.. tip::

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


Code coverage listener
----------------------

Use case
~~~~~~~~

By default the code coverage is computed with the following rule: If a line of
code is executed then it is marked as covered. And the test who executes a line
of code is therefore marked as "covering the line of code".

This can be misleading. Considering the following example::

class Bar
{
public function barZ()
{
return 'bar';
}
}

class Foo
{
private $bar;

public function __construct(Bar $bar)
{
$this->bar = $bar;
}

public function fooZ()
{
$this->bar->barZ();

return 'bar';
}
}

class FooTest extends PHPUnit\Framework\TestCase
{
public function test()
{
$bar = new Bar();
$foo = new Foo($bar);

$this->assertSame('bar', $foo->barZ());
}
}


Here the ``FooTest::test`` will execute every lines of code so the code coverage
will be 100%. But the ``Bar`` class is not tested.

The ``CoverageListener`` aim to fix this behavior by adding the ``@covers``
annotation on the test class. If an annotation already exist then the listener
do nothing.

By default the listener try to find the tested class by removing the ``Test`` part
of the classname: ``My\Namespace\Tests\FooTest`` -> ``My\Namespace\Foo``.


Installation
~~~~~~~~~~~~

Add the following configuration to the ``phpunit.xml.dist`` file

.. code-block:: xml

<!-- http://phpunit.de/manual/6.0/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
>

<!-- ... -->

<listeners>
<listener class="Symfony\Bridge\PhpUnit\CoverageListener" />
</listeners>
</phpunit>

You can also configure a new System Under Test solver:

.. code-block:: xml

<listeners>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the whole code block needs to be indented by additional four spaces

<listener class="Symfony\Bridge\PhpUnit\CoverageListener">
<arguments>
<string>My\Namespace\SutSolver::solve</string>
</arguments>
</listener>
</listeners>

The ``My\Namespace\SutSolver::solve`` should be a callable and will receive the
current test classname as first argument.

Finally the listener can add warning when the SUT solver does not find the SUT:

.. code-block:: xml

<listeners>
<listener class="Symfony\Bridge\PhpUnit\CoverageListener">
<arguments>
<null/>
<boolean>true</boolean>
</arguments>
</listener>
</listeners>

.. _PHPUnit: https://phpunit.de
.. _`PHPUnit event listener`: https://phpunit.de/manual/current/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestListener
.. _`PHPUnit's assertStringMatchesFormat()`: https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertStringMatchesFormat
Expand Down