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:
commit
b23ddfefce
@ -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__'));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user