diff --git a/src/Symfony/Component/Stopwatch/Stopwatch.php b/src/Symfony/Component/Stopwatch/Stopwatch.php index 2dba8bc3c1..8631cf8927 100644 --- a/src/Symfony/Component/Stopwatch/Stopwatch.php +++ b/src/Symfony/Component/Stopwatch/Stopwatch.php @@ -258,7 +258,7 @@ class Section */ public function isEventStarted($name) { - return isset($this->events[$name]); + return isset($this->events[$name]) && $this->events[$name]->isStarted(); } /** diff --git a/src/Symfony/Component/Stopwatch/StopwatchEvent.php b/src/Symfony/Component/Stopwatch/StopwatchEvent.php index 40ed0d6a6a..7adfdf0bb8 100644 --- a/src/Symfony/Component/Stopwatch/StopwatchEvent.php +++ b/src/Symfony/Component/Stopwatch/StopwatchEvent.php @@ -106,6 +106,16 @@ class StopwatchEvent return $this; } + /** + * Checks if the event was started + * + * @return bool + */ + public function isStarted() + { + return !empty($this->started); + } + /** * Stops the current period and then starts a new one. * diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php index 1ba69d19fa..5785827730 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchEventTest.php @@ -91,6 +91,19 @@ class StopwatchEventTest extends \PHPUnit_Framework_TestCase $event->stop(); } + public function testIsStarted() + { + $event = new StopwatchEvent(microtime(true) * 1000); + $event->start(); + $this->assertTrue($event->isStarted()); + } + + public function testIsNotStarted() + { + $event = new StopwatchEvent(microtime(true) * 1000); + $this->assertFalse($event->isStarted()); + } + public function testEnsureStopped() { // this also test overlap between two periods diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php index 2f5ec2d2f5..20d5fb506f 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php @@ -44,6 +44,28 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase $this->assertFalse($stopwatch->isStarted('foo')); } + public function testIsNotStartedEvent() + { + $stopwatch = new Stopwatch(); + + $sections = new \ReflectionProperty('Symfony\Component\Stopwatch\Stopwatch', 'sections'); + $sections->setAccessible(true); + $section = $sections->getValue($stopwatch); + + $events = new \ReflectionProperty('Symfony\Component\Stopwatch\Section', 'events'); + $events->setAccessible(true); + $events->setValue( + end($section), + array( + 'foo' => + $this->getMockBuilder('Symfony\Component\Stopwatch\StopwatchEvent') + ->setConstructorArgs([microtime(true) * 1000]) + ->getMock()) + ); + + $this->assertFalse($stopwatch->isStarted('foo')); + } + public function testStop() { $stopwatch = new Stopwatch();