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.

Tienus McVinger
  • 467
  • 9
  • 24

2 Answers2

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')>
Arleigh Hix
  • 9,990
  • 1
  • 14
  • 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.

MigMolRod
  • 388
  • 1
  • 4
  • 12