feature #23285 [Stopwatch] Add a reset method (jmgq)

This PR was merged into the 3.4 branch.

Discussion
----------

[Stopwatch] Add a reset method

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #23284
| License       | MIT
| Doc PR        | symfony/symfony-docs#8082

Let the Stopwatch to be reset to its original state, deleting all the data measured so far. This allows the stopwatch to be reusable, and emulates an actual stopwatch's reset button.

Commits
-------

7cda099663 [Stopwatch] Add a reset method
This commit is contained in:
Fabien Potencier 2017-07-03 10:03:20 +03:00
commit b23ddfefce
2 changed files with 21 additions and 1 deletions

View File

@ -30,7 +30,7 @@ class Stopwatch
public function __construct()
{
$this->sections = $this->activeSections = array('__root__' => new Section('__root__'));
$this->reset();
}
/**
@ -156,4 +156,12 @@ class Stopwatch
{
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__'));
}
}

View File

@ -153,4 +153,16 @@ class StopwatchTest extends TestCase
$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);
}
}