77class IndividualTimerTest extends TestCase {
88 public function testConstructWithFutureTime () {
99 $ epoch = microtime (true );
10- $ epochPlus50ms = $ epoch + 0.05 ;
11- $ sut = new IndividualTimer ($ epochPlus50ms );
12- self ::assertEquals ($ epochPlus50ms , $ sut ->getNextRunTime ());
10+ $ epochPlus5s = $ epoch + 5 ;
11+ $ sut = new IndividualTimer (5 );
12+ self ::assertEquals (
13+ // The timer must be scheduled within one hundredth of a second of the expectation:
14+ round ($ epochPlus5s , 2 ),
15+ round ($ sut ->getNextRunTime (), 2 )
16+ );
1317 }
1418
1519 public function testConstructWithPastTime () {
1620 $ epoch = microtime (true );
17- $ epochMinus50ms = $ epoch - 0.05 ;
18- $ sut = new IndividualTimer ($ epochMinus50ms );
19- self ::assertEquals ($ epochMinus50ms , $ sut ->getNextRunTime ());
21+ $ epochMinus5s = $ epoch - 5 ;
22+ $ sut = new IndividualTimer (-5 );
23+ self ::assertEquals (
24+ round ($ epochMinus5s , 2 ),
25+ round ($ sut ->getNextRunTime (), 2 )
26+ );
2027 }
2128
2229 public function testTickWithFutureTime () {
23- $ sut = new IndividualTimer (microtime ( true ) + 1 );
30+ $ sut = new IndividualTimer (100 );
2431 self ::assertFalse ($ sut ->tick ());
2532 }
2633
2734 public function testTickWithPastTime () {
28- $ sut = new IndividualTimer (microtime ( true ) - 1 );
35+ $ sut = new IndividualTimer (- 100 );
2936 self ::assertTrue ($ sut ->tick ());
3037 }
3138
3239 public function testIsScheduledFalseAfterRunning () {
33- $ sut = new IndividualTimer (microtime ( true ) );
40+ $ sut = new IndividualTimer (0 );
3441 self ::assertTrue ($ sut ->isScheduled ());
3542 $ sut ->tick ();
3643 self ::assertFalse ($ sut ->isScheduled ());
3744 }
3845
3946 public function testIsScheduledTrueAfterRunningMultipleScheduled () {
40- $ sut = new IndividualTimer (microtime ( true ) + 1 );
41- $ sut ->addTriggerTime (microtime ( true ) + 2 );
42- $ sut ->addTriggerTime (microtime ( true ) + 3 );
47+ $ sut = new IndividualTimer (1 );
48+ $ sut ->addTriggerTime (2 );
49+ $ sut ->addTriggerTime (3 );
4350 self ::assertTrue ($ sut ->isScheduled ());
4451 $ sut ->tick ();
4552 $ sut ->tick ();
@@ -51,8 +58,9 @@ public function testIsScheduledTrueAfterRunningMultipleScheduled() {
5158 public function testIsScheduledFalseAfterRunningMultipleScheduledAndTimeAdvances () {
5259 $ epoch = 1000 ;
5360
54- $ sut = new IndividualTimer (1001 );
61+ $ sut = new IndividualTimer ();
5562 $ sut ->setTimeFunction (function () use (&$ epoch ) { return ++$ epoch ; });
63+ $ sut ->addTriggerTime (1001 );
5664 $ sut ->addTriggerTime (1002 );
5765 $ sut ->addTriggerTime (1003 );
5866 self ::assertTrue ($ sut ->isScheduled ());
@@ -68,9 +76,7 @@ public function testIsScheduledFalseAfterRunningMultipleScheduledAndTimeAdvances
6876 public function testAddCallbackNotTriggeredInFuture () {
6977 $ callbackCount = 0 ;
7078
71- $ epoch = 1000 ;
72- $ sut = new IndividualTimer (1001 );
73- $ sut ->setTimeFunction (fn () => $ epoch );
79+ $ sut = new IndividualTimer (1 );
7480 $ sut ->addCallback (function () use (&$ callbackCount ) {
7581 $ callbackCount ++;
7682 });
@@ -81,9 +87,7 @@ public function testAddCallbackNotTriggeredInFuture() {
8187 public function testAddCallbackTriggeredInPast () {
8288 $ callbackCount = 0 ;
8389
84- $ epoch = 1000 ;
85- $ sut = new IndividualTimer (999 );
86- $ sut ->setTimeFunction (fn () => $ epoch );
90+ $ sut = new IndividualTimer (0 );
8791 $ sut ->addCallback (function () use (&$ callbackCount ) {
8892 $ callbackCount ++;
8993 });
@@ -95,7 +99,8 @@ public function testAddCallbackTriggeredAtCorrectTime() {
9599 $ callbackCount = 0 ;
96100
97101 $ epoch = 1000 ;
98- $ sut = new IndividualTimer ($ epoch );
102+ $ sut = new IndividualTimer ();
103+ $ sut ->addTriggerTime ($ epoch );
99104 $ sut ->addTriggerTime ($ epoch + 1 );
100105 $ sut ->addTriggerTime ($ epoch + 2 );
101106 $ sut ->addTriggerTime ($ epoch + 5 );
0 commit comments