[Stopwatch] Fix checking started events

This commit is contained in:
Alexander Kotynia 2013-04-16 23:54:58 +03:00 committed by Fabien Potencier
parent d060c40503
commit b6265427c6
4 changed files with 46 additions and 1 deletions

View File

@ -258,7 +258,7 @@ class Section
*/
public function isEventStarted($name)
{
return isset($this->events[$name]);
return isset($this->events[$name]) && $this->events[$name]->isStarted();
}
/**

View File

@ -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.
*

View File

@ -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

View File

@ -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();