Create sane start and end dates with nelmio\alice

1.3k views Asked by At

I want to create a completedDatetime that follows the datetime in the orderDatetime field.

Fixtures.yml

directive_{200..500}:
    orderDatetime: <dateTimeThisYear()>
    completedDatetime: '90%? <dateTimeBetween("orderDatetime", "now")>'

I used the code above in my fixtures file and got the data that follows.

enter image description here

Is there a way to ensure a sane result using faker data short of writing custom functions in LoadFixtures??

1

There are 1 answers

0
Titenis On

Since you are passing string "orderDatetime" to strtotime() function it returns 1970 year and your dateTimeBetween works like dateTimeBetween('1970', 'now'). Passing of variables is done with $orderDatetime. But if you pass such variable to dateTimeBetween(), the future dateTime might get passed which is not possible.

It only works if your order date is the past from now:

orderDatetime: '<dateTimeBetween("-200 days", "now")>'
completedDatetime: '90%? <dateTimeBetween($orderDatetime, "now")>'

If you wonna generate future dates then you need to create custom faker function for example:

public function dateTimeBetweenAfter($dateFrom, $dateTo)
{
    $date = new DateTime($dateFrom->format('Y-m-d H:i:s'));
    $dateTo = $date->modify($dateTo);

    return FakerDateTime::dateTimeBetween($dateFrom, $dateTo);
}

and use it like this:

orderDatetime: <dateTimeThisYear()>
completedDatetime: '90%? <dateTimeBetweenAfter($orderDatetime, "+1 year")>'