Skip to content

Commit

Permalink
Add method to fetch lap time from Stopwatch without stopping
Browse files Browse the repository at this point in the history
  • Loading branch information
rodnaph committed Mar 25, 2024
1 parent 21e1f13 commit 9cef2f2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Stopwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ public function isRunning(): bool
return $this->startTime !== null;
}

public function getLapTime(): Duration
{
if ($this->startTime === null) {
return Duration::zero();
}

return Duration::between($this->startTime, $this->clock->getTime());
}

/**
* Returns the total elapsed time.
*
Expand Down
37 changes: 37 additions & 0 deletions tests/StopwatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function testConstructorWithNullClock(): void

self::assertNull($stopwatch->getStartTime());
self::assertFalse($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(0, 0, $stopwatch->getElapsedTime());
}

Expand All @@ -35,6 +36,7 @@ public function testNew(): Stopwatch

self::assertNull($stopwatch->getStartTime());
self::assertFalse($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(0, 0, $stopwatch->getElapsedTime());

return $stopwatch;
Expand All @@ -51,6 +53,7 @@ public function testStart(Stopwatch $stopwatch): Stopwatch

self::assertInstantIs(1000, 1, $stopwatch->getStartTime());
self::assertTrue($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(0, 0, $stopwatch->getElapsedTime());

return $stopwatch;
Expand All @@ -70,13 +73,28 @@ public function testElapsedTimeWhileRunning(Stopwatch $stopwatch): Stopwatch
return $stopwatch;
}

/**
* @depends testStart
*/
public function testLapTimeWhileRunning(Stopwatch $stopwatch): Stopwatch
{
self::setClockTime(2000, 0);

self::assertInstantIs(1000, 1, $stopwatch->getStartTime());
self::assertTrue($stopwatch->isRunning());
self::assertDurationIs(999, 999999999, $stopwatch->getLapTime());

return $stopwatch;
}

public function testStopWithNullStartTime(): void
{
$stopwatch = new Stopwatch();

self::assertDurationIs(0, 0, $stopwatch->stop());
self::assertNull($stopwatch->getStartTime());
self::assertFalse($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(0, 0, $stopwatch->getElapsedTime());
}

Expand All @@ -90,6 +108,7 @@ public function testStop(Stopwatch $stopwatch): Stopwatch
self::assertDurationIs(2000, 1, $stopwatch->stop());
self::assertNull($stopwatch->getStartTime());
self::assertFalse($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(2000, 1, $stopwatch->getElapsedTime());

return $stopwatch;
Expand All @@ -104,6 +123,7 @@ public function testFrozenAfterStop(Stopwatch $stopwatch): Stopwatch

self::assertNull($stopwatch->getStartTime());
self::assertFalse($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(2000, 1, $stopwatch->getElapsedTime());

return $stopwatch;
Expand All @@ -120,6 +140,7 @@ public function testRestart(Stopwatch $stopwatch): Stopwatch

self::assertInstantIs(5000, 9, $stopwatch->getStartTime());
self::assertTrue($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(2000, 1, $stopwatch->getElapsedTime());

return $stopwatch;
Expand All @@ -139,6 +160,20 @@ public function testElapsedTimeWhileRunningAfterRestart(Stopwatch $stopwatch): S
return $stopwatch;
}

/**
* @depends testRestart
*/
public function testLapTimeWhileRunningAfterRestart(Stopwatch $stopwatch): Stopwatch
{
self::setClockTime(5001, 10);

self::assertInstantIs(5000, 9, $stopwatch->getStartTime());
self::assertTrue($stopwatch->isRunning());
self::assertDurationIs(1, 1, $stopwatch->getLapTime());

return $stopwatch;
}

/**
* @depends testElapsedTimeWhileRunningAfterRestart
*/
Expand All @@ -149,6 +184,7 @@ public function testStopAgain(Stopwatch $stopwatch): Stopwatch
self::assertDurationIs(2, 11, $stopwatch->stop());
self::assertNull($stopwatch->getStartTime());
self::assertFalse($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(2002, 12, $stopwatch->getElapsedTime());

return $stopwatch;
Expand All @@ -163,6 +199,7 @@ public function testFrozenAfterSecondStop(Stopwatch $stopwatch): void

self::assertNull($stopwatch->getStartTime());
self::assertFalse($stopwatch->isRunning());
self::assertDurationIs(0, 0, $stopwatch->getLapTime());
self::assertDurationIs(2002, 12, $stopwatch->getElapsedTime());
}

Expand Down

0 comments on commit 9cef2f2

Please sign in to comment.