Hey

Am 01.07.24 um 15:07 schrieb Go Kudo:
Hi, Internals.

I've been absent for a long time due to poor health. I'm finally back.

I maintain a legacy application written in PHP, and occasionally need to fake the current time for testing purposes. However, PHP doesn't provide a standard way to fake the current time, so I've been changing the OS's current time to do this, which is quite painful.

This can be avoided by using third-party libraries (such as Carbon::setTestNow()). However, it's almost impossible to modify all parts of a legacy application that depend on the current time.

Another option is to use libfaketime (https://github.com/wolfcw/libfaketime <https://github.com/wolfcw/libfaketime>), but this is also quite painful to use.

Since this was absolutely necessary for my work, I implemented this functionality as a PHP Extension. (Please ignore the dirty implementation related to PDO. I'm not planning to propose it this time.)

https://github.com/colopl/php-colopl_timeshifter <https://github.com/colopl/php-colopl_timeshifter>

However, this Extension has some problems.

The first is that there's no way to determine whether the format passed to the ext-date parser is relative or absolute time, resulting in a dirty hack using usleep. The second is that it depends on timelib, so it breaks when upstream changes related to timelib are made.

So, how about adding a `date_set_test_now(\DateInterval $shiftInterval)` function to ext-date?

This function would treat the current time as shifted by the passed DateInterval. Since it's implemented on the ext-date side, there's no need for dirty hacks using usleep.

I'd like to hear your opinions. Thank yo

Instead of tweaking the PHP sources and open them up another hellhole of things that will go south, I'd ratehr use PSR 20[1] for handling times in application tests.

Also you can already overwrite the `time` function within a namespace for testing purposes and IIRC there is a way to do that with Carbon.

Just my 0.02 €

Cheers

Andreas

[https://www.php-fig.org/psr/psr-20/]

--
                                                               ,,,
                                                              (o o)
+---------------------------------------------------------ooO-(_)-Ooo-+
| Andreas Heigl                                                       |
| mailto:andr...@heigl.org                  N 50°22'59.5" E 08°23'58" |
| https://andreas.heigl.org                                           |
+---------------------------------------------------------------------+
| https://hei.gl/appointmentwithandreas                               |
+---------------------------------------------------------------------+
| GPG-Key: https://hei.gl/keyandreasheiglorg                          |
+---------------------------------------------------------------------+

Reply via email to