Skip to content

Commit 7cda099

Browse files
committed
[Stopwatch] Add a reset method
1 parent 411d9fa commit 7cda099

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/Symfony/Component/Stopwatch/Stopwatch.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Stopwatch
3030

3131
public function __construct()
3232
{
33-
$this->sections = $this->activeSections = array('__root__' => new Section('__root__'));
33+
$this->reset();
3434
}
3535

3636
/**
@@ -156,4 +156,12 @@ public function getSectionEvents($id)
156156
{
157157
return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : array();
158158
}
159+
160+
/**
161+
* Resets the stopwatch to its original state.
162+
*/
163+
public function reset()
164+
{
165+
$this->sections = $this->activeSections = array('__root__' => new Section('__root__'));
166+
}
159167
}

src/Symfony/Component/Stopwatch/Tests/StopwatchTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,16 @@ public function testReopenANewSectionShouldThrowAnException()
153153
$stopwatch = new Stopwatch();
154154
$stopwatch->openSection('section');
155155
}
156+
157+
public function testReset()
158+
{
159+
$stopwatch = new Stopwatch();
160+
161+
$stopwatch->openSection();
162+
$stopwatch->start('foo', 'cat');
163+
164+
$stopwatch->reset();
165+
166+
$this->assertEquals(new Stopwatch(), $stopwatch);
167+
}
156168
}

0 commit comments

Comments
 (0)