diff --git a/CHANGELOG.md b/CHANGELOG.md index 88088dca7..e724f17a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added new `defer()` helper. - Added `Cache::flexible()` method to add SWR support to the cache. - Added dynamic creation of post type/taxonomy factories. +- Added `Reset_Server` trait to reset the server between tests. +- Add `with_https()` to control if the request being tested is over HTTPS. ### Changed diff --git a/src/mantle/testing/class-pending-testable-request.php b/src/mantle/testing/class-pending-testable-request.php index 374678125..8ccc8785e 100644 --- a/src/mantle/testing/class-pending-testable-request.php +++ b/src/mantle/testing/class-pending-testable-request.php @@ -46,6 +46,11 @@ class Pending_Testable_Request { */ public HeaderBag $headers; + /** + * Indicates whether the request should be made over HTTPS. + */ + public bool $https = false; + /** * The cookies for the request. */ @@ -89,6 +94,17 @@ public function with_header( string $name, string $value ): static { return $this->with_headers( [ $name => $value ] ); } + /** + * Define whether the request should be made over HTTPS. + * + * @param bool $value Whether to use HTTPS. + */ + public function with_https( bool $value ): static { + $this->https = $value; + + return $this; + } + /** * Set the referer header and previous URL session value in order to simulate * a previous request. @@ -407,6 +423,12 @@ protected function reset_request_state(): void { } } + if ( $this->https ) { + $_SERVER['HTTPS'] = 'on'; + } else { + unset( $_SERVER['HTTPS'] ); + } + // phpcs:enable } diff --git a/src/mantle/testing/class-test-case.php b/src/mantle/testing/class-test-case.php index c3bff6deb..26825b17d 100644 --- a/src/mantle/testing/class-test-case.php +++ b/src/mantle/testing/class-test-case.php @@ -93,16 +93,15 @@ public static function setUpBeforeClass(): void { static::register_traits(); if ( ! empty( static::$test_uses ) ) { - static::get_test_case_traits() - ->each( - function ( $trait ): void { - $method = strtolower( class_basename( $trait ) ) . '_set_up_before_class'; - - if ( method_exists( static::class, $method ) ) { - call_user_func( [ static::class, $method ] ); - } + static::get_test_case_traits()->each( + function ( $trait ): void { + $method = strtolower( class_basename( $trait ) ) . '_set_up_before_class'; + + if ( method_exists( static::class, $method ) ) { + call_user_func( [ static::class, $method ] ); } - ); + } + ); } parent::setUpBeforeClass(); @@ -116,6 +115,18 @@ function ( $trait ): void { * Runs the routine after all tests have been run. */ public static function tearDownAfterClass(): void { + if ( ! empty( static::$test_uses ) ) { + static::get_test_case_traits()->each( + function ( $trait ): void { + $method = strtolower( class_basename( $trait ) ) . '_tear_down_after_class'; + + if ( method_exists( static::class, $method ) ) { + call_user_func( [ static::class, $method ] ); + } + } + ); + } + parent::tearDownAfterClass(); if ( isset( static::$test_uses[ Refresh_Database::class ] ) ) { diff --git a/src/mantle/testing/class-test-response.php b/src/mantle/testing/class-test-response.php index 64a3b464e..96747eec8 100644 --- a/src/mantle/testing/class-test-response.php +++ b/src/mantle/testing/class-test-response.php @@ -18,7 +18,7 @@ */ class Test_Response { use Concerns\Element_Assertions; - use Concerns\Snapshot_Testing; + use Concerns\Response_Snapshot_Testing; use Macroable; /** diff --git a/src/mantle/testing/concerns/trait-makes-http-requests.php b/src/mantle/testing/concerns/trait-makes-http-requests.php index 3557fba5d..b49c6ab2a 100644 --- a/src/mantle/testing/concerns/trait-makes-http-requests.php +++ b/src/mantle/testing/concerns/trait-makes-http-requests.php @@ -21,18 +21,23 @@ */ trait Makes_Http_Requests { /** - * Additional headers for the request. + * Additional cookies for the request. * * @var array */ - protected array $default_headers = []; + protected array $default_cookies = []; /** - * Additional cookies for the request. + * Additional headers for the request. * * @var array */ - protected array $default_cookies = []; + protected array $default_headers = []; + + /** + * Whether to use HTTPS by default. + */ + protected bool|null $default_https = null; /** * The array of callbacks to be run before the event is started. @@ -74,6 +79,7 @@ protected function create_pending_request(): Pending_Testable_Request { function ( Pending_Testable_Request $request ): void { $request->cookies->add( $this->default_cookies ); $request->headers->add( $this->default_headers ); + $request->with_https( $this->default_https ?? false ); }, ); } @@ -92,6 +98,15 @@ public function add_default_header( array|string $headers, ?string $value = null } } + /** + * Set the default HTTPS setting for all requests. + * + * @param bool|null $value Whether to use HTTPS by default. + */ + public function set_default_https( bool|null $value = true ): void { + $this->default_https = $value; + } + /** * Flush all the configured headers. */ @@ -102,7 +117,7 @@ public function flush_default_headers(): static { } /** - * Define additional headers to be sent with the request. + * Create a pending request with a specific headers included. * * @param array $headers Headers for the request. */ @@ -111,7 +126,7 @@ public function with_headers( array $headers ): Pending_Testable_Request { } /** - * Define additional header to be sent with the request. + * Create a pending request with a specific header included. * * @param string $name Header name (key). * @param string $value Header value. @@ -120,6 +135,15 @@ public function with_header( string $name, string $value ): Pending_Testable_Req return $this->with_headers( [ $name => $value ] ); } + /** + * Create a pending request with the HTTPS enabled/disabled. + * + * @param bool $value Whether to use HTTPS. + */ + public function with_https( bool $value = true ): Pending_Testable_Request { + return $this->create_pending_request()->with_https( $value ); + } + /** * Set the referer header and previous URL session value in order to simulate * a previous request. diff --git a/src/mantle/testing/concerns/trait-reset-server.php b/src/mantle/testing/concerns/trait-reset-server.php new file mode 100644 index 000000000..f68729fd2 --- /dev/null +++ b/src/mantle/testing/concerns/trait-reset-server.php @@ -0,0 +1,31 @@ +get( '/' )->assertOk(); + + $this->assertEmpty( $_SERVER['HTTPS'] ?? '' ); + + $this->with_https()->get( 'https://example.com' )->assertOk(); + + $this->assertEquals( 'on', $_SERVER['HTTPS'] ); + } + public function test_multiple_requests() { $methods = collect( get_class_methods( $this ) ) ->filter( fn ( $method ) => false === strpos( $method, '_snapshot_' ) ) diff --git a/tests/Testing/Concerns/ResetServerTest.php b/tests/Testing/Concerns/ResetServerTest.php new file mode 100644 index 000000000..e7aa30ec8 --- /dev/null +++ b/tests/Testing/Concerns/ResetServerTest.php @@ -0,0 +1,33 @@ +assertSame( WP_TESTS_DOMAIN, $_SERVER['HTTP_HOST'] ); + + $_SERVER['HTTP_HOST'] = 'other.org'; + + $this->assertSame( 'other.org', $_SERVER['HTTP_HOST'] ); + } + + public function test_modify_server_request_uri() { + $this->assertEquals( WP_TESTS_DOMAIN, $_SERVER['HTTP_HOST'] ); + } +}