From 7cda0996631e4d72998feb416c84715014bffe2f Mon Sep 17 00:00:00 2001 From: Jose Gonzalez Date: Sat, 24 Jun 2017 11:52:11 +0100 Subject: [PATCH] [Stopwatch] Add a reset method --- src/Symfony/Component/Stopwatch/Stopwatch.php | 10 +++++++++- .../Component/Stopwatch/Tests/StopwatchTest.php | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Stopwatch/Stopwatch.php b/src/Symfony/Component/Stopwatch/Stopwatch.php index 8ad93165bfd74..562f220f673b1 100644 --- a/src/Symfony/Component/Stopwatch/Stopwatch.php +++ b/src/Symfony/Component/Stopwatch/Stopwatch.php @@ -30,7 +30,7 @@ class Stopwatch public function __construct() { - $this->sections = $this->activeSections = array('__root__' => new Section('__root__')); + $this->reset(); } /** @@ -156,4 +156,12 @@ public function getSectionEvents($id) { return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : array(); } + + /** + * Resets the stopwatch to its original state. + */ + public function reset() + { + $this->sections = $this->activeSections = array('__root__' => new Section('__root__')); + } } diff --git a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php index f7d9171d8bc04..519803d9908d2 100644 --- a/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php +++ b/src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php @@ -153,4 +153,16 @@ public function testReopenANewSectionShouldThrowAnException() $stopwatch = new Stopwatch(); $stopwatch->openSection('section'); } + + public function testReset() + { + $stopwatch = new Stopwatch(); + + $stopwatch->openSection(); + $stopwatch->start('foo', 'cat'); + + $stopwatch->reset(); + + $this->assertEquals(new Stopwatch(), $stopwatch); + } }