pock

Responses and failures

Static responses

reply() returns a PockResponseBuilder . It can set status, replace or append headers, and write a body from a string, resource, PSR-7 stream, or file:

$pock->matchUri('https://api.example.com/report')
    ->reply(202)
    ->withHeaders([
        'Content-Type' => 'text/plain',
        'X-Job' => '42',
    ])
    ->withBody('queued');

withHeader() replaces the named header; withAddedHeader() appends another value. withJson() and withXml() serialize structured bodies but do not add a content type automatically.

Use an existing PSR-7 response when exact identity or a specialized response implementation matters:

$response = $psr17Factory->createResponse(204);
$pock->matchUri('https://api.example.com/cache')->replyWith($response);

Dynamic responses

A callback receives the matched request and a fresh response builder:

use Pock\PockResponseBuilder;
use Psr\Http\Message\RequestInterface;

$pock->matchPath('/echo')->replyWithCallback(
    static function (RequestInterface $request, PockResponseBuilder $response) {
        return $response->withStatusCode(200)
            ->withBody((string) $request->getBody())
            ->getResponse();
    }
);

Reusable dynamic responders implement Pock\Factory\ReplyFactoryInterface and are registered with replyWithFactory() .

Failures

Use the convenience methods to exercise PSR-18 error handling:

$pock->matchUri('https://api.example.com/offline')
    ->throwNetworkException('Connection refused');

throwClientException() , throwNetworkException() , and throwRequestException() create the corresponding PSR-18 exception type. throwException() accepts any Throwable . Request-aware pock exceptions receive the matched request before being thrown.

Delegating requests

replyWithClient() delegates a matched request to another PSR-18 client. setFallbackClient() delegates only requests for which no mock matches:

$pock->matchHost('sandbox.example.com')->replyWithClient($realClient);
$pock->setFallbackClient($realClient);

Both features can perform real network traffic; use them deliberately in integration tests.

Search results