Skip to content

Commit eb0ef7d

Browse files
author
Paul Bearne
committed
Add PHPUnit tests for wp_cache_* compatibility methods
1 parent 5ba7fb8 commit eb0ef7d

12 files changed

Lines changed: 985 additions & 0 deletions
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Test cases for wp_cache_add_multiple().
4+
*
5+
* @package WordPress
6+
* @subpackage UnitTests
7+
* @since 6.0.0
8+
*
9+
* @group cache
10+
* @group compat
11+
*
12+
* @covers ::wp_cache_add_multiple
13+
*/
14+
class Tests_wp_cache_add_multiple extends WP_UnitTestCase {
15+
16+
/**
17+
* Tests adding multiple values to the cache.
18+
*
19+
* @ticket 65955
20+
*
21+
* @dataProvider data_wp_cache_add_multiple
22+
*
23+
* @param array $initial_cache Array of existing cache items to preload.
24+
* @param array $data Data to add.
25+
* @param string $group Cache group.
26+
* @param array $expected Expected return array.
27+
*/
28+
public function test_wp_cache_add_multiple( array $initial_cache, array $data, string $group, array $expected ): void {
29+
foreach ( $initial_cache as $key => $value ) {
30+
wp_cache_set( $key, $value, $group );
31+
}
32+
33+
$result = wp_cache_add_multiple( $data, $group );
34+
35+
$this->assertSame( $expected, $result );
36+
37+
foreach ( $data as $key => $value ) {
38+
if ( isset( $initial_cache[ $key ] ) ) {
39+
$this->assertSame( $initial_cache[ $key ], wp_cache_get( $key, $group ) );
40+
} else {
41+
$this->assertSame( $value, wp_cache_get( $key, $group ) );
42+
}
43+
}
44+
}
45+
46+
/**
47+
* Data provider for test_wp_cache_add_multiple.
48+
*
49+
* @return array<string, array{
50+
* initial_cache: array<string, mixed>,
51+
* data: array<string, mixed>,
52+
* group: string,
53+
* expected: array<string, bool>,
54+
* }>
55+
*/
56+
public function data_wp_cache_add_multiple(): array {
57+
return array(
58+
'adding multiple items to empty cache' => array(
59+
'initial_cache' => array(),
60+
'data' => array(
61+
'key1' => 'val1',
62+
'key2' => 'val2',
63+
'key3' => 'val3',
64+
),
65+
'group' => 'test_group',
66+
'expected' => array(
67+
'key1' => true,
68+
'key2' => true,
69+
'key3' => true,
70+
),
71+
),
72+
'adding with existing key' => array(
73+
'initial_cache' => array(
74+
'key1' => 'existing_val',
75+
),
76+
'data' => array(
77+
'key1' => 'new_val',
78+
'key2' => 'val2',
79+
),
80+
'group' => 'test_group',
81+
'expected' => array(
82+
'key1' => false,
83+
'key2' => true,
84+
),
85+
),
86+
'empty data array' => array(
87+
'initial_cache' => array(),
88+
'data' => array(),
89+
'group' => 'test_group',
90+
'expected' => array(),
91+
),
92+
);
93+
}
94+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Test cases for wp_cache_delete_multiple().
4+
*
5+
* @package WordPress
6+
* @subpackage UnitTests
7+
* @since 6.0.0
8+
*
9+
* @group cache
10+
* @group compat
11+
*
12+
* @covers ::wp_cache_delete_multiple
13+
*/
14+
class Tests_wp_cache_delete_multiple extends WP_UnitTestCase {
15+
16+
/**
17+
* Tests deleting multiple values from the cache.
18+
*
19+
* @ticket 65955
20+
*
21+
* @dataProvider data_wp_cache_delete_multiple
22+
*
23+
* @param array $initial_cache Array of existing cache items to preload.
24+
* @param array $keys Keys to delete.
25+
* @param string $group Cache group.
26+
* @param array $expected Expected return array.
27+
*/
28+
public function test_wp_cache_delete_multiple( array $initial_cache, array $keys, string $group, array $expected ): void {
29+
foreach ( $initial_cache as $key => $value ) {
30+
wp_cache_set( $key, $value, $group );
31+
}
32+
33+
$result = wp_cache_delete_multiple( $keys, $group );
34+
35+
$this->assertSame( $expected, $result );
36+
37+
foreach ( $keys as $key ) {
38+
$this->assertFalse( wp_cache_get( $key, $group ) );
39+
}
40+
}
41+
42+
/**
43+
* Data provider for test_wp_cache_delete_multiple.
44+
*
45+
* @return array<string, array{
46+
* initial_cache: array<string, mixed>,
47+
* keys: string[],
48+
* group: string,
49+
* expected: array<string, bool>,
50+
* }>
51+
*/
52+
public function data_wp_cache_delete_multiple(): array {
53+
return array(
54+
'deleting existing and missing keys' => array(
55+
'initial_cache' => array(
56+
'key1' => 'val1',
57+
'key2' => 'val2',
58+
),
59+
'keys' => array( 'key1', 'key2', 'missing_key' ),
60+
'group' => 'test_group',
61+
'expected' => array(
62+
'key1' => true,
63+
'key2' => true,
64+
'missing_key' => false,
65+
),
66+
),
67+
'empty keys array' => array(
68+
'initial_cache' => array(
69+
'key1' => 'val1',
70+
),
71+
'keys' => array(),
72+
'group' => 'test_group',
73+
'expected' => array(),
74+
),
75+
);
76+
}
77+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
/**
3+
* Test cases for wp_cache_flush_group().
4+
*
5+
* @package WordPress
6+
* @subpackage UnitTests
7+
* @since 6.1.0
8+
*
9+
* @group cache
10+
* @group compat
11+
*
12+
* @covers ::wp_cache_flush_group
13+
*/
14+
class Tests_wp_cache_flush_group extends WP_UnitTestCase {
15+
16+
/**
17+
* Tests flush group behavior.
18+
*
19+
* @ticket 65955
20+
*
21+
* @dataProvider data_wp_cache_flush_group
22+
*
23+
* @param array $group_to_flush_items Items belonging to the target group.
24+
* @param string $group_to_flush The group to flush.
25+
* @param array $other_group_items Items belonging to a different group.
26+
* @param string $other_group The other group that should not be affected.
27+
*/
28+
public function test_wp_cache_flush_group( array $group_to_flush_items, string $group_to_flush, array $other_group_items, string $other_group ): void {
29+
foreach ( $group_to_flush_items as $key => $value ) {
30+
wp_cache_set( $key, $value, $group_to_flush );
31+
$this->assertSame( $value, wp_cache_get( $key, $group_to_flush ) );
32+
}
33+
34+
foreach ( $other_group_items as $key => $value ) {
35+
wp_cache_set( $key, $value, $other_group );
36+
$this->assertSame( $value, wp_cache_get( $key, $other_group ) );
37+
}
38+
39+
if ( ! wp_cache_supports( 'flush_group' ) ) {
40+
$this->setExpectedIncorrectUsage( 'wp_cache_flush_group' );
41+
$result = wp_cache_flush_group( $group_to_flush );
42+
$this->assertFalse( $result );
43+
} else {
44+
$result = wp_cache_flush_group( $group_to_flush );
45+
$this->assertTrue( $result );
46+
47+
foreach ( $group_to_flush_items as $key => $value ) {
48+
$this->assertFalse( wp_cache_get( $key, $group_to_flush ) );
49+
}
50+
51+
foreach ( $other_group_items as $key => $value ) {
52+
$this->assertSame( $value, wp_cache_get( $key, $other_group ) );
53+
}
54+
}
55+
}
56+
57+
/**
58+
* Data provider for test_wp_cache_flush_group.
59+
*
60+
* @return array<string, array{
61+
* group_to_flush_items: array<string, mixed>,
62+
* group_to_flush: string,
63+
* other_group_items: array<string, mixed>,
64+
* other_group: string,
65+
* }>
66+
*/
67+
public function data_wp_cache_flush_group(): array {
68+
return array(
69+
'flushing group with multiple items' => array(
70+
'group_to_flush_items' => array(
71+
'key1' => 'value1',
72+
'key2' => 'value2',
73+
),
74+
'group_to_flush' => 'group_a',
75+
'other_group_items' => array(
76+
'key3' => 'value3',
77+
),
78+
'other_group' => 'group_b',
79+
),
80+
);
81+
}
82+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Test cases for wp_cache_flush_runtime().
4+
*
5+
* @package WordPress
6+
* @subpackage UnitTests
7+
* @since 6.0.0
8+
*
9+
* @group cache
10+
* @group compat
11+
*
12+
* @covers ::wp_cache_flush_runtime
13+
*/
14+
class Tests_wp_cache_flush_runtime extends WP_UnitTestCase {
15+
16+
/**
17+
* Tests flush runtime behavior.
18+
*
19+
* @ticket 65955
20+
*
21+
* @dataProvider data_wp_cache_flush_runtime
22+
*
23+
* @param array $items Cache items to preload before flushing.
24+
* @param string $group Cache group.
25+
*/
26+
public function test_wp_cache_flush_runtime( array $items, string $group ): void {
27+
foreach ( $items as $key => $value ) {
28+
wp_cache_set( $key, $value, $group );
29+
$this->assertSame( $value, wp_cache_get( $key, $group ) );
30+
}
31+
32+
if ( ! wp_cache_supports( 'flush_runtime' ) ) {
33+
$this->setExpectedIncorrectUsage( 'wp_cache_flush_runtime' );
34+
$result = wp_cache_flush_runtime();
35+
$this->assertFalse( $result );
36+
} else {
37+
$result = wp_cache_flush_runtime();
38+
$this->assertTrue( $result );
39+
40+
foreach ( $items as $key => $value ) {
41+
$this->assertFalse( wp_cache_get( $key, $group ) );
42+
}
43+
}
44+
}
45+
46+
/**
47+
* Data provider for test_wp_cache_flush_runtime.
48+
*
49+
* @return array<string, array{
50+
* items: array<string, mixed>,
51+
* group: string,
52+
* }>
53+
*/
54+
public function data_wp_cache_flush_runtime(): array {
55+
return array(
56+
'flushing single item' => array(
57+
'items' => array(
58+
'key1' => 'value1',
59+
),
60+
'group' => 'test_group',
61+
),
62+
'flushing multiple items' => array(
63+
'items' => array(
64+
'key1' => 'value1',
65+
'key2' => 'value2',
66+
),
67+
'group' => 'test_group',
68+
),
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)