Skip to content

Commit

Permalink
[VarDumper] Allow to configure VarDumperTestTrait casters & flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ogizanagi committed Jul 9, 2019
1 parent 8935ea5 commit 0e53f95
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php
Expand Up @@ -19,6 +19,23 @@
*/
trait VarDumperTestTrait
{
private static $casters = [];
private static $varCloner;
private static $flags;

protected static function setUpVarDumper(array $casters, int $flags = null): void
{
self::$varCloner = null;
self::$casters = $casters;
self::$flags = $flags;
}

protected static function tearDownVarDumper(): void
{
self::$casters = [];
self::$flags = self::$varCloner = null;
}

public function assertDumpEquals($expected, $data, $filter = 0, $message = '')
{
$this->assertSame($this->prepareExpectation($expected, $filter), $this->getDump($data, null, $filter), $message);
Expand All @@ -31,15 +48,22 @@ 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;
$flags = self::$flags;
if (null === $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;
}

if (!self::$varCloner) {
self::$varCloner = new VarCloner();
self::$varCloner->addCasters(self::$casters);
}

$cloner = new VarCloner();
$cloner->setMaxItems(-1);
self::$varCloner->setMaxItems(-1);
$dumper = new CliDumper(null, null, $flags);
$dumper->setColors(false);
$data = $cloner->cloneVar($data, $filter)->withRefHandles(false);
$data = self::$varCloner->cloneVar($data, $filter)->withRefHandles(false);
if (null !== $key && null === $data = $data->seek($key)) {
return;
}
Expand Down
Expand Up @@ -12,6 +12,9 @@
namespace Symfony\Component\VarDumper\Tests\Test;

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

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

public function testItCanBeConfigured()
{
self::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, self::$flags);
$this->assertSame($casters, self::$casters);

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

$this->assertInstanceOf(VarCloner::class, self::$varCloner);

self::tearDownVarDumper();

$this->assertNull(self::$varCloner);
$this->assertNull(self::$flags);
$this->assertSame([], self::$casters);
}
}

0 comments on commit 0e53f95

Please sign in to comment.