From 3b8518e4abdd2666daabc9c87d776d20ef52e75b Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Wed, 16 Apr 2014 11:09:18 +1200 Subject: [PATCH] NEW Set current date and time through Behat --- README.md | 5 ++ .../Context/SilverStripeContext.php | 46 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/README.md b/README.md index 34c77ca..66eef02 100644 --- a/README.md +++ b/README.md @@ -648,6 +648,11 @@ It's based on the `vendor/bin/behat -di @cms` output. - Example: Given a "group" "Admin" with permissions "Access to 'Pages' section" and "Access to 'Files' section" # SilverStripe\Cms\Test\Behaviour\FixtureContext::stepCreateGroupWithPermissions() +### Environment + + Given /^the current date is "([^"]*)"$/ + Given /^the current time is "([^"]*)"$/ + ### Transformations Behat [transformations](http://docs.behat.org/guides/2.definitions.html#step-argument-transformations) diff --git a/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php b/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php index d58e913..0bbe77b 100644 --- a/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php +++ b/src/SilverStripe/BehatExtension/Context/SilverStripeContext.php @@ -366,4 +366,50 @@ class SilverStripeContext extends MinkContext implements SilverStripeAwareContex ); } + /** + * Sets the current date. Relies on the underlying functionality using + * {@link SS_Datetime::now()} rather than PHP's system time methods like date(). + * Supports ISO fomat: Y-m-d + * Example: Given the current date is "2009-10-31" + * + * @Given /^the current date is "([^"]*)"$/ + */ + public function givenTheCurrentDateIs($date) { + $newDatetime = \DateTime::createFromFormat('Y-m-d', $date); + if(!$newDatetime) { + throw new InvalidArgumentException(sprintf('Invalid date format: %s (requires "Y-m-d")', $date)); + } + + $state = $this->testSessionEnvironment->getState(); + $oldDatetime = \DateTime::createFromFormat('Y-m-d H:i:s', isset($state->datetime) ? $state->datetime : null); + if($oldDatetime) { + $newDatetime->setTime($oldDatetime->format('H'), $oldDatetime->format('i'), $oldDatetime->format('s')); + } + $state->datetime = $newDatetime->format('Y-m-d H:i:s'); + $this->testSessionEnvironment->applyState($state); + } + + /** + * Sets the current time. Relies on the underlying functionality using + * {@link \SS_Datetime::now()} rather than PHP's system time methods like date(). + * Supports ISO fomat: H:i:s + * Example: Given the current time is "20:31:50" + * + * @Given /^the current time is "([^"]*)"$/ + */ + public function givenTheCurrentTimeIs($time) { + $newDatetime = \DateTime::createFromFormat('H:i:s', $date); + if(!$newDatetime) { + throw new InvalidArgumentException(sprintf('Invalid date format: %s (requires "H:i:s")', $date)); + } + + $state = $this->testSessionEnvironment->getState(); + $oldDatetime = \DateTime::createFromFormat('Y-m-d H:i:s', isset($state->datetime) ? $state->datetime : null); + if($oldDatetime) { + $newDatetime->setDate($oldDatetime->format('Y'), $oldDatetime->format('m'), $oldDatetime->format('d')); + } + $state->datetime = $newDatetime->format('Y-m-d H:i:s'); + $this->testSessionEnvironment->applyState($state); + } + }