@@ -248,6 +248,21 @@ This will provide you with two new assertions:
248
248
is like the previous method but accepts placeholders in the expected dump,
249
249
based on the ``assertStringMatchesFormat() `` method provided by PHPUnit.
250
250
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
+
251
266
Example::
252
267
253
268
use PHPUnit\Framework\TestCase;
@@ -257,14 +272,33 @@ Example::
257
272
{
258
273
use VarDumperTestTrait;
259
274
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
+
260
292
public function testWithDumpEquals()
261
293
{
262
294
$testedVar = [123, 'foo'];
263
295
296
+ // the expected dump contents don't have the default VarDumper structure
297
+ // because of the custom casters and flags used in the test
264
298
$expectedDump = <<<EOTXT
265
- array:2 [
266
- 0 => 123
267
- 1 => "foo"
299
+ [
300
+ 123,
301
+ "foo",
268
302
]
269
303
EOTXT;
270
304
0 commit comments