diff --git a/tests/phpunit/tests/formatting/wpSpacesRegexp.php b/tests/phpunit/tests/formatting/wpSpacesRegexp.php new file mode 100644 index 0000000000000..7c0bf31188768 --- /dev/null +++ b/tests/phpunit/tests/formatting/wpSpacesRegexp.php @@ -0,0 +1,41 @@ +markTestSkipped( 'The exec() function is not available.' ); + } + + $php_binary = defined( 'WP_PHP_BINARY' ) ? WP_PHP_BINARY : escapeshellarg( PHP_BINARY ); + + $php_code = sprintf( + 'require %s; require %s; $filter_calls = 0; add_filter( "wp_spaces_regexp", static function ( $pattern ) use ( &$filter_calls ) { ++$filter_calls; return "[ ]"; } ); $first = wp_spaces_regexp(); $second = wp_spaces_regexp(); echo json_encode( array( "first" => $first, "second" => $second, "calls" => $filter_calls ) );', + var_export( ABSPATH . WPINC . '/plugin.php', true ), + var_export( ABSPATH . WPINC . '/formatting.php', true ) + ); + + $command = $php_binary . ' -r ' . escapeshellarg( $php_code ); + exec( $command, $output, $return_var ); + + $this->assertSame( 0, $return_var, 'PHP execution should succeed.' ); + + $result = json_decode( implode( "\n", $output ), true ); + $this->assertIsArray( $result, 'Subprocess output should decode to an array.' ); + $this->assertSame( '[ ]', $result['first'], 'First call should return filtered pattern.' ); + $this->assertSame( 1, $result['calls'], 'Filter should be called exactly once on first invocation.' ); + $this->assertSame( '[ ]', $result['second'], 'Second call should return cached filtered pattern.' ); + } +}