From 9fdb1a97f0a6e9d3098b5ff6fe4f8ddcf7a8537f Mon Sep 17 00:00:00 2001 From: David Herrera Date: Fri, 13 Dec 2024 09:12:28 -0500 Subject: [PATCH] Close output buffer when exception occurs during setup (#607) * Close output buffer when exception occurs during setup * CHANGELOG --------- Co-authored-by: Sean Fisher --- CHANGELOG.md | 6 ++++++ .../testing/class-pending-testable-request.php | 14 +++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1562d4ff..6aeec7cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## v1.3.1 - 2024-12-13 + +### Fixed + +- Fixed issue where output buffers were left open when an exception occurred during setup. + ## v1.3.0 - 2024-12-02 ### Added diff --git a/src/mantle/testing/class-pending-testable-request.php b/src/mantle/testing/class-pending-testable-request.php index 8ccc8785..040fdfda 100644 --- a/src/mantle/testing/class-pending-testable-request.php +++ b/src/mantle/testing/class-pending-testable-request.php @@ -219,12 +219,14 @@ protected function format_server_header_key( $name ) { /** * Call the given URI and return the Response. * + * @throws \Exception Exceptions thrown while setting up the WordPress query are re-thrown to the caller. + * * @param string $method Request method. * @param mixed $uri Request URI. * @param array $parameters Request params. * @param array $server Server vars. - * @param array $cookies Cookies to be sent with the request. - * @param string|null $content Request content. + * @param array $cookies Cookies to be sent with the request. + * @param string|null $content Request content. */ public function call( string $method, mixed $uri, array $parameters = [], array $server = [], array $cookies = [], ?string $content = null ): Test_Response { $this->reset_request_state(); @@ -323,7 +325,13 @@ public function call( string $method, mixed $uri, array $parameters = [], array ob_start(); - $this->setup_wordpress_query(); + try { + $this->setup_wordpress_query(); + } catch ( \Exception $e ) { + // If an exception occurs, make sure the output buffer is closed before the exception continues to the caller. + ob_end_clean(); + throw $e; + } if ( $this->rest_api_response ) { // Use the response from the REST API server.