Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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
Post 在不发送或接收HTTP请求的情况下测试HTTP参数_Post_Get_Phpunit - Fatal编程技术网

Post 在不发送或接收HTTP请求的情况下测试HTTP参数

Post 在不发送或接收HTTP请求的情况下测试HTTP参数,post,get,phpunit,Post,Get,Phpunit,我正在使用PHPUnit测试一个函数,该函数检查是否设置了任何GET POST参数。 如果设置为Set,则返回null。我不知道如何测试它,因为我没有设置任何http请求。功能如下: public static function getURIParameter($param, $defaultValue=null) { if(isset($_REQUEST[$param]) && is_array($_REQUEST[$param])) return $

我正在使用PHPUnit测试一个函数,该函数检查是否设置了任何GET POST参数。 如果设置为Set,则返回null。我不知道如何测试它,因为我没有设置任何http请求。功能如下:

 public static function getURIParameter($param, $defaultValue=null) {
    if(isset($_REQUEST[$param]) && is_array($_REQUEST[$param]))
        return $_REQUEST[$param];
    else
        return isset($_REQUEST[$param]) ? trim($_REQUEST[$param]) : $defaultValue;
}
这是我的测试函数,有一些伪代码

     public function testGetURIParameter()
{   
    $_POST['parameter'] = true; // is something like that possible?
    $this->assertSame('POST', Core::getURIParameter('parameter'));

} 

有人能告诉我如何测试这个函数吗

首先,您应该将代码分为逻辑层和传输层。然后使用模式将传输注入逻辑:

<?php

interface TransportInterface {

    public function request($url, $params, $method);

}

class Logic {

    public function __construct(TransportInterface $transport) {
        $this->transport = $transport;
    }
}

查看模拟对象以伪造您想要返回到测试功能的内容。