Skip to content

Commit

Permalink
Add test for od_get_cache_purge_post_id()
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Nov 14, 2024
1 parent eee4761 commit 28a6491
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions plugins/optimization-detective/tests/test-detection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,76 @@

class Test_OD_Detection extends WP_UnitTestCase {

/**
* Data provider.
*
* @return array<string, array{set_up: Closure, expected_is_query_object: bool, expected_query_object_class: string|null}>
*/
public function data_provider_od_get_cache_purge_post_id(): array {
return array(
'singular' => array(
'set_up' => function () {
$post_id = self::factory()->post->create();
$this->go_to( get_permalink( $post_id ) );
return $post_id;
},
'expected_is_query_object' => true,
'expected_query_object_class' => WP_Post::class,
),
'home' => array(
'set_up' => function () {
$post_id = self::factory()->post->create();
$this->go_to( home_url() );
return $post_id;
},
'expected_is_query_object' => false,
'expected_query_object_class' => null,
),
'category' => array(
'set_up' => function () {
$cat_id = self::factory()->category->create();
$post_id = self::factory()->post->create();
wp_set_post_categories( $post_id, array( $cat_id ) );
$this->go_to( get_category_link( $cat_id ) );
return $post_id;
},
'expected_is_query_object' => false,
'expected_query_object_class' => WP_Term::class,
),
'not_found' => array(
'set_up' => function () {
$this->go_to( '/this-page-does-not-exist' );
return null;
},
'expected_is_query_object' => false,
'expected_query_object_class' => null,
),
);
}

/**
* Tests od_get_cache_purge_post_id().
*
* @covers ::od_get_cache_purge_post_id
*
* @dataProvider data_provider_od_get_cache_purge_post_id
*/
public function test_od_get_cache_purge_post_id( Closure $set_up, bool $expected_is_query_object, ?string $expected_query_object_class ): void {
$expected = $set_up();
$this->assertSame( $expected, od_get_cache_purge_post_id() );
if ( $expected_is_query_object ) {
$this->assertSame( $expected, get_queried_object_id() );
} else {
$this->assertNotSame( $expected, get_queried_object_id() );
}

if ( null === $expected_query_object_class ) {
$this->assertNull( get_queried_object() );
} else {
$this->assertSame( $expected_query_object_class, get_class( get_queried_object() ) );
}
}

/**
* Data provider.
*
Expand Down

0 comments on commit 28a6491

Please sign in to comment.