Allow retrieving unstopped stopwatch events

Section:
  - added method getEvent()

Stopwatch:
  - added method getEvent()

StopwatchTest:
  - modified testStart() to test for getEvent() method
  - added testUnknownEvent()
This commit is contained in:
jochenvdv 2014-02-04 12:05:45 +01:00
parent d3d097d659
commit 2efe461a46
2 changed files with 40 additions and 0 deletions

View File

@ -125,6 +125,18 @@ class Stopwatch
return end($this->activeSections)->stopEvent($name)->start();
}
/**
* Returns a specific event by name
*
* @param string $name The event name
*
* @return StopwatchEvent A StopwatchEvent instance
*/
public function getEvent($name)
{
return end($this->activeSections)->getEvent($name);
}
/**
* Gets all events for a given section.
*
@ -293,6 +305,24 @@ class Section
return $this->stopEvent($name)->start();
}
/**
* Returns a specific event by name
*
* @param string $name The event name
*
* @return StopwatchEvent The event
*
* @throws \LogicException When the event is not known
*/
public function getEvent($name)
{
if (!isset($this->events[$name])) {
throw new \LogicException(sprintf('Event "%s" is not known.', $name));
}
return $this->events[$name];
}
/**
* Returns the events from this section.
*

View File

@ -29,6 +29,7 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
$this->assertInstanceof('Symfony\Component\Stopwatch\StopwatchEvent', $event);
$this->assertEquals('cat', $event->getCategory());
$this->assertSame($event, $stopwatch->getEvent('foo'));
}
public function testIsStarted()
@ -92,6 +93,15 @@ class StopwatchTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(200, $event->getDuration(), null, self::DELTA);
}
/**
* @expectedException \LogicException
*/
public function testUnknownEvent()
{
$stopwatch = new Stopwatch();
$stopwatch->getEvent('foo');
}
/**
* @expectedException \LogicException
*/