Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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 expect调用只有一个参数-更详细地描述参数_Php_Phpunit - Fatal编程技术网

PHPUnit expect调用只有一个参数-更详细地描述参数

PHPUnit expect调用只有一个参数-更详细地描述参数,php,phpunit,Php,Phpunit,我有以下几点: $this->httpClient->expects($this->at(1))->method('send') ->with($this->isInstanceOf(RequestInterface::class)) ->willReturn($responseMock); 因此,with函数调用检查的send方法的参数必须是RequestInterface的实例。但是,我需要更详细地检查此参数

我有以下几点:

    $this->httpClient->expects($this->at(1))->method('send')
        ->with($this->isInstanceOf(RequestInterface::class))
        ->willReturn($responseMock);
因此,with函数调用检查的send方法的参数必须是RequestInterface的实例。但是,我需要更详细地检查此参数:

它需要是作为RequestInterface实例的对象 对象的url属性需要有一个特定的值 对象的方法必须是“GET” 我该怎么做呢?

您可以使用PHPUnit向断言中添加自定义逻辑,例如

$this->httpClient
    ->expects($this->at(1))->method('send')
    ->with($this->callback(function (RequestInterface $request) {
        $this->assertSame('https://some-domain.com', $request->getUri());
        $this->assertSame('GET', $request->getMethod());

        return true;
    }))
    ->willReturn($responseMock);
如果传递的对象被认为是有效的,那么回调应该返回true,但是您也可以在回调中使用本机断言assertSame等-这些断言引发的任何异常都会在测试本身中出现。这里的instanceof check由回调上的type提示处理,因为如果不匹配,将引发TypeError。如果愿意,还可以省略类型提示并手动运行assertInstanceOf


注意:我假设您使用的是here-显然,回调中的方法名称需要更改,否则。

非常有用。非常感谢。该请求符合PSR-7,因此非常适合。