VOOZH about

URL: https://deepwiki.com/hypervel/foundation/6.4-test-response-assertions

⇱ Test Response Assertions | hypervel/foundation | DeepWiki


Loading...
Last indexed: 7 February 2026 (101eff)
Menu

Test Response Assertions

Purpose and Scope

This document covers the TestResponse class and its assertion methods for validating HTTP response properties in tests. The TestResponse class provides a fluent interface for asserting response headers, cookies, session data, validation errors, and JSON content. For information about making HTTP requests in tests and obtaining TestResponse objects, see HTTP Request Testing. For database-related test assertions, see Database Testing Utilities.


TestResponse Class Overview

The TestResponse class extends Hyperf's HyperfTestResponse to provide Laravel-style assertion methods. It serves as a wrapper around PSR-7 ResponseInterface objects, enabling intuitive test assertions with descriptive failure messages.

Key Responsibilities:

  • Wrap HTTP responses from test requests
  • Provide fluent assertion API for response properties
  • Integrate with session and validation systems
  • Offer debugging utilities for response inspection

Sources: src/Testing/Http/TestResponse.php20-28


Assertion Categories

The TestResponse class organizes assertions into five primary categories, each addressing specific response characteristics:

CategoryMethodsPurpose
HeadersassertHeader, assertHeaderMissingVerify HTTP header presence and values
CookiesassertCookie, assertCookieExpired, assertCookieNotExpired, assertCookieMissingValidate cookie properties and expiration
DownloadsassertDownloadConfirm file download responses
ValidationassertValid, assertInvalidCheck validation error presence in JSON responses
SessionassertSessionHas, assertSessionHasErrors, assertSessionMissingVerify session state after requests

Sources: src/Testing/Http/TestResponse.php1-468


Header Assertions

Header assertions verify the presence, absence, and values of HTTP response headers. These methods enable validation of content types, cache control, security headers, and custom headers.

assertHeader

Asserts that a specific header exists in the response and optionally matches a given value.


Method Signature:


Implementation Details:

assertHeaderMissing

Asserts that a specific header is not present in the response.

Method Signature:


Implementation Details:

Sources: src/Testing/Http/TestResponse.php30-64


Download Assertions

The assertDownload method validates that a response represents a file download by checking the Content-Disposition header.

assertDownload

Verifies that the response offers a file download with optional filename validation.


Method Signature:


Implementation Details:

Sources: src/Testing/Http/TestResponse.php66-109


Cookie Assertions

Cookie assertions validate the presence, values, and expiration status of cookies in the response. These methods work with the Cookie class from the hypervel/cookie package.

Cookie Assertion Methods


assertCookie / assertPlainCookie

Asserts that a cookie with the given name exists and optionally matches a value.

Method Signatures:


Implementation:

assertCookieExpired / assertCookieNotExpired

Validates cookie expiration status using timestamp comparison.

Implementation Details:

getCookie Helper

Retrieves a cookie by name from the response.

Method Signature:


Implementation:

Sources: src/Testing/Http/TestResponse.php112-210


Validation Assertions

Validation assertions provide convenient methods for checking validation errors in JSON responses, typically from form request validation failures.

assertValid / assertInvalid

Simplified aliases for JSON validation error assertions.

Method Signatures:


Implementation:

Usage Context: These methods validate responses from form requests (see Form Request Validation and Casting) that return validation errors in JSON format. The $keys parameter accepts:

  • null: Assert no validation errors exist for any fields
  • string: Assert no error for a specific field
  • array: Assert no errors for multiple fields

Sources: src/Testing/Http/TestResponse.php212-226


Session Assertions

Session assertions verify session state changes after HTTP requests. These methods require the hypervel/session package and integrate with the session system documented in Authentication and Session Testing.

Session Access

The TestResponse class accesses the session through the application container:


Implementation:

Sources: src/Testing/Http/TestResponse.php228-236

assertSessionHas

Asserts that the session contains a specific key with optional value validation.

Method Signature:


Parameter Behaviors:

Parameter TypeBehavior
array as $keyDelegates to assertSessionHasAll()
string as $key, null as $valueAsserts key exists in session
string as $key, Closure as $valueExecutes closure with session value, asserts truthy
string as $key, scalar as $valueAsserts session value equals expected value

Implementation:

assertSessionHasAll

Asserts multiple session keys and values.

Method Signature:


Array Format Support:

  • Numeric keys: [0 => 'key1', 1 => 'key2'] - asserts keys exist
  • Associative keys: ['key1' => 'value1', 'key2' => 'value2'] - asserts keys and values

Implementation:

Sources: src/Testing/Http/TestResponse.php238-275

assertSessionHasInput

Asserts that the session contains flashed input (old input) from previous requests.

Method Signature:


Purpose: Validates that input was flashed to the session, typically after validation failures. The session stores old input using SessionContract::flashInput() methods.

Implementation:

Sources: src/Testing/Http/TestResponse.php277-308

Session Error Assertions

Error assertions validate validation errors stored in the session's ViewErrorBag.


assertSessionHasErrors

Asserts that specific validation errors exist in the session.

Method Signature:


Parameters:

  • $keys: Error field names or field => message pairs
  • $format: Message format string (passed to MessageBag::get())
  • $errorBag: Named error bag (defaults to 'default')

Implementation:

assertSessionHasErrorsIn

Convenience method for asserting errors in a named error bag.

Method Signature:


Implementation:

assertSessionHasNoErrors

Asserts that no validation errors exist in any error bag.

Method Signature:


Implementation:

assertSessionDoesntHaveErrors

Asserts that specific keys do not have errors, or no errors exist if keys empty.

Method Signature:


Implementation:

Sources: src/Testing/Http/TestResponse.php310-398

assertSessionMissing

Asserts that specific keys do not exist in the session.

Method Signature:


Implementation:

Sources: src/Testing/Http/TestResponse.php400-417


Session Setup in Tests

Tests must properly initialize session state before making requests with session assertions. The InteractsWithSession trait provides helper methods for session management.


Key Methods:

MethodPurposeImplementation
withSession(array $data)Set session data before requestDelegates to session() src/Testing/Concerns/InteractsWithSession.php14-19
session(array $data)Set session dataStarts session, puts each key-value pair src/Testing/Concerns/InteractsWithSession.php24-33
startSession()Initialize sessionGenerates ID if needed, calls start() src/Testing/Concerns/InteractsWithSession.php38-54
flushSession()Clear session dataCalls session()->flush() src/Testing/Concerns/InteractsWithSession.php59-66

Session ID Generation: The startSession() method ensures a session ID exists before starting. In tests (unlike production where middleware sets ID from cookie), if no ID exists, it calls setId(null) which generates a new ID src/Testing/Concerns/InteractsWithSession.php46-48

Sources: src/Testing/Concerns/InteractsWithSession.php1-68


Debugging Utilities

The TestResponse class provides debugging methods for inspecting response content during test development.

dump / dd Methods


dump

Dumps the response content to output, automatically parsing JSON if valid.

Method Signature:


Implementation:

dd

Dumps response content and terminates script execution.

Method Signature:


Implementation:

dumpHeaders

Dumps all response headers.

Method Signature:


Implementation:

dumpSession

Dumps all session data.

Method Signature:


Implementation:

Sources: src/Testing/Http/TestResponse.php419-468


Integration with Testing Workflow

The TestResponse class integrates into the broader testing workflow as the return value from HTTP test requests.


Typical Test Flow:

  1. Setup: Test initializes session/auth state using traits
  2. Request: Test calls HTTP method ($this->post(...))
  3. Response: Request returns TestResponse instance
  4. Assertions: Test chains assertion methods
  5. Validation: Each assertion validates response state

Example Pattern:


Sources: src/Testing/Http/TestResponse.php20-28