Skip to content

Commit

Permalink
Assorted bugs when using WordPress core test framework (#526)
Browse files Browse the repository at this point in the history
* Fix two issues with the 1.x release: the timing of set_up being called, the pre value not being respected, and streaming repsonse returning WP_Error

* CHANGELOG

* Testing CI

* Rector fix
  • Loading branch information
srtfisher authored Apr 9, 2024
1 parent 7c0bfaa commit 4dd4233
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v1.0.1 - 2024-04-09

### Fixed

- Changed the timing of the `set_up` method being called in tests to be after
the database transaction is started.
- Allow other use of the `pre_http_request` filter when preventing external
requests during testing.
- Fixed an issue with the streamed HTTP response not being converted to a
`WP_Error` when needed.

## v1.0.0 - 2024-04-04

### Added
Expand Down
2 changes: 0 additions & 2 deletions src/mantle/console/concerns/trait-interacts-with-io.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,6 @@ public function set_input( InputInterface $input ): void {

/**
* Retrieve the output interface.
*
* @return OutputInterface|Output_Style
*/
public function output(): OutputInterface|Output_Style {
return $this->output;
Expand Down
2 changes: 1 addition & 1 deletion src/mantle/events/trait-wordpress-action.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function validate_argument_type( $argument, ReflectionParameter $param
throw new RuntimeException( $type::class . ' is not a supported type-hint.' );
}

if ( $type instanceof ReflectionNamedType && $argument_type === $type->getName() ) {
if ( $argument_type === $type->getName() ) {
return $argument;
}

Expand Down
11 changes: 6 additions & 5 deletions src/mantle/testing/class-test-case.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,6 @@ protected function setUp(): void {

parent::setUp();

// Call the PHPUnit 8 'set_up' method if it exists.
if ( method_exists( $this, 'set_up' ) ) {
$this->set_up();
}

if ( ! isset( $this->app ) ) {
$this->refresh_application();
}
Expand All @@ -171,6 +166,11 @@ function( $trait ): void {

remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
add_filter( 'wp_die_handler', [ WP_Die::class, 'get_handler' ] );

// Call the PHPUnit 8 'set_up' method if it exists.
if ( method_exists( $this, 'set_up' ) ) {
$this->set_up();
}
}

/**
Expand Down Expand Up @@ -231,6 +231,7 @@ function( $trait ): void {

parent::tearDown();

// Reset the application instance after everything else.
if ( $this->app ) {
$this->app = null;

Expand Down
11 changes: 10 additions & 1 deletion src/mantle/testing/concerns/trait-interacts-with-requests.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ public function mock_response( string $body = '', array $headers = [] ): Mock_Ht
* @throws RuntimeException If the request was made without a matching faked request.
*/
public function pre_http_request( $preempt, $request_args, $url ) {
// Bail early if the preemption is already set.
if ( false !== $preempt ) {
return $preempt;
}

$request = new Request( $request_args, $url );

$this->recorded_requests[] = $request;
Expand All @@ -199,7 +204,11 @@ public function pre_http_request( $preempt, $request_args, $url ) {
// If the request is for streaming the response to a file, store the
// response body in the requested file.
if ( ! is_wp_error( $stub ) && ! empty( $request_args['stream'] ) ) {
return $this->store_streamed_response( $url, $stub, $request_args );
try {
return $this->store_streamed_response( $url, $stub, $request_args );
} catch ( RuntimeException $e ) {
return new WP_Error( 'http_request_failed', $e->getMessage() );
}
}

return $stub;
Expand Down

0 comments on commit 4dd4233

Please sign in to comment.