pock

Extension points

Custom request matcher

Implement RequestMatcherInterface and add it to a builder:

use Pock\Matchers\RequestMatcherInterface;
use Psr\Http\Message\RequestInterface;

final class SignedRequestMatcher implements RequestMatcherInterface
{
    public function matches(RequestInterface $request): bool
    {
        return hash_equals('expected', $request->getHeaderLine('X-Signature'));
    }
}

$pock->addMatcher(new SignedRequestMatcher())->reply(204);

Custom reply factory

Implement ReplyFactoryInterface when dynamic response logic should be reusable or stateful:

use Pock\Factory\ReplyFactoryInterface;
use Pock\PockResponseBuilder;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class EchoReplyFactory implements ReplyFactoryInterface
{
    public function createReply(
        RequestInterface $request,
        PockResponseBuilder $responseBuilder
    ): ResponseInterface {
        return $responseBuilder->withBody((string) $request->getBody())->getResponse();
    }
}

$pock->replyWithFactory(new EchoReplyFactory());

Custom serializer

A serializer adapter converts a value to a string. Register separate instances for JSON and XML when their formats differ:

use Pock\Factory\JsonSerializerFactory;
use Pock\Serializer\SerializerInterface;

final class ProjectJsonSerializer implements SerializerInterface
{
    public function serialize($data): string
    {
        return ProjectEncoder::encode($data);
    }
}

JsonSerializerFactory::setSerializer(new ProjectJsonSerializer());

Passing null to a serializer factory's setSerializer() restores automatic discovery for future factory calls. Configure serializers before constructing JSON or XML mocks so cached serializer instances do not retain an earlier choice.

Search results