【发布时间】:2015-08-28 04:51:32
【问题描述】:
所以,我要实现的是阻止用户登录站点如果当前机器日期与远程机器不同。
我创建了一个验证,用远程服务器的日期时间检查和验证机器。我已经尝试通过手动更改我的机器日期来进行手动测试,一切正常。
问题是,如果我想使用 selenium 测试来测试它怎么办?我想说每次我想运行测试时手动更改我的机器时间很烦人。
这是我的测试函数,其中包含更改默认时区的方法(但是,它不起作用)
public function testLogin1()
{
date_default_timezone_set('America/Anchorage');
$testDate = getdate();
var_dump(date_default_timezone_get());
var_dump($testDate);
$this->login('user', 'pass');
sleep(3);
}
public function testLogin2()
{
date_default_timezone_set('Asia/Bangkok');
$testDate = getdate();
var_dump(date_default_timezone_get());
var_dump($testDate);
$this->login('user', 'pass');
sleep(3);
}
有没有可以设置全局 php DateTime / getdate() 的特定函数?
下面是我的 TestTime 类函数。
public static function isValid($currentDateTime) {
self::initRemoteServerDateTime();
self::initLatestUserActivityDateTime();
if (self::$remoteServerDateTime || self::$latestUserActivityDateTime) {
$refDate = (self::$remoteServerDateTime) ? self::$remoteServerDateTime : self::$latestUserActivityDateTime;
if ($currentDateTime->format('Y-m-d') == $refDate->format('Y-m-d')) {
return true;
}
}
return false;
}
这是我调用 TestTime 函数的操作类。
$currentDateTime = new DateTime();
if (!TestTime::isValid($currentDateTime)) {
$errorMessage = 'Server\'s date and time is not set properly ['. $currentDateTime->format('j M Y H:i') .']. </br> Please contact your system administrator.';
throw new sfValidatorErrorSchema($this, array($this->getOption('username_field') => new sfValidatorError($this, $errorMessage)));
}
当用户单击登录按钮并提交表单时,我的操作类正在验证期间发生。
或者,有什么不同的方法可以做到这一点?
【问题讨论】:
-
你能c/p你想测试的功能吗?
-
@PierreMarichez 我已经为你添加了代码。对于您的额外信息,我正在使用 symfony 框架。
标签: php selenium phpunit symfony1 functional-testing