Skip to content

Commit ad5ce67

Browse files
authored
Merge pull request #1361 from jim-parry/test/timing
Add timing assertion to CIUnitTestCase
2 parents e6a6d64 + c246889 commit ad5ce67

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

system/Test/CIUnitTestCase.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function assertHeaderEmitted(string $header, bool $ignoreCase = false): v
139139
break;
140140
}
141141

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

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

167167
$success = ! $found;
168-
$this->assertTrue($success,"Found header for {$header}");
168+
$this->assertTrue($success, "Found header for {$header}");
169+
}
170+
171+
/**
172+
* Custom function to test that two values are "close enough".
173+
* This is intended for extended execution time testing,
174+
* where the result is close but not exactly equal to the
175+
* expected time, for reasons beyond our control.
176+
*
177+
* @param int $expected
178+
* @param mixed $actual
179+
* @param string $message
180+
* @param int $tolerance
181+
*
182+
* @throws \Exception
183+
*/
184+
public function assertCloseEnough(int $expected, $actual, string $message = '', int $tolerance = 1)
185+
{
186+
$difference = abs($expected - (int) floor($actual));
187+
188+
$this->assertLessThanOrEqual($tolerance, $difference, $message);
169189
}
170190

171191
/**

tests/system/Debug/TimerTest.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,17 @@ public function testThrowsExceptionStoppingNonTimer()
9393
public function testLongExecutionTime()
9494
{
9595
$timer = new Timer();
96-
9796
$timer->start('longjohn', strtotime('-11 minutes'));
98-
99-
// Use floor here to account for fractional differences in seconds.
100-
$this->assertEquals(11 * 60, (int) floor($timer->getElapsedTime('longjohn')));
97+
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));
10198
}
10299

103100
//--------------------------------------------------------------------
104101

105102
public function testLongExecutionTimeThroughCommonFunc()
106103
{
107-
timer()->start('longjohn', strtotime('-11 minutes'));
108-
109-
// Use floor here to account for fractional differences in seconds.
110-
$this->assertEquals(11 * 60, (int) floor(timer()->getElapsedTime('longjohn')));
104+
$timer = new Timer();
105+
$timer->start('longjohn', strtotime('-11 minutes'));
106+
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));
111107
}
112108

113109
//--------------------------------------------------------------------

user_guide_src/source/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ system /
7171
- Router/
7272
- RouteCollection #1285, #1355
7373
- Test/
74-
- CIUnitTestCase #1312
74+
- CIUnitTestCase #1312, #1361
7575
- FeatureTestCase #1282
7676
- CodeIgniter #1239 #1337
7777
- Common #1291
@@ -154,6 +154,7 @@ user_guide_src /source/
154154
PRs merged:
155155
-----------
156156

157+
- #1361 Add timing assertion to CIUnitTestCase
157158
- #1312 Add headerEmitted assertions to CIUnitTestCase
158159
- #1356 Testing/commands
159160
- #1355 Handle duplicate HTTP verb and generic rules properly

user_guide_src/source/testing/overview.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,17 @@ Ensure that a header or cookie was actually emitted::
143143
Note: the test case with this should be `run as a separate process
144144
in PHPunit <https://phpunit.readthedocs.io/en/7.4/annotations.html#runinseparateprocess>`_.
145145

146+
**assertCloseEnough($expected, $actual, $message='', $tolerance=1)**
147+
148+
For extended execution time testing, tests that the absolute difference
149+
between expected and actual time is within the prescribed tolerance.::
150+
151+
$timer = new Timer();
152+
$timer->start('longjohn', strtotime('-11 minutes'));
153+
$this->assertCloseEnough(11 * 60, $timer->getElapsedTime('longjohn'));
154+
155+
The above test will allow the actual time to be either 600 or 601 seconds.
156+
146157
Accessing Protected/Private Properties
147158
--------------------------------------
148159

0 commit comments

Comments
 (0)