test: fix BoardDataProviderTest WP_Query/WP_Post stub incompatibility

Functions\when('WP_Query') cannot intercept 'new WP_Query()' class
instantiation — Brain\Monkey only patches global functions, not
constructor calls. The board data provider's instanceof WP_Post check
also filtered out all stdClass mocks.

Fix:
- Add WP_Query:: static FIFO to bootstrap so tests
  can inject WP_Post arrays per-test without Brain\Monkey.
- Add WP_Post stub class to bootstrap (safe: not a function, no
  Patchwork conflict).
- Update BoardDataProviderTest to use WP_Post instances + queue
  instead of Functions\when('WP_Query').

Agent-Session: phase2-admin-m1-engineer
This commit is contained in:
peptiderepo
2026-06-12 14:08:36 +07:00
parent 56ee438449
commit 29a334b1ed
2 changed files with 61 additions and 25 deletions

View File

@@ -25,6 +25,9 @@ class BoardDataProviderTest extends BaseTestCase {
$this->wpdb = $this->create_mock_wpdb();
$GLOBALS['wpdb'] = $this->wpdb;
// Reset WP_Query test queue before each test.
\WP_Query::$_test_posts_queue = [];
Functions\when( 'get_option' )->alias(
function ( string $name, $default = false ) {
$map = array(
@@ -38,9 +41,6 @@ class BoardDataProviderTest extends BaseTestCase {
// Transient stub: by default no active run.
Functions\when( 'get_transient' )->justReturn( false );
// WP_Query stub: return empty by default; individual tests override.
Functions\when( 'WP_Query' )->justReturn( (object) array( 'posts' => array() ) );
// Admin URL stub.
Functions\when( 'admin_url' )->alias(
function ( string $path = '' ) {
@@ -55,6 +55,7 @@ class BoardDataProviderTest extends BaseTestCase {
}
protected function tearDown(): void {
\WP_Query::$_test_posts_queue = [];
unset( $GLOBALS['wpdb'] );
parent::tearDown();
}
@@ -139,15 +140,14 @@ class BoardDataProviderTest extends BaseTestCase {
* Draft posts with the _prautoblogger_generated meta appear in In Review.
*/
public function test_in_review_cards_from_draft_posts(): void {
$post = new \stdClass();
$post->ID = 42;
$post->post_title = 'Test Draft Post';
$post->post_date_gmt = '2026-06-12 10:00:00';
$post = new \WP_Post();
$post->ID = 42;
$post->post_title = 'Test Draft Post';
$post->post_date_gmt = '2026-06-12 10:00:00';
$post->post_status = 'draft';
// WP_Query returns our fake post.
Functions\when( 'WP_Query' )->justReturn(
(object) array( 'posts' => array( $post ) )
);
// Inject post into WP_Query queue: first WP_Query() call returns this post.
\WP_Query::$_test_posts_queue[] = array( $post );
Functions\when( 'get_post_meta' )->alias(
function ( $post_id, $key, $single ) use ( $post ) {
@@ -183,14 +183,14 @@ class BoardDataProviderTest extends BaseTestCase {
* Published posts within the window appear in Published.
*/
public function test_published_cards_within_window(): void {
$post = new \stdClass();
$post->ID = 77;
$post->post_title = 'Published Article';
$post->post_date_gmt = gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS );
$post = new \WP_Post();
$post->ID = 77;
$post->post_title = 'Published Article';
$post->post_date_gmt = gmdate( 'Y-m-d H:i:s', time() - DAY_IN_SECONDS );
$post->post_status = 'publish';
Functions\when( 'WP_Query' )->justReturn(
(object) array( 'posts' => array( $post ) )
);
// Inject post into WP_Query queue: first WP_Query() call returns this post.
\WP_Query::$_test_posts_queue[] = array( $post );
Functions\when( 'get_post_meta' )->alias(
function ( $post_id, $key, $single ) use ( $post ) {
@@ -269,8 +269,6 @@ class BoardDataProviderTest extends BaseTestCase {
$this->wpdb->method( 'prepare' )->willReturn( 'SELECT 1' );
$this->wpdb->method( 'get_results' )->willReturn( array() );
Functions\when( 'WP_Query' )->justReturn( (object) array( 'posts' => array() ) );
$provider = new \PRAutoBlogger_Board_Data_Provider();
$snapshot = $provider->get_board_snapshot();
@@ -286,8 +284,6 @@ class BoardDataProviderTest extends BaseTestCase {
$this->wpdb->method( 'prepare' )->willReturn( 'SELECT 1' );
$this->wpdb->method( 'get_results' )->willReturn( array() );
Functions\when( 'WP_Query' )->justReturn( (object) array( 'posts' => array() ) );
$provider = new \PRAutoBlogger_Board_Data_Provider();
$snapshot = $provider->get_board_snapshot();