ApiTest

APITester
in package

API Tester class.

Contains all the automated API tests.

Table of Contents

Properties

$client  : Client
Guzzle client object.
$cookieJar  : CookieJar
Cookie jar object.
$history  : array<int, array<string, mixed>>
The history container.
$response  : ResponseInterface
The complete response object.
$requestContentType  : string
Request content data type.
$responseContentType  : string
Response content data type.

Methods

__construct()  : mixed
Constructor method.
apiEndPointTest()  : string
Api end point test method.
checkBody()  : string
Check body method.
checkStatus()  : int
Check status method.
testApiEndPoint()  : string
Test API end point method.
checkResponse()  : string
Store and validate a response from either a request or an exception.
displayException()  : void
Display exception details without reading the response stream again.
getGuzzleOptions()  : array<string, mixed>
Get guzzle options.
getHandlerStack()  : HandlerStack
Get handler stack.
getRequestHeaders()  : array<string|int, mixed>
Validate headers and apply the configured User-Agent.
readResponseBody()  : string
Read a seekable body in full and restore its original position.

Properties

$client

Guzzle client object.

public Client $client

$cookieJar

Cookie jar object.

public CookieJar $cookieJar

$history

The history container.

public array<int, array<string, mixed>> $history = []

$response

The complete response object.

public ResponseInterface $response

$requestContentType

Request content data type.

private string $requestContentType

$responseContentType

Response content data type.

private string $responseContentType

Methods

__construct()

Constructor method.

public __construct(string $host[, string $requestContentType = 'application/json' ][, string $responseContentType = 'application/json' ]) : mixed
Parameters
$host : string

The host for the tests.

$requestContentType : string = 'application/json'

The request data type.

$responseContentType : string = 'application/json'

The response data type.

apiEndPointTest()

Api end point test method.

public apiEndPointTest(string $method, string $endPoint[, null|string|array<string|int, mixed> $data = null ][, TestOptions $testOptions = new TestOptions() ]) : string
Parameters
$method : string

The HTTP method to use.

$endPoint : string

The API end point.

$data : null|string|array<string|int, mixed> = null

The JSON or form data to process. Can be null, string, or various array structures.

$testOptions : TestOptions = new TestOptions()

The options object. Contains various options.

Tags
throws
RequestException

If the request fails without a response.

Exception

If request processing fails.

Return values
string

checkBody()

Check body method.

public static checkBody(ResponseInterface $response[, bool $expectedJson = true ][, bool $errorExpected = false ][, bool $contentRequired = true ][, bool $tryBasicAsserts = true ]) : string
Parameters
$response : ResponseInterface

The PSR7 response object.

$expectedJson : bool = true

Indicates whether the body is expected to be json or not.

$errorExpected : bool = false

Indicates whether an error field is expected in the response or not.

$contentRequired : bool = true

Indicates whether content is required in the response body.

$tryBasicAsserts : bool = true

Indicates whether to try running basic asserts or not.

Return values
string

checkStatus()

Check status method.

public static checkStatus(ResponseInterface $response[, bool $errorExpected = false ][, bool $tryBasicAsserts = true ]) : int
Parameters
$response : ResponseInterface

The PSR7 response object.

$errorExpected : bool = false

Indicates whether an error is expected or not.

$tryBasicAsserts : bool = true

Indicates whether to try running basic asserts or not.

Return values
int

testApiEndPoint()

Test API end point method.

public testApiEndPoint(string $method, string $endPoint, null|string|array<string|int, mixed> $data[, bool|string $dataType = false ][, bool $isError = false ][, bool $errorRequired = false ][, bool $contentRequired = true ]) : string

since v1.6.32, use apiEndPointTest() instead.

Parameters
$method : string

The HTTP method to use.

$endPoint : string

The API end point.

$data : null|string|array<string|int, mixed>

The JSON data to process.

$dataType : bool|string = false

The data type. True, if it is multipart form data. Implying some of the data may be binary. If it is a string, the string indicates the type of data ('body', 'json', 'form_params').

$isError : bool = false

Indicates whether an error is expected or not.

$errorRequired : bool = false

Indicates whether an error field is expected in the response or not.

$contentRequired : bool = true

Indicates whether content is required in the response body.

Return values
string

checkResponse()

Store and validate a response from either a request or an exception.

private checkResponse(ResponseInterface $response, TestOptions $testOptions) : string
Parameters
$response : ResponseInterface

The received response.

$testOptions : TestOptions

The request assertions to apply.

Return values
string

displayException()

Display exception details without reading the response stream again.

private static displayException(RequestException $exception, string $responseContent) : void
Parameters
$exception : RequestException

The exception to process.

$responseContent : string

The captured response body.

getGuzzleOptions()

Get guzzle options.

private getGuzzleOptions(null|string|array<string|int, mixed> $data, TestOptions $testOptions) : array<string, mixed>
Parameters
$data : null|string|array<string|int, mixed>

The JSON data to process.

$testOptions : TestOptions

The options object. Contains various options.

Return values
array<string, mixed>

getHandlerStack()

Get handler stack.

private getHandlerStack() : HandlerStack
Return values
HandlerStack

getRequestHeaders()

Validate headers and apply the configured User-Agent.

private getRequestHeaders(mixed $headers, string $userAgent) : array<string|int, mixed>
Parameters
$headers : mixed

The supplied Guzzle headers option.

$userAgent : string

The User-Agent to apply.

Tags
throws
InvalidArgumentException

If headers are not an array.

Return values
array<string|int, mixed>

readResponseBody()

Read a seekable body in full and restore its original position.

private static readResponseBody(StreamInterface $stream) : string

Nonseekable streams are consumed from their current position.

Parameters
$stream : StreamInterface

The response body stream.

Return values
string
On this page

Search results