NEW Set current date and time through Behat

This commit is contained in:
Ingo Schommer 2014-04-16 11:09:18 +12:00
parent d313b4ccce
commit 3b8518e4ab
2 changed files with 51 additions and 0 deletions

View File

@ -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)

View File

@ -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);
}
}