Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions tests/phpunit/tests/load/timerFloat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* Tests for timer_float().
*
* @group load
*
* @covers ::timer_float
*/
class Tests_Load_TimerFloat extends WP_UnitTestCase {

/**
* Original $_SERVER['REQUEST_TIME_FLOAT'] value.
*
* @var float|null
*/
private $original_request_time_float;

public function set_up() {
parent::set_up();

$this->original_request_time_float = $_SERVER['REQUEST_TIME_FLOAT'] ?? null;
}

public function tear_down() {
if ( null !== $this->original_request_time_float ) {
$_SERVER['REQUEST_TIME_FLOAT'] = $this->original_request_time_float;
} else {
unset( $_SERVER['REQUEST_TIME_FLOAT'] );
}

parent::tear_down();
}

/**
* @ticket 65819
*/
public function test_returns_elapsed_seconds_as_float() {
$_SERVER['REQUEST_TIME_FLOAT'] = microtime( true ) - 1.25;

$result = timer_float();

$this->assertIsFloat( $result );
$this->assertGreaterThanOrEqual( 1.25, $result );
$this->assertLessThanOrEqual( microtime( true ) - $_SERVER['REQUEST_TIME_FLOAT'], $result );
}

/**
* @ticket 65819
*/
public function test_elapsed_time_increases() {
$_SERVER['REQUEST_TIME_FLOAT'] = microtime( true );

$first = timer_float();
usleep( 10000 ); // 10ms.
$second = timer_float();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the sleep-based test_elapsed_time_increases() before landing. The first test already checks elapsed seconds with lower and upper bounds; the second mainly checks that the real clock advances. Keeping the first test is sufficient here. Alternatively, vary the supplied request-start timestamp and assert meaningful bounds without waiting. Simply deleting usleep() while retaining the strict successive-call comparison could produce equal timestamps.


$this->assertGreaterThan( $first, $second );
}
}
Loading