Skip to content

Commit

Permalink
First pass at some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilsoncc committed Dec 23, 2024
1 parent d030306 commit 06daf91
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tests/phpunit/tests/option/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,4 +548,57 @@ public function test_add_option_clears_the_notoptions_cache() {
$updated_notoptions = wp_cache_get( 'notoptions', 'options' );
$this->assertArrayNotHasKey( $option_name, $updated_notoptions, 'The "foobar" option should not be in the notoptions cache after adding it.' );
}

/**
* Test that get_option() does not hit the external cache multiple times for the same option.
*
* @ticket 62692
*
* @dataProvider data_get_option_does_not_hit_the_external_cache_multiple_times_for_the_same_option
*
* @param bool $option_exists Whether the option should be set.
* @param string $autoload Whether the option should be auto loaded. Default true.
*/
public function test_get_option_does_not_hit_the_external_cache_multiple_times_for_the_same_option( $option_exists = true, $autoload = true ) {
if ( ! wp_using_ext_object_cache() ) {
$this->markTestSkipped( 'This test requires an external object cache.' );
}

if ( ! function_exists( 'wp_cache_get_stats' ) ) {
$this->markTestSkipped( 'This test requires the Memcached PECL extension.' );
}

if ( $option_exists ) {
add_option( 'ticket-62692', 'value', '', $autoload );
}

wp_cache_delete_multiple( array( 'ticket-62692', 'notoptions', 'alloptions' ), 'options' );

$stats = wp_cache_get_stats();
$connections_start = $stats[0]['total_connections'];

Check failure on line 579 in tests/phpunit/tests/option/option.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Functions must not contain multiple empty lines in a row; found 2 empty lines

$call_getter = 10;
while ( $call_getter-- ) {
get_option( 'ticket-62692' );
}

$stats = wp_cache_get_stats();
$connections_end = $stats[0]['total_connections'];

$this->assertSame( 0, $connections_end - $connections_start );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_get_option_does_not_hit_the_external_cache_multiple_times_for_the_same_option() {
return array(
'exists, autoload' => array( true, true ),
'exists, not autoloaded' => array( true, false ),
'does not exist' => array( false ),
);
}
}

0 comments on commit 06daf91

Please sign in to comment.