Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 767b265

Browse files
committed
bug #34179 [Stopwatch] Fixed a bug in StopwatchEvent::getStartTime (TimoBakx)
This PR was merged into the 3.4 branch. Discussion ---------- [Stopwatch] Fixed a bug in StopwatchEvent::getStartTime | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #34088 | License | MIT | Doc PR | N/A When using a `StopwatchEvent` with an `$origin` that's smaller than the first start time, calling `getStartTime()` before ending the event will give `0` instead of the correct number. The proposed fix in #34088 fixes this. Commits ------- b2b7eab [Stopwatch] Fixed a bug in stopwatch event getStartTime
2 parents a1b5079 + b2b7eab commit 767b265

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Symfony/Component/Stopwatch/StopwatchEvent.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,15 @@ public function getPeriods()
154154
*/
155155
public function getStartTime()
156156
{
157-
return isset($this->periods[0]) ? $this->periods[0]->getStartTime() : 0;
157+
if (isset($this->periods[0])) {
158+
return $this->periods[0]->getStartTime();
159+
}
160+
161+
if ($this->started) {
162+
return $this->started[0];
163+
}
164+
165+
return 0;
158166
}
159167

160168
/**

src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,27 @@ public function testStartTime()
152152
$this->assertEqualsWithDelta(0, $event->getStartTime(), self::DELTA);
153153
}
154154

155+
public function testStartTimeWhenStartedLater()
156+
{
157+
$event = new StopwatchEvent(microtime(true) * 1000);
158+
usleep(100000);
159+
$this->assertLessThanOrEqual(0.5, $event->getStartTime());
160+
161+
$event = new StopwatchEvent(microtime(true) * 1000);
162+
usleep(100000);
163+
$event->start();
164+
$event->stop();
165+
$this->assertLessThanOrEqual(101, $event->getStartTime());
166+
167+
$event = new StopwatchEvent(microtime(true) * 1000);
168+
usleep(100000);
169+
$event->start();
170+
usleep(100000);
171+
$this->assertEqualsWithDelta(100, $event->getStartTime(), self::DELTA);
172+
$event->stop();
173+
$this->assertEqualsWithDelta(100, $event->getStartTime(), self::DELTA);
174+
}
175+
155176
public function testInvalidOriginThrowsAnException()
156177
{
157178
$this->expectException('InvalidArgumentException');

0 commit comments

Comments
 (0)