4

I'm currently working on a project in which I create fixtures with Alice-bundle to run tests to make sure my API-endpoints work properly. Everything works fine, except for the DateTime properties.

No matter what string I pass it, eg: <dateTime('2019-09-23 14:00:00')>, it always gives me the wrong date and time, usually something like: 1998-10-25T14:29:45+01:00.

Even using <dateTime('now')> does not work -- it gives me some pre-2000s date & time as well, while that's exactly what some examples I'd found do use.

A fixture could look something like this:

Path\To\Task\Entity: my_task: title: 'My tasks' description: 'These are all the tasks just for me!!!' startsAt: <dateTime('now')> endsAt: <dateTime('now')> createdBy: '@some_higher_user'

Ideally I just want to pass it a string so I can define both a Date and Time and make sure it works properly, in the right format.

And help would be appreciated.

2 Answers 2

5

Looking here https://github.com/nelmio/alice/blob/master/doc/advanced-guide.md#functions we read:

function can be a Faker or a PHP native (or registered in the global scope) function.

So I would recommend trying a PHP native function that creates a \DateTime object

<date_create_from_format ( 'Y-m-d H:i:s' , '2019-09-23 14:00:00')>
// or
<date_create('now')>
2
  • This works nicely and seems a neater solution than passing the two exact same DateTime strings to dateTimeBetween(). Thank you! Sep 24, 2019 at 10:53
  • 2
    Nice. You should mark this as correct answer instead of mine, @TienusMcVinger
    – MigMolRod
    Sep 24, 2019 at 11:31
1

That's how it works. The <dateTime()> function takes an argument called $max. So what it does is create a date between a starting date (not sure which one, probably something in the 1900 range or so) and that $max argument.

I guess you will want to use <dateTimeBetween()> which takes a startDate and an endDate to create a fake date between them. So I suppose if startDate = endDate, then you'll get the desired fixed date.

Take a look at fzaninotto/Faker library documentation. It's the library used by AliceBundle to actually fake data. There you can see what DateTime related functions you can use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.