Skip to content

Commit

Permalink
Merge branch '3.4' into 4.3
Browse files Browse the repository at this point in the history
* 3.4:
  [DependencyInjection] Fix broken references in tests
  Avoid using of kernel after shutdown
  Simplify PHP CS Fixer configuration
  [FrameworkBundle] [HttpKernel] fixed correct EOL and EOM month
  Fix MockFileSessionStorageTest::sessionDir being used after it's unset
  bumped Symfony version to 3.4.34
  updated VERSION for 3.4.33
  update CONTRIBUTORS for 3.4.33
  updated CHANGELOG for 3.4.33
  [Stopwatch] Fixed a bug in stopwatch event getStartTime
  [Stopwatch] Fixed bug in getDuration when counting multiple ongoing periods
  Adding some validations tags on validators.et.xlf
  add missing translation for 94 (it)
  • Loading branch information
nicolas-grekas committed Nov 5, 2019
2 parents 1e4ff45 + efe0af2 commit e96c259
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
18 changes: 12 additions & 6 deletions 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 All @@ -177,12 +185,10 @@ public function getEndTime()
public function getDuration()
{
$periods = $this->periods;
$stopped = \count($periods);
$left = \count($this->started) - $stopped;
$left = \count($this->started);

for ($i = 0; $i < $left; ++$i) {
$index = $stopped + $i;
$periods[] = new StopwatchPeriod($this->started[$index], $this->getNow(), $this->morePrecision);
for ($i = $left - 1; $i >= 0; --$i) {
$periods[] = new StopwatchPeriod($this->started[$i], $this->getNow(), $this->morePrecision);
}

$total = 0;
Expand Down
40 changes: 39 additions & 1 deletion Tests/StopwatchEventTest.php
Expand Up @@ -99,8 +99,25 @@ public function testDurationBeforeStop()
$event->stop();
usleep(50000);
$event->start();
usleep(100000);
$this->assertEqualsWithDelta(100, $event->getDuration(), self::DELTA);
usleep(100000);
$this->assertEqualsWithDelta(200, $event->getDuration(), self::DELTA);
}

public function testDurationWithMultipleStarts()
{
$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(100000);
$event->start();
usleep(100000);
$this->assertEqualsWithDelta(300, $event->getDuration(), self::DELTA);
$event->stop();
$this->assertEqualsWithDelta(300, $event->getDuration(), self::DELTA);
usleep(100000);
$this->assertEqualsWithDelta(400, $event->getDuration(), self::DELTA);
$event->stop();
$this->assertEqualsWithDelta(400, $event->getDuration(), self::DELTA);
}

public function testStopWithoutStart()
Expand Down Expand Up @@ -152,6 +169,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 testHumanRepresentation()
{
$event = new StopwatchEvent(microtime(true) * 1000);
Expand Down

0 comments on commit e96c259

Please sign in to comment.