When asserting microtime output as a number, make it a number

microtime is by default a string. Doing a greater then or less than check of that string is a bad idea since it uses the first part (the micro part of microtime) rather then the actual time. This adds a helper to convert microtime output into a float which we can then use to properly compare the output of microtime.

This fixes an intermittent test failure.

See #30336

Merges [30337] to the 4.0 branch.


git-svn-id: https://develop.svn.wordpress.org/branches/4.0@40566 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2017-05-01 23:42:16 +00:00
parent f594b54fcd
commit 9e7df076f4
2 changed files with 14 additions and 4 deletions

View File

@ -124,7 +124,7 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
}
}
}
function flush_cache() {
global $wp_object_cache;
$wp_object_cache->group_ops = array();
@ -446,4 +446,12 @@ class WP_UnitTestCase extends PHPUnit_Framework_TestCase {
$files = $this->files_in_dir( $uploads['basedir'] );
return $files;
}
}
/**
* Helper to Convert a microtime string into a float
*/
protected function _microtime_to_float($microtime ){
$time_array = explode( ' ', $microtime );
return array_sum( $time_array );
}
}

View File

@ -79,7 +79,9 @@ class Tests_Post_getPages extends WP_UnitTestCase {
// This should bump last_changed.
wp_delete_post( $pages[0]->ID );
$this->assertGreaterThan( $last_changed, wp_cache_get( 'last_changed', 'posts' ) );
$old_changed_float = $this->_microtime_to_float( $last_changed );
$new_changed_float = $this->_microtime_to_float( wp_cache_get( 'last_changed', 'posts' ) );
$this->assertGreaterThan( $old_changed_float, $new_changed_float );
$num_queries = $wpdb->num_queries;
$last_changed = wp_cache_get( 'last_changed', 'posts' );
@ -268,4 +270,4 @@ class Tests_Post_getPages extends WP_UnitTestCase {
$exclude6 = get_pages( array( 'exclude_tree' => array( $post_id1, $post_id3 ) ) );
$this->assertCount( 2, $exclude6 );
}
}
}