mirror of
https://github.com/silverstripe/silverstripe-behat-extension
synced 2024-10-22 17:05:32 +02:00
Merge pull request #34 from silverstripe-labs/pulls/datetime
NEW Set current date and time through Behat
This commit is contained in:
commit
58499365fb
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user