Skip to content

Commit d130be4

Browse files
committed
[VarDumper] Documented setUpVarDumper() and tearDownVarDumper()
1 parent 8f6a938 commit d130be4

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

components/var_dumper.rst

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,21 @@ This will provide you with two new assertions:
248248
is like the previous method but accepts placeholders in the expected dump,
249249
based on the ``assertStringMatchesFormat()`` method provided by PHPUnit.
250250

251+
The ``VarDumperTestTrait`` also includes these other methods:
252+
253+
:method:`Symfony\\Component\\VarDumper\\Test\\VarDumperTestTrait::setUpVarDumper`
254+
is used to configure the available casters and their options, which is a way
255+
to only control the fields you're expecting and allows writing concise tests.
256+
257+
:method:`Symfony\\Component\\VarDumper\\Test\\VarDumperTestTrait::tearDownVarDumper`
258+
is called automatically after each case to reset the custom configuration
259+
made in ``setUpVarDumper()``.
260+
261+
.. versionadded:: 4.4
262+
263+
The ``setUpVarDumper()`` and ``tearDownVarDumper()`` methods were introduced
264+
in Symfony 4.4.
265+
251266
Example::
252267

253268
use PHPUnit\Framework\TestCase;
@@ -257,14 +272,33 @@ Example::
257272
{
258273
use VarDumperTestTrait;
259274

275+
protected function setUp()
276+
{
277+
$casters = [
278+
\DateTimeInterface::class => static function (\DateTimeInterface $date, array $a, Stub $stub): array {
279+
$stub->class = 'DateTime';
280+
return ['date' => $date->format('d/m/Y')];
281+
},
282+
];
283+
284+
$flags = CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR;
285+
286+
// this configures the casters & flags to use for all the tests in this class.
287+
// If you need custom configurations per test rather than for the whole class,
288+
// call this setUpVarDumper() method from those tests instead.
289+
$this->setUpVarDumper($casters, $flags);
290+
}
291+
260292
public function testWithDumpEquals()
261293
{
262294
$testedVar = [123, 'foo'];
263295

296+
// the expected dump contents don't have the default VarDumper structure
297+
// because of the custom casters and flags used in the test
264298
$expectedDump = <<<EOTXT
265-
array:2 [
266-
0 => 123
267-
1 => "foo"
299+
[
300+
123,
301+
"foo",
268302
]
269303
EOTXT;
270304

0 commit comments

Comments
 (0)