Skip to content

Commit

Permalink
Merge pull request #1467 from jim-parry/fix2
Browse files Browse the repository at this point in the history
Provide time testing within tolerance
  • Loading branch information
jim-parry authored Nov 12, 2018
2 parents b3bc80e + 72d5a98 commit d72700b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
36 changes: 36 additions & 0 deletions system/Test/CIUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,42 @@ public function assertCloseEnough(int $expected, $actual, string $message = '',
$this->assertLessThanOrEqual($tolerance, $difference, $message);
}

/**
* 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 integer $expected
* @param mixed $actual
* @param string $message
* @param integer $tolerance
*
* @throws \Exception
*/
public function assertCloseEnoughString($expected, $actual, string $message = '', int $tolerance = 1)
{
$expected = (string) $expected;
$actual = (string) $actual;
if (strlen($expected) !== strlen($actual))
{
return false;
}

try
{
$expected = (int) substr($expected, -2);
$actual = (int) substr($actual, -2);
$difference = abs($expected - $actual);

$this->assertLessThanOrEqual($tolerance, $difference, $message);
}
catch (\Exception $e)
{
return false;
}
}

/**
* Loads up an instance of CodeIgniter
* and gets the environment setup.
Expand Down
4 changes: 2 additions & 2 deletions tests/system/CLI/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public function testWait()
{
$time = time();
CLI::wait(1, true);
$this->assertEquals(1, time() - $time);
$this->assertCloseEnough(1, time() - $time);

$time = time();
CLI::wait(1);
$this->assertEquals(1, time() - $time);
$this->assertCloseEnough(1, time() - $time);

// Leaving the code fragment below in, to remind myself (or others)
// of what appears to be the most likely path to test this last
Expand Down
4 changes: 2 additions & 2 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function testCreateFromFormatWithTimezoneString()
{
$time = Time::createFromFormat('F j, Y', 'January 15, 2017', 'Europe/London');

$this->assertEquals(date('2017-01-15 H:i:s'), $time->toDateTimeString());
$this->assertCloseEnoughString(date('2017-01-15 H:i:s'), $time->toDateTimeString());
}

public function testCreateFromFormatWithTimezoneObject()
Expand All @@ -207,7 +207,7 @@ public function testCreateFromFormatWithTimezoneObject()

$time = Time::createFromFormat('F j, Y', 'January 15, 2017', $tz);

$this->assertEquals(date('2017-01-15 H:i:s'), $time->toDateTimeString());
$this->assertCloseEnoughString(date('2017-01-15 H:i:s'), $time->toDateTimeString());
}

public function testCreateFromTimestamp()
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 @@ -154,6 +154,17 @@ between expected and actual time is within the prescribed tolerance.::

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

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

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

$timer = new Timer();
$timer->start('longjohn', strtotime('-11 minutes'));
$this->assertCloseEnoughString(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 d72700b

Please sign in to comment.