sheoak
sheoak

Reputation: 91

Pushing requests to a HandlerStack after Guzzle Client has been created

I’m trying to send fake requests — for unit testing — after my Guzzle Client (v6) has been created.

I tried to look in Guzzle classes for a while but I cannot find a way to achieve this. Please note that I'm in a PHPUnit test and can only access the client, sent by another test it depends on.

// 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(…);

Upvotes: 2

Views: 680

Answers (1)

Jak S
Jak S

Reputation: 669

Almost there. For your last line:

$handler->setHandler($newMock);

Upvotes: 2

Related Questions