Skip to content

Commit

Permalink
Merge pull request #1361 from jim-parry/test/timing
Browse files Browse the repository at this point in the history
Add timing assertion to CIUnitTestCase
  • Loading branch information
jim-parry authored Oct 26, 2018
2 parents e6a6d64 + c246889 commit ad5ce67
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 11 deletions.
24 changes: 22 additions & 2 deletions system/Test/CIUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function assertHeaderEmitted(string $header, bool $ignoreCase = false): v
break;
}

$this->assertTrue($found,"Didn't find header for {$header}");
$this->assertTrue($found, "Didn't find header for {$header}");
}

/**
Expand All @@ -165,7 +165,27 @@ public function assertHeaderNotEmitted(string $header, bool $ignoreCase = false)
}

$success = ! $found;
$this->assertTrue($success,"Found header for {$header}");
$this->assertTrue($success, "Found header for {$header}");
}

/**
* Custom function to test that two values are "close enough".
* This is intended for extended execution time testing,
* where the result is close but not exactly equal to the
* expected time, for reasons beyond our control.
*
* @param int $expected
* @param mixed $actual
* @param string $message
* @param int $tolerance
*
* @throws \Exception
*/
public function assertCloseEnough(int $expected, $actual, string $message = '', int $tolerance = 1)
{
$difference = abs($expected - (int) floor($actual));

$this->assertLessThanOrEqual($tolerance, $difference, $message);
}

/**
Expand Down
12 changes: 4 additions & 8 deletions tests/system/Debug/TimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,17 @@ public function testThrowsExceptionStoppingNonTimer()
public function testLongExecutionTime()
{
$timer = new Timer();

$timer->start('longjohn', strtotime('-11 minutes'));

// Use floor here to account for fractional differences in seconds.
$this->assertEquals(11 * 60, (int) floor($timer->getElapsedTime('longjohn')));
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));
}

//--------------------------------------------------------------------

public function testLongExecutionTimeThroughCommonFunc()
{
timer()->start('longjohn', strtotime('-11 minutes'));

// Use floor here to account for fractional differences in seconds.
$this->assertEquals(11 * 60, (int) floor(timer()->getElapsedTime('longjohn')));
$timer = new Timer();
$timer->start('longjohn', strtotime('-11 minutes'));
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));
}

//--------------------------------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion user_guide_src/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ system /
- Router/
- RouteCollection #1285, #1355
- Test/
- CIUnitTestCase #1312
- CIUnitTestCase #1312, #1361
- FeatureTestCase #1282
- CodeIgniter #1239 #1337
- Common #1291
Expand Down Expand Up @@ -154,6 +154,7 @@ user_guide_src /source/
PRs merged:
-----------

- #1361 Add timing assertion to CIUnitTestCase
- #1312 Add headerEmitted assertions to CIUnitTestCase
- #1356 Testing/commands
- #1355 Handle duplicate HTTP verb and generic rules properly
Expand Down
11 changes: 11 additions & 0 deletions user_guide_src/source/testing/overview.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ Ensure that a header or cookie was actually emitted::
Note: the test case with this should be `run as a separate process
in PHPunit <https://phpunit.readthedocs.io/en/7.4/annotations.html#runinseparateprocess>`_.

**assertCloseEnough($expected, $actual, $message='', $tolerance=1)**

For extended execution time testing, tests that the absolute difference
between expected and actual time is within the prescribed tolerance.::

$timer = new Timer();
$timer->start('longjohn', strtotime('-11 minutes'));
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));

The above test will allow the actual time to be either 600 or 601 seconds.

Accessing Protected/Private Properties
--------------------------------------

Expand Down

0 comments on commit ad5ce67

Please sign in to comment.