Request matching
Matchers on the same mock are combined with AND. Use only the conditions relevant to the behavior under test; matching every transport-generated header usually makes tests brittle.
Method and URI
Match the complete URI when it is stable:
$pock->matchMethod(RequestMethod::POST)
->matchUri('https://api.example.com:8443/v1/users?notify=1');
Or match URI components separately:
$pock->matchScheme(RequestScheme::HTTPS)
->matchHost('api.example.com')
->matchPort(8443)
->matchPath('/v1/users')
->matchQuery(['notify' => '1']);
matchOrigin()
adds the scheme, host, and explicit port found in one origin string. Path matching treats a leading slash as optional, but a trailing slash remains significant.
Partial and exact matching
matchHeader()
, matchHeaders()
, matchQuery()
, and matchFormData()
require the expected values but allow additional values. Their matchExact*()
variants reject unexpected values.
$pock->matchHeader('Authorization', 'Bearer secret')
->matchHeaders([
'Accept' => 'application/json',
'X-Tenant' => 'acme',
])
->matchQuery(['page' => 2]);
Use matchHeaderLine()
when the unparsed comma-separated header line matters. matchHeaderLineRegexp()
accepts a PCRE pattern for that line.
Bodies
matchBody()
compares the complete body from a string, resource, or PSR-7 stream. matchBodyRegExp()
applies a PCRE pattern. Dedicated methods understand structured formats:
$pock->matchJsonBody(['name' => 'Jane']);
$pock->matchXmlBody('<user><name>Jane</name></user>');
$pock->matchFormData(['name' => 'Jane']);
See the JSON and XML guides for serializer-aware matching.
Multipart bodies are parsed before the callback is invoked:
use Riverline\MultiPartParser\StreamedPart;
$pock->matchMultipartFormData(static function (StreamedPart $part) {
return 'avatar' === $part->getName() && 'image/png' === $part->getMimeType();
});
Regular expressions and callbacks
URI, path, query string, header line, and body regular-expression matchers accept ordinary PCRE patterns and optional preg_match()
flags.
$pock->matchPathRegExp('~^/users/\d+$~')
->matchQueryRegExp('~(^|&)page=\d+(&|$)~');
For application-specific rules, inspect the complete PSR-7 request:
use Psr\Http\Message\RequestInterface;
$pock->matchCallback(static function (RequestInterface $request) {
return 'internal' === $request->getHeaderLine('X-Caller');
});
A reusable matcher can implement Pock\Matchers\RequestMatcherInterface
and be registered through addMatcher()
.