From 416a2a46df52717280e04be0c301b7e608bea828 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Wed, 25 Jan 2012 14:27:59 +0100 Subject: [PATCH] [Stopwatch] Fix some logic --- .../Component/HttpKernel/Debug/Stopwatch.php | 2 +- .../HttpKernel/Debug/StopwatchEvent.php | 28 ++++++++++++++++--- .../HttpKernel/Debug/StopwatchEventTest.php | 10 ++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php b/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php index 06aa36a8d8..f964d4228b 100644 --- a/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php +++ b/src/Symfony/Component/HttpKernel/Debug/Stopwatch.php @@ -76,7 +76,7 @@ class Stopwatch public function start($name, $category = null) { if (!isset($this->events[$name])) { - $this->events[$name] = new StopwatchEvent($this->origin, $category); + $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category); } return $this->events[$name]->start(); diff --git a/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php b/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php index 9796175ae5..f606f1bcd1 100644 --- a/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php +++ b/src/Symfony/Component/HttpKernel/Debug/StopwatchEvent.php @@ -28,10 +28,12 @@ class StopwatchEvent * * @param integer $origin The origin time in milliseconds * @param string $category The event category + * + * @throws \InvalidArgumentException When the raw time is not valid */ public function __construct($origin, $category = null) { - $this->origin = $origin; + $this->origin = $this->formatTime($origin); $this->category = is_string($category) ? $category : 'default'; $this->started = array(); $this->periods = array(); @@ -132,7 +134,7 @@ class StopwatchEvent */ public function getEndTime() { - return count($this->periods) ? $this->periods[count($this->periods) - 1][1] : 0; + return ($count = count($this->periods)) ? $this->periods[$count - 1][1] : 0; } /** @@ -147,11 +149,29 @@ class StopwatchEvent $total += $period[1] - $period[0]; } - return sprintf('%.1f', $total); + return $this->formatTime($total); } private function getNow() { - return sprintf('%.1f', microtime(true) * 1000 - $this->origin); + return $this->formatTime(microtime(true) * 1000 - $this->origin); + } + + /** + * Formats a time. + * + * @param numerical $time A raw time + * + * @return float The formatted time + * + * @throws \InvalidArgumentException When the raw time is not valid + */ + private function formatTime($time) + { + if (!is_numeric($time)) { + throw new \InvalidArgumentException('The time must be a numerical value'); + } + + return round($time, 1); } } diff --git a/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php b/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php index 82da5dad92..9ec05e5284 100644 --- a/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php +++ b/tests/Symfony/Tests/Component/HttpKernel/Debug/StopwatchEventTest.php @@ -31,7 +31,7 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase $event = new StopwatchEvent(microtime(true) * 1000); $this->assertEquals('default', $event->getCategory()); - $event = new StopwatchEvent(time(), 'cat'); + $event = new StopwatchEvent(microtime(true) * 1000, 'cat'); $this->assertEquals('cat', $event->getCategory()); } @@ -141,4 +141,12 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase $end = $event->getEndTime(); $this->assertTrue($end >= 18 && $end <= 30); } + + /** + * @expectedException \InvalidArgumentException + */ + public function testInvalidOriginThrowsAnException() + { + new StopwatchEvent("abc"); + } }