PHPUnit如何测试回调

时间:2018-04-12 10:12:43

标签: mocking phpunit

如果它是Closure的一个实例,第一个方法将启动Closure调用,它将传递参数,第二个方法resolveByType将格式化输出。

如何在PHPUnit中正确测试?

class Resolver
{

    public function initiate($table)
    {
        if($table->action instanceof \Closure) {

            $return =  call_user_func_array($table->action, $table->params);

            return $this->resolveByType($return);
        }
    }

     /**
     * resolve by returned type 
     *  
     * @param  mixed $data
     * @return void
     */
    public function resolveByType($data)
    {
        switch (gettype($data)) { 

            case 'array':  echo json_encode($data); break;
            case 'object': echo json_encode($data); break;
            default: echo $data; break;
        }
    }
}

0 个答案:

没有答案