Include running periods in duration

StopwatchEvent:
  - method getDuration() now includes periods that are not stopped yet

StopwatchEventTest:
  - added testDurationBeforeStop()
This commit is contained in:
jochenvdv 2014-02-04 00:11:49 +01:00
parent bea1537964
commit d3d097d659
2 changed files with 26 additions and 1 deletions

View File

@ -171,8 +171,17 @@ class StopwatchEvent
*/
public function getDuration()
{
$periods = $this->periods;
$stopped = count($periods);
$left = count($this->started) - $stopped;
for ($i = 0; $i < $left; $i++) {
$index = $stopped + $i;
$periods[] = new StopwatchPeriod($this->started[$index], $this->getNow());
}
$total = 0;
foreach ($this->periods as $period) {
foreach ($periods as $period) {
$total += $period->getDuration();
}

View File

@ -82,6 +82,22 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
}
public function testDurationBeforeStop()
{
$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(200000);
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
$event = new StopwatchEvent(microtime(true) * 1000);
$event->start();
usleep(100000);
$event->stop();
$event->start();
usleep(100000);
$this->assertEquals(100, $event->getDuration(), null, self::DELTA);
}
/**
* @expectedException \LogicException
*/