Skip to content

Commit

Permalink
feature #32463 [VarDumper] Allow to configure VarDumperTestTrait cast…
Browse files Browse the repository at this point in the history
…ers & 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
  • Loading branch information
nicolas-grekas committed Jul 17, 2019
2 parents f900c97 + 613dbb2 commit ba988ac
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php
Expand Up @@ -19,6 +19,29 @@
*/
trait VarDumperTestTrait
{
/**
* @internal
*/
private $varDumperConfig = [
'casters' => [],
'flags' => null,
];

protected function setUpVarDumper(array $casters, int $flags = null): void
{
$this->varDumperConfig['casters'] = $casters;
$this->varDumperConfig['flags'] = $flags;
}

/**
* @after
*/
protected function tearDownVarDumper(): void
{
$this->varDumperConfig['casters'] = [];
$this->varDumperConfig['flags'] = null;
}

public function assertDumpEquals($expected, $data, $filter = 0, $message = '')
{
$this->assertSame($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
Expand All @@ -31,11 +54,14 @@ public function assertDumpMatchesFormat($expected, $data, $filter = 0, $message

protected function getDump($data, $key = null, $filter = 0)
{
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
$flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
if (null === $flags = $this->varDumperConfig['flags']) {
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
$flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
}

$cloner = new VarCloner();
$cloner->addCasters($this->varDumperConfig['casters']);
$cloner->setMaxItems(-1);
$dumper = new CliDumper(null, null, $flags);
$dumper->setColors(false);
Expand Down
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Component\VarDumper\Tests\Test;

use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Cloner\Stub;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

class VarDumperTestTraitTest extends TestCase
Expand Down Expand Up @@ -43,4 +45,34 @@ public function testAllowsNonScalarExpectation()
{
$this->assertDumpEquals(new \ArrayObject(['bim' => 'bam']), new \ArrayObject(['bim' => 'bam']));
}

public function testItCanBeConfigured()
{
$this->setUpVarDumper($casters = [
\DateTimeInterface::class => static function (\DateTimeInterface $date, array $a, Stub $stub): array {
$stub->class = 'DateTime';

return ['date' => $date->format('d/m/Y')];
},
], CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);

$this->assertSame(CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR, $this->varDumperConfig['flags']);
$this->assertSame($casters, $this->varDumperConfig['casters']);

$this->assertDumpEquals(<<<DUMP
[
1,
2,
DateTime {
+date: "09/07/2019"
}
]
DUMP
, [1, 2, new \DateTime('2019-07-09T0:00:00+00:00')]);

$this->tearDownVarDumper();

$this->assertNull($this->varDumperConfig['flags']);
$this->assertSame([], $this->varDumperConfig['casters']);
}
}

0 comments on commit ba988ac

Please sign in to comment.