Skip to content

Commit

Permalink
[Stopwatch] Fixed a bug in stopwatch event getStartTime
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoBakx committed Oct 30, 2019
1 parent 2ecd793 commit b2b7eab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Symfony/Component/Stopwatch/StopwatchEvent.php
Expand Up @@ -154,7 +154,15 @@ public function getPeriods()
*/
public function getStartTime()
{
return isset($this->periods[0]) ? $this->periods[0]->getStartTime() : 0;
if (isset($this->periods[0])) {
return $this->periods[0]->getStartTime();
}

if ($this->started) {
return $this->started[0];
}

return 0;
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php
Expand Up @@ -152,6 +152,27 @@ public function testStartTime()
$this->assertEqualsWithDelta(0, $event->getStartTime(), self::DELTA);
}

public function testStartTimeWhenStartedLater()
{
$event = new StopwatchEvent(microtime(true) * 1000);
usleep(100000);
$this->assertLessThanOrEqual(0.5, $event->getStartTime());

$event = new StopwatchEvent(microtime(true) * 1000);
usleep(100000);
$event->start();
$event->stop();
$this->assertLessThanOrEqual(101, $event->getStartTime());

$event = new StopwatchEvent(microtime(true) * 1000);
usleep(100000);
$event->start();
usleep(100000);
$this->assertEqualsWithDelta(100, $event->getStartTime(), self::DELTA);
$event->stop();
$this->assertEqualsWithDelta(100, $event->getStartTime(), self::DELTA);
}

public function testInvalidOriginThrowsAnException()
{
$this->expectException('InvalidArgumentException');
Expand Down

0 comments on commit b2b7eab

Please sign in to comment.