1]); $GLOBALS['post'] = $previous; $post = new WP_Post(['ID' => 7, 'post_content' => '']); $setup = []; Functions\when('setup_postdata')->alias(static function (WP_Post $p) use (&$setup): bool { $setup[] = $p->ID; return true; }); Filters\expectApplied('the_content')->once()->with('')->andReturnUsing( static function () use ($post): string { echo '
echoed
'; return $GLOBALS['post'] === $post ? '' : 'wrong post'; } ); $html = (new PageRenderer())->render($post); self::assertSame('echoed
', $html); self::assertSame($previous, $GLOBALS['post']); self::assertSame([7, 1], $setup); } public function testRestoresEvenWhenRenderingThrows(): void { Functions\when('setup_postdata')->justReturn(true); Functions\expect('wp_reset_postdata')->once(); Filters\expectApplied('the_content')->once()->andReturnUsing(static function (): string { throw new RuntimeException('module crashed'); }); try { (new PageRenderer())->render(new WP_Post(['ID' => 7])); self::fail('exception expected'); } catch (RuntimeException) { self::assertArrayHasKey('post', $GLOBALS); self::assertNull($GLOBALS['post']); } } }