From fb0348366ab28d174e772f19f47fb63f1236b0eb Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 25 Nov 2023 13:42:44 +0900 Subject: [PATCH] docs: add missing Time::setTestNow() --- user_guide_src/source/testing/overview.rst | 17 +++++++++++++ .../source/testing/overview/021.php | 24 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 user_guide_src/source/testing/overview/021.php diff --git a/user_guide_src/source/testing/overview.rst b/user_guide_src/source/testing/overview.rst index d969f73cdc41..2dd63d4eed9e 100644 --- a/user_guide_src/source/testing/overview.rst +++ b/user_guide_src/source/testing/overview.rst @@ -267,6 +267,23 @@ component name: .. note:: All component Factories are reset by default between each test. Modify your test case's ``$setUpMethods`` if you need instances to persist. +Testing and Time +================ + +Testing time-dependent code can be challenging. However, when using the +:doc:`Time <../libraries/time>` class, the current time can be fixed or changed +at will during testing. + +Below is a sample test code that fixes the current time: + +.. literalinclude:: overview/021.php + +You can fix the current time with the ``Time::setTestNow()`` method. +Optionally, you can specify a locale as the second parameter. + +Don't forget to reset the current time after the test with calling it without +parameters. + .. _testing-cli-output: Testing CLI Output diff --git a/user_guide_src/source/testing/overview/021.php b/user_guide_src/source/testing/overview/021.php new file mode 100644 index 000000000000..7a8c7a026afd --- /dev/null +++ b/user_guide_src/source/testing/overview/021.php @@ -0,0 +1,24 @@ +assertSame('2023-11-25 12:00:00', (string) Time::now()); + } +}