diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index bc8c6b5f47b7..b86b056bacad 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -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; } /** diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php index 86a02b153591..16bfd69404bc 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php @@ -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');