Skip to content

[Testing] [PHPUnit-Bridge] Troubleshooting #14917

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 1 commit into from
May 25, 2021
Merged
Changes from all commits
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
46 changes: 32 additions & 14 deletions components/phpunit_bridge.rst
Original file line number Diff line number Diff line change
Expand Up @@ -789,24 +789,42 @@ namespaces in the ``phpunit.xml`` file, as done for example in the

Under the hood, a PHPUnit listener injects the mocked functions in the tested
classes' namespace. In order to work as expected, the listener has to run before
the tested class ever runs. By default, the mocked functions are created when the
annotation are found and the corresponding tests are run. Depending on how your
tests are constructed, this might be too late. In this case, you will need to declare
the namespaces of the tested classes in your ``phpunit.xml.dist``.
the tested class ever runs.

By default, the mocked functions are created when the annotation are found and
the corresponding tests are run. Depending on how your tests are constructed,
this might be too late.

You can either:
* Declare the namespaces of the tested classes in your ``phpunit.xml.dist``.
* Register the namespaces at the end of the ``config/bootstrap.php`` file.

.. code-block:: xml

<!-- phpunit.xml.dist -->
<!-- ... -->
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
<arguments>
<array>
<element key="time-sensitive"><string>Acme\MyClassTest</string></element>
</array>
</arguments>
</listener>
</listeners>
<phpunit
bootstrap="config/bootstrap.php"
>
<!-- ... -->
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
<arguments>
<array>
<element key="time-sensitive"><string>Acme\MyClassTest</string></element>
</array>
</arguments>
</listener>
</listeners>
</phpunit>

.. code-block:: php

<!-- config/bootstrap.php -->
use Symfony\Bridge\PhpUnit\ClockMock;

if ('test' === $_SERVER['APP_ENV']) {
ClockMock::register('Acme\\MyClassTest\\');
}

Modified PHPUnit script
-----------------------
Expand Down