mocking - 创建 Guzzle 客户端后将请求推送到 HandlerStack

标签 mocking phpunit handler guzzle

在创建我的 Guzzle 客户端 (v6) 后,我尝试发送虚假请求(用于单元测试)。

我尝试在 Guzzle 类中查找一段时间,但找不到实现此目的的方法。请注意,我正在进行 PHPUnit 测试,并且只能访问由它所依赖的另一个测试发送的客户端

// Create a mock and queue two responses.
$mock = new MockHandler([
    new Response(200, ['X-Foo' => 'Bar']),
    new Response(202, ['Content-Length' => 0])
]);

$handler = HandlerStack::create($mock);
$client = new Client(['handler' => $handler]);

// New requests after the client has been initiated
$newMock = new MockHandler([
    new Response(200, ['X-Foo' => 'Bar']),
    new Response(202, ['Content-Length' => 0])
]);

// useful?
$config  = $client->getConfig();
$handler = $config['handler'];

// push the new requests in $newMock?
$handler->something(…);

最佳答案

快到了。对于你的最后一行:

$handler->setHandler($newMock);

关于mocking - 创建 Guzzle 客户端后将请求推送到 HandlerStack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291709/

相关文章:

php - 应该如何使用 phpunit 测试 xss + sql 注入(inject)?

php - 使用 withConsecutive() 测试时出现问题

perl - 如何读写文件,语法错误

android - 无法使用 Mockito 模拟 WebView

javascript - 如何在 JavaScript 中真实地为模拟事件生成时间戳

php - 交响乐 4 : WebTestCase fails (Failed to start the session)

mysql - 在函数返回值到过程 mysql 之前清除处理程序

Android:具有多个 UI Activity (多线程/处理程序)的蓝牙 SPP 应用程序的一般方向

python - 在 Nose 下测试 Python 代码时,我应该如何验证日志消息?

python - 如何模拟 django 信号处理程序?