Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 28, 2025

This PR addresses the need to make assertion methods available across both Mantle\Testing\Test_Response and Mantle\Http_Client\Response classes by centralizing them in a shared trait.

Problem

Previously, the Test_Response class had many useful assertion methods like assertSuccessful(), assertStatus(), assertIsJson(), etc., but these were not available when working with HTTP client responses. This led to:

  • Code duplication between testing and HTTP client contexts
  • Inconsistent assertion capabilities
  • Limited testing options for HTTP client responses

Solution

Created a centralized Response_Assertions trait that provides consistent assertion methods for both response classes:

Key Changes

  1. New Response_Assertions trait (src/mantle/support/traits/trait-response-assertions.php)

    • Contains 23+ assertion methods for status codes, headers, content, and content types
    • Uses abstract methods to work with different response implementations
    • Provides consistent behavior across contexts
  2. Enhanced Http_Client\Response (src/mantle/http-client/class-response.php)

    • Added the Response_Assertions trait
    • Implemented abstract methods using existing response methods (status(), header(), body())
  3. Updated Test_Response (src/mantle/testing/class-test-response.php)

    • Added trait with method aliasing to avoid conflicts
    • Existing methods now delegate to centralized implementations
    • Maintains full backward compatibility

Available Assertions

Both response classes now support identical assertion methods:

Status Assertions:

  • assertSuccessful(), assertOk(), assertStatus(), assertCreated()
  • assertNotFound(), assertForbidden(), assertUnauthorized()
  • assertClientError(), assertServerError()

Header Assertions:

  • assertHeader(), assertHeaderMissing(), assertLocation()

Content Assertions:

  • assertContent(), assertSee(), assertDontSee(), assertContains()

Content-Type Assertions:

  • assertIsJson(), assertIsNotJson(), assertIsHtml(), assertIsNotHtml()

Special Assertions:

  • assertRedirect(), assertNoContent()

Usage Examples

HTTP Client Response:

$response = $http_client->get('https://api.example.com/users');
$response->assertSuccessful();
$response->assertIsJson();
$response->assertSee('users');

Test Response (unchanged):

$response = $this->get('/api/users');
$response->assertSuccessful();
$response->assertIsJson();
$response->assertSee('users');

Benefits

  • ✅ Centralized assertion logic eliminates code duplication
  • ✅ Consistent behavior across testing and HTTP client contexts
  • ✅ Full backward compatibility maintained
  • ✅ Enhanced testing capabilities for HTTP client responses
  • ✅ Easier maintenance and future updates

This change makes the framework more cohesive by ensuring both response types provide the same powerful assertion interface.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/alleyinteractive/composer-wordpress-autoloader/zipball/a3a401b3d755d08f7689d0c5f1566809ea469f90
    • Triggering command: /usr/bin/php8.3 -n -c /tmp/Y6Zn9G /usr/bin/composer install --no-dev (http block)

If you need me to access, download, or install something from one of these locations, you can either:


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] similar to how Mantle\Testing\Test_Response has assertions, make them available to the HTTP client's Mantle\Http_Client\Response class. perhaps they should be centralized so they can be used across both classes Centralize response assertions between Test_Response and Http_Client\Response classes Aug 28, 2025
Copilot AI requested a review from srtfisher August 28, 2025 03:43
Copilot finished work on behalf of srtfisher August 28, 2025 03:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants