在创建Guzzle客户端之后将请求推送到HandlerStack

时间:2015-12-15 14:26:46

标签: 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(…);

1 个答案:

答案 0 :(得分:2)

几乎就在那里。对于你的最后一行:

$handler->setHandler($newMock);