Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ba988ac

Browse filesBrowse files
feature #32463 [VarDumper] Allow to configure VarDumperTestTrait casters & flags (ogizanagi)
This PR was merged into the 4.4 branch. Discussion ---------- [VarDumper] Allow to configure VarDumperTestTrait casters & flags | Q | A | ------------- | --- | Branch? | 4.4 <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | N/A <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | TODO: showcase using `setUpVarDumper` in `setUp` or in specific test cases accordingly to the use-case. `tearDownVarDumper` is automatically called after each test case. The VarDumper component is a great tool in tests to assert objects states. The ability to register custom casters on need is a nice way to control only the fields you're expecting, or a way to write concise test cases. Hence this feature allowing to configure casters specifically per test class/case. Commits ------- 613dbb2 [VarDumper] Allow to configure VarDumperTestTrait casters & flags
2 parents f900c97 + 613dbb2 commit ba988ac
Copy full SHA for ba988ac

File tree

Expand file treeCollapse file tree

2 files changed

+61
-3
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+61
-3
lines changed

‎src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php
+29-3Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@
1919
*/
2020
trait VarDumperTestTrait
2121
{
22+
/**
23+
* @internal
24+
*/
25+
private $varDumperConfig = [
26+
'casters' => [],
27+
'flags' => null,
28+
];
29+
30+
protected function setUpVarDumper(array $casters, int $flags = null): void
31+
{
32+
$this->varDumperConfig['casters'] = $casters;
33+
$this->varDumperConfig['flags'] = $flags;
34+
}
35+
36+
/**
37+
* @after
38+
*/
39+
protected function tearDownVarDumper(): void
40+
{
41+
$this->varDumperConfig['casters'] = [];
42+
$this->varDumperConfig['flags'] = null;
43+
}
44+
2245
public function assertDumpEquals($expected, $data, $filter = 0, $message = '')
2346
{
2447
$this->assertSame($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
@@ -31,11 +54,14 @@ public function assertDumpMatchesFormat($expected, $data, $filter = 0, $message
3154

3255
protected function getDump($data, $key = null, $filter = 0)
3356
{
34-
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
35-
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
36-
$flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
57+
if (null === $flags = $this->varDumperConfig['flags']) {
58+
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
59+
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
60+
$flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
61+
}
3762

3863
$cloner = new VarCloner();
64+
$cloner->addCasters($this->varDumperConfig['casters']);
3965
$cloner->setMaxItems(-1);
4066
$dumper = new CliDumper(null, null, $flags);
4167
$dumper->setColors(false);

‎src/Symfony/Component/VarDumper/Tests/Test/VarDumperTestTraitTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/VarDumper/Tests/Test/VarDumperTestTraitTest.php
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Component\VarDumper\Tests\Test;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\VarDumper\Cloner\Stub;
16+
use Symfony\Component\VarDumper\Dumper\CliDumper;
1517
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
1618

1719
class VarDumperTestTraitTest extends TestCase
@@ -43,4 +45,34 @@ public function testAllowsNonScalarExpectation()
4345
{
4446
$this->assertDumpEquals(new \ArrayObject(['bim' => 'bam']), new \ArrayObject(['bim' => 'bam']));
4547
}
48+
49+
public function testItCanBeConfigured()
50+
{
51+
$this->setUpVarDumper($casters = [
52+
\DateTimeInterface::class => static function (\DateTimeInterface $date, array $a, Stub $stub): array {
53+
$stub->class = 'DateTime';
54+
55+
return ['date' => $date->format('d/m/Y')];
56+
},
57+
], CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
58+
59+
$this->assertSame(CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR, $this->varDumperConfig['flags']);
60+
$this->assertSame($casters, $this->varDumperConfig['casters']);
61+
62+
$this->assertDumpEquals(<<<DUMP
63+
[
64+
1,
65+
2,
66+
DateTime {
67+
+date: "09/07/2019"
68+
}
69+
]
70+
DUMP
71+
, [1, 2, new \DateTime('2019-07-09T0:00:00+00:00')]);
72+
73+
$this->tearDownVarDumper();
74+
75+
$this->assertNull($this->varDumperConfig['flags']);
76+
$this->assertSame([], $this->varDumperConfig['casters']);
77+
}
4678
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.