Skip to content

Commit 3043a9d

Browse files
committed
Performance/LowExpiryCacheTime: improve the tests
* Add some more variations to the pre-existing tests: - Non-lowercase function call(s). - Fully qualified function calls. - Use PHP 7.3+ trailing comma's in a few function calls. * Add tests for some edge-cases (empty text string, parse error). * Add tests with a few additional PHP syntaxes to document these are handled correctly.
1 parent b52dad5 commit 3043a9d

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.1.inc

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ wp_cache_set( $testing, $data, 'test_group', 5*MINUTE_IN_SECONDS );
1010
wp_cache_set( 123, $data, 'test_group', 5 * MINUTE_IN_SECONDS );
1111
wp_cache_set( 1234, $data, '', 425 );
1212
wp_cache_set( $testing, $data, null, 350 );
13-
wp_cache_set( $testing, $data );
13+
\wp_cache_set( $testing, $data );
1414
wp_cache_set( 'test', $data, $group );
1515

1616
wp_cache_add( 'test', $data, $group, 300 );
1717
wp_cache_add( $testing, $data, 'test_group', 6*MINUTE_IN_SECONDS );
18-
wp_cache_add( 1234, $data, '', 425 );
18+
WP_CACHE_ADD( 1234, $data, '', 425 );
1919
wp_cache_add( $testing, $data, null, 350 );
2020

2121
wp_cache_replace( 'test', $data, $group, 300 );
@@ -30,13 +30,13 @@ wp_cache_set( 123, $data, null, 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
3030
wp_cache_set( $testing, $data, '', 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
3131

3232
wp_cache_add( 'test', $data, $group, 100 ); // Lower than 300.
33-
wp_cache_add( 'test', $data, $group, 2*MINUTE_IN_SECONDS ); // Lower than 300.
33+
\wp_cache_add( 'test', $data, $group, 2*MINUTE_IN_SECONDS ); // Lower than 300.
3434
wp_cache_add( 123, $data, null, 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
3535
wp_cache_add( $testing, $data, '', 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
3636

3737
wp_cache_replace( 'test', $data, $group, 100 ); // Lower than 300.
3838
wp_cache_replace( 'test', $data, $group, 2*MINUTE_IN_SECONDS ); // Lower than 300.
39-
wp_cache_replace( 123, $data, null, 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
39+
WP_CACHE_REPLACE( 123, $data, null, 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
4040
wp_cache_replace( $testing, $data, '', 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
4141

4242
// Test error being reported on the line containing the parameter.
@@ -49,7 +49,7 @@ wp_cache_replace(
4949

5050
// Test calculations with floats.
5151
wp_cache_replace( $testing, $data, '', 7.5 * MINUTE_IN_SECONDS ); // OK.
52-
wp_cache_replace( $testing, $data, '', 500 * 0.1 ); // Bad.
52+
wp_cache_replace( $testing, $data, '', 500 * 0.1, ); // Bad.
5353

5454
// Test comment handling.
5555
wp_cache_add( 'test', $data, $group, /* Deliberately left empty */ ); // OK.
@@ -71,11 +71,11 @@ wp_cache_add(
7171
); // OK.
7272

7373
// Test variable/constant with or without calculation being passed.
74-
wp_cache_set( $key, $data, '', $time ); // Manual inspection warning.
74+
WP_Cache_Set( $key, $data, '', $time ); // Manual inspection warning.
7575
wp_cache_set( $key, $data, '', PREFIX_FIVE_MINUTES ); // Manual inspection warning.
7676
wp_cache_set( $key, $data, '', 20 * $time ); // Manual inspection warning.
77-
wp_cache_set( $key, $data, '', $base + $extra ); // Manual inspection warning.
78-
wp_cache_set( $key, $data, '', 300 + $extra ); // Manual inspection warning.
77+
wp_cache_set( $key, $data, '', $base + $extra, ); // Manual inspection warning.
78+
\wp_cache_set( $key, $data, '', 300 + $extra ); // Manual inspection warning.
7979
wp_cache_set( $key, $data, '', PREFIX_CUSTOM_TIME * 5); // Manual inspection warning.
8080

8181
// Test calculations with additional aritmetic operators.
@@ -84,7 +84,7 @@ wp_cache_add( 'test', $data, $group, WEEK_IN_SECONDS / 3 + HOUR_IN_SECONDS ); /
8484

8585
// Test calculations grouped with parentheses.
8686
wp_cache_set( $key, $data, '', (24 * 60 * 60) ); // OK.
87-
wp_cache_set( $key, $data, '', (-(2 * 60) + 600) ); // OK.
87+
wp_cache_set( $key, $data, '', (-(2 * 60) + 600), ); // OK.
8888
wp_cache_set( $key, $data, '', (2 * 60) ); // Bad.
8989

9090

@@ -113,11 +113,27 @@ wp_cache_set( 'test', $data, $group, month_in_seconds ); // Bad - constants are
113113
wp_cache_set( 'test', $data, $group, HOUR_IN_SECONDS::methodName() ); // Bad - not a constant.
114114
wp_cache_set( 'test', $data, $group, $obj->MONTH_IN_SECONDS ); // Bad - not a constant.
115115
wp_cache_set( 'test', $data, $group, $obj::MONTH_IN_SECONDS ); // Bad - not the WP constant.
116-
wp_cache_set( 'test', $data, $group, PluginNamespace\SubLevel\DAY_IN_SECONDS ); // Bad - not the WP constant.
116+
WP_Cache_Set( 'test', $data, $group, PluginNamespace\SubLevel\DAY_IN_SECONDS, ); // Bad - not the WP constant.
117117

118118
// Test passing negative number as cache time.
119119
wp_cache_set( 'test', $data, $group, -300 ); // Bad.
120120
wp_cache_add( $testing, $data, 'test_group', -6 * MINUTE_IN_SECONDS ); // Bad.
121121

122122
// Test more complex logic in the parameter.
123123
wp_cache_add( $key, $data, '', ($toggle ? 200 : 400) ); // Manual inspection warning.
124+
125+
// Test handling of non-numeric data in text string.
126+
wp_cache_set( 'test', $data, $group, '' ); // Manual inspection warning.
127+
wp_cache_set( 'test', $data, $group, '300 Mulberry Street' ); // Manual inspection warning.
128+
129+
// Test handling of edge case/parse error.
130+
wp_cache_set( 'test', $data, $group,\); // OK, ignore.
131+
132+
// Test handling of some modern PHP syntaxes.
133+
wp_cache_add('test', $data, $group, ...$params); // PHP 5.6 argument unpacking. Manual inspection warning.
134+
wp_cache_add( $key, $data, '', $toggle ?? 400) ); // PHP 7.0 null coalesce. Manual inspection warning.
135+
add_action('my_action', wp_cache_set(...)); // PHP 8.1 first class callable. OK, ignore.
136+
137+
// Looks like a function call, but is a PHP 8.0+ class instantiation via an attribute.
138+
#[WP_Cache_Replace('text', 'data', 'group', 50)]
139+
function foo() {}

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function getWarningList( $testFile = '' ) {
7070
119 => 1,
7171
120 => 1,
7272
123 => 1,
73+
126 => 1,
74+
127 => 1,
75+
133 => 1,
76+
134 => 1,
7377
];
7478

7579
default:

0 commit comments

Comments
 (0)