Testing 如何在behat中测试当前日期条件?

Testing 如何在behat中测试当前日期条件?,testing,symfony4,behat,Testing,Symfony4,Behat,我有一个基于当前日期的特性,问题是在场景中编写if条件是否是一个好的解决方案。简单示例:如果测试日期等于当前日期,则其他字段等于0,否则等于10。也许symfony 4中有模拟当前日期时间的库。您可以编写如下步骤: Then I should see sysdate with format "Y-m-d" in the "example" field 那么在你的上下文中: public function iShouldSeeSysdateWithFormatInTheField(string

我有一个基于当前日期的特性,问题是在场景中编写if条件是否是一个好的解决方案。简单示例:如果测试日期等于当前日期,则其他字段等于0,否则等于10。也许symfony 4中有模拟当前日期时间的库。

您可以编写如下步骤:

Then I should see sysdate with format "Y-m-d" in the "example" field
那么在你的上下文中:

public function iShouldSeeSysdateWithFormatInTheField(string $format, string $key)
{
    $field = $this->getSession()->getPage()->findField($key);
    $value = (new DateTime())->format($format);

    if (is_null($field->getValue())) {
        assertEmpty($value);
    }

    if($value != $field->getValue()) {
        throw new Exception('date does not match');
    }
}

请更具体一点,因为不太清楚您试图解决什么问题,并添加您尝试过的内容。