Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions components/var_dumper.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,21 @@ This will provide you with two new assertions:
is like the previous method but accepts placeholders in the expected dump,
based on the ``assertStringMatchesFormat()`` method provided by PHPUnit.

The ``VarDumperTestTrait`` also includes these other methods:

:method:`Symfony\\Component\\VarDumper\\Test\\VarDumperTestTrait::setUpVarDumper`
is used to configure the available casters and their options, which is a way
to only control the fields you're expecting and allows writing concise tests.

:method:`Symfony\\Component\\VarDumper\\Test\\VarDumperTestTrait::tearDownVarDumper`
is called automatically after each case to reset the custom configuration
made in ``setUpVarDumper()``.

.. versionadded:: 4.4

The ``setUpVarDumper()`` and ``tearDownVarDumper()`` methods were introduced
in Symfony 4.4.

Example::

use PHPUnit\Framework\TestCase;
Expand All @@ -257,14 +272,33 @@ Example::
{
use VarDumperTestTrait;

protected function setUp()
{
$casters = [
\DateTimeInterface::class => static function (\DateTimeInterface $date, array $a, Stub $stub): array {
$stub->class = 'DateTime';
return ['date' => $date->format('d/m/Y')];
},
];

$flags = CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR;

// this configures the casters & flags to use for all the tests in this class.
// If you need custom configurations per test rather than for the whole class,
// call this setUpVarDumper() method from those tests instead.
$this->setUpVarDumper($casters, $flags);
}

public function testWithDumpEquals()
{
$testedVar = [123, 'foo'];

// the expected dump contents don't have the default VarDumper structure
// because of the custom casters and flags used in the test
$expectedDump = <<<EOTXT
array:2 [
0 => 123
1 => "foo"
[
123,
"foo",
]
EOTXT;

Expand Down