Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Phpunit,如何模拟连接到时间、周期的事件?_Php_Phpunit - Fatal编程技术网

Phpunit,如何模拟连接到时间、周期的事件?

Phpunit,如何模拟连接到时间、周期的事件?,php,phpunit,Php,Phpunit,假设我有一个数据库记录: | ID | GAME_NAME | CREATED | TIMETOLIVEINSECONDS 1 game1 11:11:00 40 2 game2 11:11:00 50 3 game3 11:11:00 60 4 game4 11:11:00 70 假设现在的时间是11:11:30,10,20

假设我有一个数据库记录:

| ID | GAME_NAME | CREATED  | TIMETOLIVEINSECONDS
  1      game1     11:11:00           40
  2      game2     11:11:00           50
  3      game3     11:11:00           60
  4      game4     11:11:00           70

假设现在的时间是
11:11:30
10
20
30
40
秒,当这个时间过去后,一个自动算法会删除记录。所以我必须确保这些游戏真的会消失,我的问题是,如何在测试中模仿它?我不认为有一个计划周密的
sleep()
函数:)

就可以创建一个提供当前时间的类。基本上是时间()语句的包装器

然后,在测试中,您可以模拟该类或创建一个子类,该子类将根据需要工作

在实际代码中,必须将该类作为参数传递,或通过ServiceFactory创建,或使用允许您从测试中设置时间提供程序类的任何其他方法


如果您选择子类,这里有一个示例。假设您有一个带有getCurrentTimestamp方法的TimeProvider类:

在测试中,您将使用以下子类:

class ConfigurableTimeProvider extends \libraries\time\TimeProvider
{
    private $current_timestamp = null;

    public function getCurrentTimestamp()
    {
        if (!$this->current_timestamp) {
            $this->current_timestamp = parent::getCurrentTimestamp();
        }
        return $this->current_timestamp;
    }

    public function setCurrentTimestamp($timestamp)
    {
        $this->current_timestamp = $timestamp;
    }

    public function applyOffset($offset)
    {
        $this->setCurrentTimestamp($this->getCurrentTimestamp() + $offset);
    }
}

applyOffset对于您需要的测试特别有用,因为它允许模拟时间流逝,因此测试变得非常清晰。

您可以创建一个类来提供当前时间。基本上是时间()语句的包装器

然后,在测试中,您可以模拟该类或创建一个子类,该子类将根据需要工作

在实际代码中,必须将该类作为参数传递,或通过ServiceFactory创建,或使用允许您从测试中设置时间提供程序类的任何其他方法


如果您选择子类,这里有一个示例。假设您有一个带有getCurrentTimestamp方法的TimeProvider类:

在测试中,您将使用以下子类:

class ConfigurableTimeProvider extends \libraries\time\TimeProvider
{
    private $current_timestamp = null;

    public function getCurrentTimestamp()
    {
        if (!$this->current_timestamp) {
            $this->current_timestamp = parent::getCurrentTimestamp();
        }
        return $this->current_timestamp;
    }

    public function setCurrentTimestamp($timestamp)
    {
        $this->current_timestamp = $timestamp;
    }

    public function applyOffset($offset)
    {
        $this->setCurrentTimestamp($this->getCurrentTimestamp() + $offset);
    }
}

applyOffset对于您需要的测试特别有用,因为它允许模拟时间流逝,因此测试变得非常清晰。

您可以创建一个类来提供当前时间。基本上是时间()语句的包装器

然后,在测试中,您可以模拟该类或创建一个子类,该子类将根据需要工作

在实际代码中,必须将该类作为参数传递,或通过ServiceFactory创建,或使用允许您从测试中设置时间提供程序类的任何其他方法


如果您选择子类,这里有一个示例。假设您有一个带有getCurrentTimestamp方法的TimeProvider类:

在测试中,您将使用以下子类:

class ConfigurableTimeProvider extends \libraries\time\TimeProvider
{
    private $current_timestamp = null;

    public function getCurrentTimestamp()
    {
        if (!$this->current_timestamp) {
            $this->current_timestamp = parent::getCurrentTimestamp();
        }
        return $this->current_timestamp;
    }

    public function setCurrentTimestamp($timestamp)
    {
        $this->current_timestamp = $timestamp;
    }

    public function applyOffset($offset)
    {
        $this->setCurrentTimestamp($this->getCurrentTimestamp() + $offset);
    }
}

applyOffset对于您需要的测试特别有用,因为它允许模拟时间流逝,因此测试变得非常清晰。

您可以创建一个类来提供当前时间。基本上是时间()语句的包装器

然后,在测试中,您可以模拟该类或创建一个子类,该子类将根据需要工作

在实际代码中,必须将该类作为参数传递,或通过ServiceFactory创建,或使用允许您从测试中设置时间提供程序类的任何其他方法


如果您选择子类,这里有一个示例。假设您有一个带有getCurrentTimestamp方法的TimeProvider类:

在测试中,您将使用以下子类:

class ConfigurableTimeProvider extends \libraries\time\TimeProvider
{
    private $current_timestamp = null;

    public function getCurrentTimestamp()
    {
        if (!$this->current_timestamp) {
            $this->current_timestamp = parent::getCurrentTimestamp();
        }
        return $this->current_timestamp;
    }

    public function setCurrentTimestamp($timestamp)
    {
        $this->current_timestamp = $timestamp;
    }

    public function applyOffset($offset)
    {
        $this->setCurrentTimestamp($this->getCurrentTimestamp() + $offset);
    }
}
applyOffset对于您需要的测试特别有用,因为它允许模拟时间流逝,因此测试变得非常清晰