Skip to content

Commit e8c1b8f

Browse files
committed
minor #12330 Inform about how to use new DateTime() when Clock Mocking (benjamintoussaint)
This PR was submitted for the 4.3 branch but it was merged into the 3.4 branch instead (closes #12330). Discussion ---------- Inform about how to use new DateTime() when Clock Mocking <!-- If your pull request fixes a BUG, use the oldest maintained branch that contains the bug (see https://symfony.com/roadmap for the list of maintained branches). If your pull request documents a NEW FEATURE, use the same Symfony branch where the feature was introduced (and `master` for features of unreleased versions). --> Commits ------- 8212a62 Inform about how to use new DateTime() when Clock Mocking
2 parents a9f0078 + 8212a62 commit e8c1b8f

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

components/phpunit_bridge.rst

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,14 @@ Clock Mocking
331331
~~~~~~~~~~~~~
332332

333333
The :class:`Symfony\\Bridge\\PhpUnit\\ClockMock` class provided by this bridge
334-
allows you to mock the PHP's built-in time functions ``time()``,
335-
``microtime()``, ``sleep()`` and ``usleep()``.
334+
allows you to mock the PHP's built-in time functions ``time()``, ``microtime()``,
335+
``sleep()``, ``usleep()`` and ``gmdate()``. Additionally the function ``date()``
336+
is mocked so it uses the mocked time if no timestamp is specified.
337+
338+
This means that you don't need to make a single change in your original code, except when using ``new DateTime()``, which must be replaced by ``DateTime::createFromFormat('U', time())`` to use the mocked ``time()`` function.
339+
340+
Other functions with an optional timestamp parameter that defaults to ``time()``
341+
will still use the system time instead of the mocked time.
336342

337343
To use the ``ClockMock`` class in your test, add the ``@group time-sensitive``
338344
annotation to its class or methods. This annotation only works when executing

0 commit comments

Comments
 (0)