diff --git a/packages/phpunit-wp-hooks/README.md b/packages/phpunit-wp-hooks/README.md index c8e52b4..a359db0 100644 --- a/packages/phpunit-wp-hooks/README.md +++ b/packages/phpunit-wp-hooks/README.md @@ -55,7 +55,7 @@ Where 15 is the priority from the filter. If you forget this part the default pr To isolate a hook to reduce it to a single callback it is possible by adding `@hook-isolated` annotation on the test method: ```php /** - * @hook-isolate my-event myCallback 15 + * @hook-isolated my-event myCallback 15 */ ``` Where 15 is the priority from the filter. If you forget this part the default priority will be 10. diff --git a/packages/phpunit-wp-hooks/src/IsolateHookTrait.php b/packages/phpunit-wp-hooks/src/IsolateHookTrait.php index f80a9a1..6c6dad6 100644 --- a/packages/phpunit-wp-hooks/src/IsolateHookTrait.php +++ b/packages/phpunit-wp-hooks/src/IsolateHookTrait.php @@ -15,16 +15,26 @@ protected function unregisterAllCallbacksExcept( $event_name, $method_name, $pri global $wp_filter; $this->original_wp_filter[ $event_name ] = $wp_filter[ $event_name ]->callbacks; - foreach ( $this->original_wp_filter[ $event_name ][ $priority ] as $key => $config ) { + $callbacks = []; + + foreach ( $this->original_wp_filter[ $event_name ][ $priority ] as $key => $config ) { // Skip if not this tests callback. if ( substr( $key, - strlen( $method_name ) ) !== $method_name ) { continue; } - $wp_filter[ $event_name ]->callbacks = [ - $priority => [ $key => $config ], - ]; + if(key_exists($priority, $callbacks)) { + $callbacks = [ + $priority => array_merge_recursive($callbacks[$priority], [ $key => $config ]) + ]; + }else { + $callbacks = [ + $priority => [ $key => $config ], + ]; + } + + $wp_filter[ $event_name ]->callbacks = $callbacks; } try {