Symfony中的PHPUnit会在灯具中缓存时间,打破基于时间的测试

Some of my tests rely on times, and for simplicity I use offsets from "now".

I run all my fixtures in the set up for all tests. So when creating objects, I will do something like this:

$start_time = Carbon::now('UTC');
if(($state === 'scheduled') || ($state === 'sent')) {
   $start_time->addHours(5);
} elseif ($state === 'complete') {
   $start_time->subMinutes(5);
}
$entity->setStartTime($start_time);
$manager->persist($entity);

The problem is, the "now" always seems to be based on the last time I cleared the test cache, or updated the php file the fixtures came from. So when I run the tests over a few minutes and log both the object timestamp and "now" within the test, I get this (when run three times over four minutes):

[2015-09-17 09:40:07] app.INFO: TIME START: 2015-09-17 04:40:06 [] []
[2015-09-17 09:40:07] app.INFO: TIME NOW: 2015-09-16 23:40:07 [] []

[2015-09-17 09:40:24] app.INFO: TIME START: 2015-09-17 04:40:06 [] []
[2015-09-17 09:40:24] app.INFO: TIME NOW: 2015-09-16 23:40:24 [] []

[2015-09-17 09:44:57] app.INFO: TIME START: 2015-09-17 04:40:06 [] []
[2015-09-17 09:44:57] app.INFO: TIME NOW: 2015-09-16 23:44:57 [] []

This is less than ideal behaviour, as it will start causing tests to fail as the cache gets past five hours old.

It would be nice to have my fixtures based on current times rather than some explicitly specified fixed date that gets older and older, even though that would be a workaround.

I also don't really want to use Carbon::setTestNow($someFixedDate) in the test setup function as I like the idea that my tests get run at all sorts of different times without explicitly specifying times.

Ideally, when I run the tests, the fixtures should actually behave as you would expect them to - using the actual current time to create objects.

I'm using Symfony 2.7, with PHPUnit 4.4.5.