Set date (x)days from now in Behat with Symfony2 forms

Using Behat i needed to be able to set a date in a form (x) number of days from the current date. This however is a problem as Behat cannot set a date (x) number of days from now as it does not know the current date. So a simple solution is to create a Behat step definition that takes the field name and a number of days. My step definition looks something like this:

The reason i went with a CSS selector is because Behat supports this anyway, and Symfony2 can create a date form by splitting the field into 3 separate fields, for Month/Day and Year respectively. So we will need to identify 3 fields in our form. Symfony2 will label them with each with the overall field name with an additional underscore and designation. Each field will be a select field.

For example, if my field is called Post_unlockedUntilDate (which was a real field i needed this test case for [the Post_ designation came from symfony based on the entity the form related to), then the 3 fields i would need to find (as created by symfony) would be:

  • Post_unlockedUntilDate_year
  • Post_unlockedUntilDate_month
  • Post_unlockedUntilDate_day

So i created a step definition that takes the CSS and finds the fields by the given selector. Then identifies each field in the results and assigns them to an array with a key for ‘day’, ‘month’ and ‘year’. Then all we need to do is create a new datetime object set (x) number of days from now and then apply that to each field.

So to use it we just call:

Hope this helps. Good luck.

2 thoughts on Set date (x)days from now in Behat with Symfony2 forms

  1. Nice, one little remark “([^”]*)” allows an empty value which is not what you want.

    And you can extend this this even further to:
    ^I select from "([^"]+)" a date "([^"]+)" from "([^"]+)"$

    Which allows for a more complex pattern and starting point.
    Just some idea’s.

  2. Thanks Sebastian, so if we changed now to ‘now’ as a parameter, then it might be even better if we did this:

    ^I select from “([^”]+)” a date “([^”]+)”

    where by the whole string for the date object constructor would look like; ‘now + 2 days’ so we would use it like:

    ^I select from “select[id^=Entity_field]” a date “now + 2 days”

    That would be pretty flexible.

    Thanks for the ideas/input Sebastian.

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">