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
-------
613dbb267d
[VarDumper] Allow to configure VarDumperTestTrait casters & flags
This commit is contained in:
commit
ba988acaec
@ -19,6 +19,29 @@ use Symfony\Component\VarDumper\Dumper\CliDumper;
|
||||
*/
|
||||
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);
|
||||
@ -31,11 +54,14 @@ trait VarDumperTestTrait
|
||||
|
||||
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);
|
||||
|
@ -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
|
||||
@ -43,4 +45,34 @@ EODUMP;
|
||||
{
|
||||
$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']);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user