Skip to content

Commit 3dc74f6

Browse files
committed
minor #14917 [Testing] [PHPUnit-Bridge] Troubleshooting (sylfabre)
This PR was submitted for the 5.4 branch but it was merged into the 4.4 branch instead. Discussion ---------- [Testing] [PHPUnit-Bridge] Troubleshooting This PR documents a new solution to an issue I'm having with ClockMock related to https://bugs.php.net/bug.php?id=76788. I was the original author of the current Troubleshooting section and came across another use-case Commits ------- 8e162de [PHPUnit-Bridge] Troubleshooting
2 parents a1fb993 + 8e162de commit 3dc74f6

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

components/phpunit_bridge.rst

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -789,24 +789,42 @@ namespaces in the ``phpunit.xml`` file, as done for example in the
789789
790790
Under the hood, a PHPUnit listener injects the mocked functions in the tested
791791
classes' namespace. In order to work as expected, the listener has to run before
792-
the tested class ever runs. By default, the mocked functions are created when the
793-
annotation are found and the corresponding tests are run. Depending on how your
794-
tests are constructed, this might be too late. In this case, you will need to declare
795-
the namespaces of the tested classes in your ``phpunit.xml.dist``.
792+
the tested class ever runs.
793+
794+
By default, the mocked functions are created when the annotation are found and
795+
the corresponding tests are run. Depending on how your tests are constructed,
796+
this might be too late.
797+
798+
You can either:
799+
* Declare the namespaces of the tested classes in your ``phpunit.xml.dist``.
800+
* Register the namespaces at the end of the ``config/bootstrap.php`` file.
796801

797802
.. code-block:: xml
798803
799804
<!-- phpunit.xml.dist -->
800-
<!-- ... -->
801-
<listeners>
802-
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
803-
<arguments>
804-
<array>
805-
<element key="time-sensitive"><string>Acme\MyClassTest</string></element>
806-
</array>
807-
</arguments>
808-
</listener>
809-
</listeners>
805+
<phpunit
806+
bootstrap="config/bootstrap.php"
807+
>
808+
<!-- ... -->
809+
<listeners>
810+
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
811+
<arguments>
812+
<array>
813+
<element key="time-sensitive"><string>Acme\MyClassTest</string></element>
814+
</array>
815+
</arguments>
816+
</listener>
817+
</listeners>
818+
</phpunit>
819+
820+
.. code-block:: php
821+
822+
<!-- config/bootstrap.php -->
823+
use Symfony\Bridge\PhpUnit\ClockMock;
824+
825+
if ('test' === $_SERVER['APP_ENV']) {
826+
ClockMock::register('Acme\\MyClassTest\\');
827+
}
810828
811829
Modified PHPUnit script
812830
-----------------------

0 commit comments

Comments
 (0)