Skip to content

TestResponse

Assertable wrapper around a PSR-7 response, returned by HttpTestCase’s get()/post()/json() etc.

final class TestResponse

SourceTesting/Http/TestResponse.php

public function __construct(ResponseInterface $response): mixed

ParameterTypeDescription
$responseResponseInterface

Returns mixed

MethodDescription
__call(string $name, array<int, mixed> $arguments): mixed
assertCreated(): TestResponseAsserts the status is 201 Created.
assertDontSee(string $needle): TestResponseAsserts the response body does not contain $needle.
assertForbidden(): TestResponseAsserts the status is 403 Forbidden.
assertHasXPath(string $expression): TestResponseAsserts the XPath expression matches at least one node in the response body.
assertHeader(string $name, ?string $value = null): TestResponseAsserts the response carries the named header.
assertJson(array<mixed> $expected): TestResponse
assertJsonEquals(array<mixed> $expected): TestResponse
assertJsonFragment(array<mixed> $expected): TestResponseLooser than TestResponse::assertJson(): the subset must match at least one element when the decoded body is a list of records.
assertNoContent(): TestResponseAsserts the status is 204 No Content.
assertNotFound(): TestResponseAsserts the status is 404 Not Found.
assertOk(): TestResponseAsserts the status is 200 OK.
assertRedirect(?string $uri = null): TestResponseAsserts the status is any 3xx redirect.
assertSee(string $needle): TestResponseAsserts the response body contains $needle.
assertStatus(int $expected): TestResponseAsserts the response status is exactly $expected.
assertUnauthorized(): TestResponseAsserts the status is 401 Unauthorized.
assertXml(string $expectedXml): TestResponseAsserts the response body is XML equivalent to $expectedXml.
clearExtensions(): voidDrops every registered assertion.
extend(string $name, callable $assertion): voidRegisters a custom assertion callable in the process-global extension table, callable on any instance as $response->$name(...).
getContent(): stringReturns the response body as a string.
getHeaderLine(string $name): stringReturns the named header’s values joined with commas, or an empty string if it is absent.
getPsrResponse(): ResponseInterfaceReturns the wrapped PSR-7 response, for assertions this class does not cover.
getStatusCode(): intReturns the response’s HTTP status code.
hasExtension(string $name): boolWhether an assertion has been registered under $name.
json(): array<mixed>
xml(): SimpleXMLElementParses the response body as XML and returns its root element.

public function __call(string $name, array<int, mixed> $arguments): mixed

ParameterTypeDescription
$namestring
$argumentsarray``<``int``, ``mixed``>

Returns mixed

public function assertCreated(): TestResponse

Asserts the status is 201 Created.

Returns $this for chaining.

Returns TestResponse

public function assertDontSee(string $needle): TestResponse

Asserts the response body does not contain $needle.

The same raw, case-sensitive substring match as TestResponse::assertSee(). Returns $this for chaining.

ParameterTypeDescription
$needlestring

Returns TestResponse

public function assertForbidden(): TestResponse

Asserts the status is 403 Forbidden.

Returns $this for chaining.

Returns TestResponse

public function assertHasXPath(string $expression): TestResponse

Asserts the XPath expression matches at least one node in the response body.

Evaluated against the parsed document from TestResponse::xml(), so a body that is not valid XML fails there first. The failure message includes the body. Returns $this for chaining.

ParameterTypeDescription
$expressionstring

Returns TestResponse

public function assertHeader(string $name, ?string $value = null): TestResponse

Asserts the response carries the named header.

Passing $value additionally asserts the header line equals it exactly; a multi-valued header is compared as its comma-joined form. Returns $this for chaining.

ParameterTypeDescription
$namestring
$value?``string

Returns TestResponse

public function assertJson(array<mixed> $expected): TestResponse

ParameterTypeDescription
$expectedarray``<``mixed``>

Returns TestResponse

public function assertJsonEquals(array<mixed> $expected): TestResponse

ParameterTypeDescription
$expectedarray``<``mixed``>

Returns TestResponse

public function assertJsonFragment(array<mixed> $expected): TestResponse

Looser than TestResponse::assertJson(): the subset must match at least one element when the decoded body is a list of records.

ParameterTypeDescription
$expectedarray``<``mixed``>

Returns TestResponse

public function assertNoContent(): TestResponse

Asserts the status is 204 No Content.

Does not check that the body is empty. Returns $this for chaining.

Returns TestResponse

public function assertNotFound(): TestResponse

Asserts the status is 404 Not Found.

Returns $this for chaining.

Returns TestResponse

public function assertOk(): TestResponse

Asserts the status is 200 OK.

Returns $this for chaining.

Returns TestResponse

public function assertRedirect(?string $uri = null): TestResponse

Asserts the status is any 3xx redirect.

Passing $uri additionally asserts the Location header equals it exactly; omitting it checks only that a redirect happened. Returns $this for chaining.

ParameterTypeDescription
$uri?``string

Returns TestResponse

public function assertSee(string $needle): TestResponse

Asserts the response body contains $needle.

A raw, case-sensitive substring match on the body as sent — no HTML parsing or entity decoding. Returns $this for chaining.

ParameterTypeDescription
$needlestring

Returns TestResponse

public function assertStatus(int $expected): TestResponse

Asserts the response status is exactly $expected.

The failure message carries the body, so an unexpected 500 shows what went wrong rather than only the number. Returns $this so assertions chain.

ParameterTypeDescription
$expectedint

Returns TestResponse

public function assertUnauthorized(): TestResponse

Asserts the status is 401 Unauthorized.

Returns $this for chaining.

Returns TestResponse

public function assertXml(string $expectedXml): TestResponse

Asserts the response body is XML equivalent to $expectedXml.

Both sides are run through C14N canonicalization before comparison, so insignificant whitespace, attribute order and namespace-prefix spelling do not matter. Either side failing to parse fails the test with the parser’s messages. Returns $this for chaining.

ParameterTypeDescription
$expectedXmlstring

Returns TestResponse

public static function clearExtensions(): void

Drops every registered assertion.

The table is static, so a suite that registers extensions in one test and needs the next to start clean calls this from its tear-down.

public static function extend(string $name, callable $assertion): void

Registers a custom assertion callable in the process-global extension table, callable on any instance as $response->$name(...).

The callable is bound to the TestResponse it is invoked on, so $this inside it is the response under assertion. Re-registering an existing name replaces it and logs a warning rather than failing, since a test bootstrap may legitimately run more than once.

ParameterTypeDescription
$namestring
$assertioncallable

public function getContent(): string

Returns the response body as a string.

The stream is rewound and drained on the first call and the result kept, so repeated assertions against the body do not consume it and every one sees the same bytes.

Returns string

public function getHeaderLine(string $name): string

Returns the named header’s values joined with commas, or an empty string if it is absent.

ParameterTypeDescription
$namestring

Returns string

public function getPsrResponse(): ResponseInterface

Returns the wrapped PSR-7 response, for assertions this class does not cover.

Returns ResponseInterface

public function getStatusCode(): int

Returns the response’s HTTP status code.

Returns int

public static function hasExtension(string $name): bool

Whether an assertion has been registered under $name.

Real methods on the class are not extensions, so this is false for them.

ParameterTypeDescription
$namestring

Returns bool

public function json(): array<mixed>

Returns array``<``mixed``>

public function xml(): SimpleXMLElement

Parses the response body as XML and returns its root element.

The parse happens once and is kept for subsequent calls. libxml’s own error reporting is captured for the duration so a malformed body fails the test with the parser’s messages and the body itself, instead of emitting warnings; the previous libxml setting is restored either way.

Returns SimpleXMLElement