@@ -809,24 +809,42 @@ namespaces in the ``phpunit.xml`` file, as done for example in the
809
809
810
810
Under the hood, a PHPUnit listener injects the mocked functions in the tested
811
811
classes' namespace. In order to work as expected, the listener has to run before
812
- the tested class ever runs. By default, the mocked functions are created when the
813
- annotation are found and the corresponding tests are run. Depending on how your
814
- tests are constructed, this might be too late. In this case, you will need to declare
815
- the namespaces of the tested classes in your ``phpunit.xml.dist ``.
812
+ the tested class ever runs.
813
+
814
+ By default, the mocked functions are created when the annotation are found and
815
+ the corresponding tests are run. Depending on how your tests are constructed,
816
+ this might be too late.
817
+
818
+ You can either:
819
+ * Declare the namespaces of the tested classes in your ``phpunit.xml.dist ``.
820
+ * Register the namespaces at the end of the ``config/bootstrap.php `` file.
816
821
817
822
.. code-block :: xml
818
823
819
824
<!-- phpunit.xml.dist -->
820
- <!-- ... -->
821
- <listeners >
822
- <listener class =" Symfony\Bridge\PhpUnit\SymfonyTestsListener" >
823
- <arguments >
824
- <array >
825
- <element key =" time-sensitive" ><string >Acme\MyClassTest</string ></element >
826
- </array >
827
- </arguments >
828
- </listener >
829
- </listeners >
825
+ <phpunit
826
+ bootstrap =" config/bootstrap.php"
827
+ >
828
+ <!-- ... -->
829
+ <listeners >
830
+ <listener class =" Symfony\Bridge\PhpUnit\SymfonyTestsListener" >
831
+ <arguments >
832
+ <array >
833
+ <element key =" time-sensitive" ><string >Acme\MyClassTest</string ></element >
834
+ </array >
835
+ </arguments >
836
+ </listener >
837
+ </listeners >
838
+ </phpunit >
839
+
840
+ .. code-block ::
841
+
842
+ <!-- config/bootstrap.php -->
843
+ use Symfony\Bridge\PhpUnit\ClockMock;
844
+
845
+ if ('test' === $_SERVER['APP_ENV']) {
846
+ ClockMock::register('Acme\\MyClassTest\\');
847
+ }
830
848
831
849
Modified PHPUnit script
832
850
-----------------------
0 commit comments