Skip to content

Commit 20d5691

Browse files
committed
Reset profiler.
1 parent 0fb75d8 commit 20d5691

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

DataCollector/ValidatorDataCollector.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Validator\Validator\TraceableValidator;
2020
use Symfony\Component\VarDumper\Caster\Caster;
2121
use Symfony\Component\VarDumper\Caster\ClassStub;
22+
use Symfony\Component\VarDumper\Cloner\Data;
2223
use Symfony\Component\VarDumper\Cloner\Stub;
2324

2425
/**
@@ -31,10 +32,7 @@ class ValidatorDataCollector extends DataCollector implements LateDataCollectorI
3132
public function __construct(TraceableValidator $validator)
3233
{
3334
$this->validator = $validator;
34-
$this->data = array(
35-
'calls' => array(),
36-
'violations_count' => 0,
37-
);
35+
$this->reset();
3836
}
3937

4038
/**
@@ -45,23 +43,38 @@ public function collect(Request $request, Response $response, \Exception $except
4543
// Everything is collected once, on kernel terminate.
4644
}
4745

46+
public function reset()
47+
{
48+
$this->validator->reset();
49+
$this->data = array(
50+
'calls' => $this->cloneVar(array()),
51+
'violations_count' => 0,
52+
);
53+
}
54+
4855
/**
4956
* {@inheritdoc}
5057
*/
5158
public function lateCollect()
5259
{
5360
$collected = $this->validator->getCollectedData();
5461
$this->data['calls'] = $this->cloneVar($collected);
55-
$this->data['violations_count'] += array_reduce($collected, function ($previous, $item) {
56-
return $previous += count($item['violations']);
62+
$this->data['violations_count'] = array_reduce($collected, function ($previous, $item) {
63+
return $previous + count($item['violations']);
5764
}, 0);
5865
}
5966

67+
/**
68+
* @return Data
69+
*/
6070
public function getCalls()
6171
{
6272
return $this->data['calls'];
6373
}
6474

75+
/**
76+
* @return int
77+
*/
6578
public function getViolationsCount()
6679
{
6780
return $this->data['violations_count'];

Tests/DataCollector/ValidatorDataCollectorTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,33 @@ public function testCollectsValidatorCalls()
5050
$this->assertCount(2, $call['violations']);
5151
}
5252

53+
public function testReset()
54+
{
55+
$originalValidator = $this->createMock(ValidatorInterface::class);
56+
$validator = new TraceableValidator($originalValidator);
57+
58+
$collector = new ValidatorDataCollector($validator);
59+
60+
$violations = new ConstraintViolationList(array(
61+
$this->createMock(ConstraintViolation::class),
62+
$this->createMock(ConstraintViolation::class),
63+
));
64+
$originalValidator->method('validate')->willReturn($violations);
65+
66+
$validator->validate(new \stdClass());
67+
68+
$collector->lateCollect();
69+
$collector->reset();
70+
71+
$this->assertCount(0, $collector->getCalls());
72+
$this->assertSame(0, $collector->getViolationsCount());
73+
74+
$collector->lateCollect();
75+
76+
$this->assertCount(0, $collector->getCalls());
77+
$this->assertSame(0, $collector->getViolationsCount());
78+
}
79+
5380
protected function createMock($classname)
5481
{
5582
return $this->getMockBuilder($classname)->disableOriginalConstructor()->getMock();

Validator/TraceableValidator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Validator\Validator;
1313

14-
use Symfony\Component\Validator\ConstraintViolationList;
1514
use Symfony\Component\Validator\Context\ExecutionContextInterface;
1615

1716
/**
@@ -30,13 +29,18 @@ public function __construct(ValidatorInterface $validator)
3029
}
3130

3231
/**
33-
* @return ConstraintViolationList[]
32+
* @return array
3433
*/
3534
public function getCollectedData()
3635
{
3736
return $this->collectedData;
3837
}
3938

39+
public function reset()
40+
{
41+
$this->collectedData = array();
42+
}
43+
4044
/**
4145
* {@inheritdoc}
4246
*/

0 commit comments

Comments
 (0)