pock

PockBuilder
in package
Uses JsonDecoderTrait, JsonSerializerAwareTrait, XmlSerializerAwareTrait

Class PockBuilder

Tags
category

PockBuilder

SuppressWarnings

(PHPMD.CouplingBetweenObjects)

SuppressWarnings

(PHPMD.TooManyPublicMethods)

SuppressWarnings

(PHPMD.TooManyMethods)

SuppressWarnings

(PHPMD.ExcessivePublicCount)

SuppressWarnings

(PHPMD.ExcessiveClassComplexity)

Table of Contents

__construct()  : mixed
PockBuilder constructor.
addMatcher()  : self
Add custom matcher to the mock.
always()  : self
Always execute this mock if matched. Mock with this call will not be expired ever.
at()  : self
Match request only at Nth hit. Previous matches will not be executed.
getClient()  : Client
jsonDecode()  : mixed
json_decode which throws exception on error.
matchBody()  : self
Match entire request body.
matchBodyRegExp()  : self
Match entire request body using provided regular expression.
matchCallback()  : self
Match request using provided callback. Callback should receive RequestInterface and return boolean.
matchExactFormData()  : self
Match request with form-data. Additional fields aren't allowed.
matchExactHeader()  : self
Matches request by the exact header pattern or values.
matchExactHeaders()  : self
Matches request by headers values or several values.
matchExactQuery()  : self
Match request by its query. Additional query parameters aren't allowed.
matchFormData()  : self
Match request with form-data.
matchHeader()  : self
Matches request by header value or several values. Header can have other values which are not specified here.
matchHeaderLine()  : self
Matches request by the unparsed header line.
matchHeaderLineRegexp()  : self
Matches request by the unparsed header line using provided regular expression.
matchHeaders()  : self
Matches request by headers values or several values. Headers can have other values which are not specified here.
matchHost()  : self
Matches request by hostname.
matchJsonBody()  : self
Match JSON request body.
matchMethod()  : self
Match request by its method.
matchMultipartFormData()  : self
Match request multipart form data. Will not match the request if body is not multipart.
matchOrigin()  : self
Matches request by origin.
matchPath()  : self
Match request by its path. Path with and without slash at the start will be treated as the same path.
matchPathRegExp()  : self
Match request by its path using regular expression. This matcher doesn't care about prefix slash since it's pretty easy to do it using regular expression.
matchPort()  : self
Matches request by the port.
matchQuery()  : self
Match request by its query. Request can contain other query variables.
matchQueryRegExp()  : self
Match request by its query using regular expression.
matchScheme()  : self
Match request by its scheme.
matchSerializedJsonBody()  : self
Match JSON request body against JSON string or array with data.
matchSerializedXmlBody()  : self
Match XML request body.
matchUri()  : self
Matches request by the whole URI.
matchUriRegExp()  : self
Matches request by the whole URI using regular expression.
matchXmlBody()  : self
Match XML request body using raw XML data.
repeat()  : self
Repeat this mock provided amount of times.
reply()  : PockResponseBuilder
replyWithCallback()  : void
Construct the response during request execution using provided callback.
replyWithClient()  : void
Reply to the request using provided client. Can be used to send real network request.
replyWithFactory()  : void
Construct the response during request execution using provided ReplytFactoryInterface implementation.
reset()  : self
Resets the builder.
setFallbackClient()  : self
Sets fallback Client. It will be used if no request can be matched.
throwClientException()  : self
Throw an ClientExceptionInterface instance with specified message
throwException()  : self
Throw an exception when request is being sent.
throwNetworkException()  : self
Throw an NetworkExceptionInterface instance with specified message
throwRequestException()  : self
Throw an RequestExceptionInterface instance with specified message

Methods

__construct()

PockBuilder constructor.

public __construct() : mixed
Return values
mixed

always()

Always execute this mock if matched. Mock with this call will not be expired ever.

public always() : self
Return values
self

at()

Match request only at Nth hit. Previous matches will not be executed.

public at(int $hit) : self

Note: There IS a catch if you use this with the equal mocks. The test Client will not register hit for the second mock and the second mock will be executed at N+1 time.

For example, if you try to send 5 requests with this mocks and log response codes:

$builder = new PockBuilder();

$builder->matchHost('example.com')->at(2)->reply(200);
$builder->matchHost('example.com')->at(4)->reply(201);
$builder->always()->reply(400);

You will get this: 400, 400, 200, 400, 400, 201 Instead of this: 400, 400, 200, 400, 201, 400

Parameters
$hit : int
Return values
self

jsonDecode()

json_decode which throws exception on error.

public static jsonDecode(string $json[, bool $associative = false ][, int $depth = 512 ], int $flags) : mixed
Parameters
$json : string
$associative : bool = false
$depth : int = 512
$flags : int
Tags
throws
JsonException
SuppressWarnings

(PHPMD.BooleanArgumentFlag)

Return values
mixed

matchBody()

Match entire request body.

public matchBody(StreamInterface|resource|string $data) : self
Parameters
$data : StreamInterface|resource|string
Return values
self

matchBodyRegExp()

Match entire request body using provided regular expression.

public matchBodyRegExp(string $expression, int $flags) : self
Parameters
$expression : string
$flags : int
Return values
self

matchCallback()

Match request using provided callback. Callback should receive RequestInterface and return boolean.

public matchCallback(callable $callback) : self

If returned value is true then request is matched.

Parameters
$callback : callable

Callable that accepts PSR-7 RequestInterface as it's first argument.

Return values
self

matchExactFormData()

Match request with form-data. Additional fields aren't allowed.

public matchExactFormData(array<string, mixed> $formFields) : self
Parameters
$formFields : array<string, mixed>
Return values
self

matchExactHeader()

Matches request by the exact header pattern or values.

public matchExactHeader(string $header, string|array<string|int, string> $value) : self
Parameters
$header : string
$value : string|array<string|int, string>
Return values
self

matchExactHeaders()

Matches request by headers values or several values.

public matchExactHeaders(array<string, string|string[]> $headers) : self

Note: only host header will be dropped. Any other headers will not be excluded and can result in the problems with the exact matching.

Parameters
$headers : array<string, string|string[]>
Return values
self

matchExactQuery()

Match request by its query. Additional query parameters aren't allowed.

public matchExactQuery(array<string, mixed> $query) : self
Parameters
$query : array<string, mixed>
Return values
self

matchFormData()

Match request with form-data.

public matchFormData(array<string, mixed> $formFields) : self
Parameters
$formFields : array<string, mixed>
Return values
self

matchHeader()

Matches request by header value or several values. Header can have other values which are not specified here.

public matchHeader(string $header, string|array<string|int, string> $value) : self
Parameters
$header : string
$value : string|array<string|int, string>
Tags
see
PockBuilder::matchExactHeader()

if you want to match exact header values.

Return values
self

matchHeaderLine()

Matches request by the unparsed header line.

public matchHeaderLine(string $header, string $value) : self
Parameters
$header : string
$value : string
Return values
self

matchHeaderLineRegexp()

Matches request by the unparsed header line using provided regular expression.

public matchHeaderLineRegexp(string $header, string $pattern) : self
Parameters
$header : string
$pattern : string
Return values
self

matchHeaders()

Matches request by headers values or several values. Headers can have other values which are not specified here.

public matchHeaders(array<string, string|string[]> $headers) : self
Parameters
$headers : array<string, string|string[]>
Tags
see
PockBuilder::matchExactHeaders()

if you want to match exact headers collection.

Return values
self

matchHost()

Matches request by hostname.

public matchHost(string $host) : self
Parameters
$host : string
Return values
self

matchJsonBody()

Match JSON request body.

public matchJsonBody(mixed $data) : self
Parameters
$data : mixed
Tags
throws
JsonException
Return values
self

matchMethod()

Match request by its method.

public matchMethod(string $method) : self
Parameters
$method : string
Return values
self

matchMultipartFormData()

Match request multipart form data. Will not match the request if body is not multipart.

public matchMultipartFormData(callable $callback) : self

Uses third-party library to parse the data.

Parameters
$callback : callable

Accepts Riverline\MultiPartParser\StreamedPart as an argument, returns true if matched.

Tags
see
https://github.com/Riverline/multipart-parser#usage
Return values
self

matchOrigin()

Matches request by origin.

public matchOrigin(string $origin) : self
Parameters
$origin : string
Tags
throws
RuntimeException
Return values
self

matchPath()

Match request by its path. Path with and without slash at the start will be treated as the same path.

public matchPath(string $path) : self

It's not the same for the path with slash at the end of it.

Parameters
$path : string
Return values
self

matchPathRegExp()

Match request by its path using regular expression. This matcher doesn't care about prefix slash since it's pretty easy to do it using regular expression.

public matchPathRegExp(string $expression, int $flags) : self
Parameters
$expression : string
$flags : int
Return values
self

matchPort()

Matches request by the port.

public matchPort(int $port) : self
Parameters
$port : int
Return values
self

matchQuery()

Match request by its query. Request can contain other query variables.

public matchQuery(array<string, mixed> $query) : self
Parameters
$query : array<string, mixed>
Tags
see
PockBuilder::matchExactQuery()

if you want to match an entire query string.

Return values
self

matchQueryRegExp()

Match request by its query using regular expression.

public matchQueryRegExp(string $expression, int $flags) : self
Parameters
$expression : string
$flags : int
Return values
self

matchScheme()

Match request by its scheme.

public matchScheme(string $scheme) : self
Parameters
$scheme : string
Return values
self

matchSerializedJsonBody()

Match JSON request body against JSON string or array with data.

public matchSerializedJsonBody(array<int|string, mixed>|string $data) : self
Parameters
$data : array<int|string, mixed>|string
Tags
throws
JsonException
Return values
self

matchSerializedXmlBody()

Match XML request body.

public matchSerializedXmlBody(string|array<string|int, mixed>|object $data) : self

This method will try to use available XML serializer before matching.

Parameters
$data : string|array<string|int, mixed>|object
Tags
phpstan-ignore-next-line
throws
XmlException
Return values
self

matchUri()

Matches request by the whole URI.

public matchUri(UriInterface|string $uri) : self
Parameters
$uri : UriInterface|string
Return values
self

matchUriRegExp()

Matches request by the whole URI using regular expression.

public matchUriRegExp(string $expression, int $flags) : self
Parameters
$expression : string
$flags : int
Return values
self

matchXmlBody()

Match XML request body using raw XML data.

public matchXmlBody(DOMDocument|StreamInterface|resource|string $data) : self

Note: this method will fallback to the string comparison if ext-xsl is not available. It also doesn't serializer values with available XML serializer. Use PockBuilder::matchSerializedXmlBody if you want to execute available serializer.

Parameters
$data : DOMDocument|StreamInterface|resource|string
Tags
throws
XmlException
see
PockBuilder::matchSerializedXmlBody()
Return values
self

repeat()

Repeat this mock provided amount of times.

public repeat(int $hits) : self

For example, if you pass 2 as an argument mock will be able to handle two identical requests.

Parameters
$hits : int
Return values
self

replyWithCallback()

Construct the response during request execution using provided callback.

public replyWithCallback(callable $callback) : void

Callback should receive the same parameters as in the ReplyFactoryInterface::createReply method.

Parameters
$callback : callable
Tags
see
ReplyFactoryInterface::createReply()
Return values
void

replyWithClient()

Reply to the request using provided client. Can be used to send real network request.

public replyWithClient(ClientInterface $client) : void
Parameters
$client : ClientInterface
Tags
SuppressWarnings

(unused)

Return values
void

reset()

Resets the builder.

public reset() : self
Return values
self

setFallbackClient()

Sets fallback Client. It will be used if no request can be matched.

public setFallbackClient([ClientInterface|null $fallbackClient = null ]) : self
Parameters
$fallbackClient : ClientInterface|null = null
Return values
self

throwClientException()

Throw an ClientExceptionInterface instance with specified message

public throwClientException([string $message = 'Pock ClientException' ]) : self
Parameters
$message : string = 'Pock ClientException'
Return values
self

throwException()

Throw an exception when request is being sent.

public throwException(Throwable $throwable) : self
Parameters
$throwable : Throwable
Return values
self

throwNetworkException()

Throw an NetworkExceptionInterface instance with specified message

public throwNetworkException([string $message = 'Pock NetworkException' ]) : self
Parameters
$message : string = 'Pock NetworkException'
Return values
self

throwRequestException()

Throw an RequestExceptionInterface instance with specified message

public throwRequestException([string $message = 'Pock RequestException' ]) : self
Parameters
$message : string = 'Pock RequestException'
Return values
self

Search results