[VarDumper] Fix CliDumper coloration

When using AbstractDumper::DUMP_LIGHT_ARRAY
This commit is contained in:
Laurent VOULLEMIER 2020-03-24 21:48:50 +01:00
parent b30f4c1537
commit 7af3469771
2 changed files with 54 additions and 1 deletions

View File

@ -268,7 +268,8 @@ class CliDumper extends AbstractDumper
} elseif (Cursor::HASH_RESOURCE === $type) {
$prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' ');
} else {
$prefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? $this->style('note', 'array:'.$class).' [' : '[';
$unstyledPrefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? 'array:'.$class : '';
$prefix = $this->style('note', $unstyledPrefix).($unstyledPrefix ? ' [' : '[');
}
if ($cursor->softRefCount || 0 < $cursor->softRefHandle) {

View File

@ -13,6 +13,7 @@ namespace Symfony\Component\VarDumper\Tests\Dumper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\AbstractDumper;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
use Twig\Environment;
@ -572,6 +573,57 @@ EOTXT
);
}
public function provideDumpArrayWithColor()
{
yield [
['foo' => 'bar'],
0,
<<<EOTXT
\e[0;38;5;208m\e[38;5;38marray:1\e[0;38;5;208m [\e[m
\e[0;38;5;208m"\e[38;5;113mfoo\e[0;38;5;208m" => "\e[1;38;5;113mbar\e[0;38;5;208m"\e[m
\e[0;38;5;208m]\e[m
EOTXT
];
yield [[], AbstractDumper::DUMP_LIGHT_ARRAY, "\e[0;38;5;208m\e[38;5;38m\e[0;38;5;208m[]\e[m\n"];
yield [
['foo' => 'bar'],
AbstractDumper::DUMP_LIGHT_ARRAY,
<<<EOTXT
\e[0;38;5;208m\e[38;5;38m\e[0;38;5;208m[\e[m
\e[0;38;5;208m"\e[38;5;113mfoo\e[0;38;5;208m" => "\e[1;38;5;113mbar\e[0;38;5;208m"\e[m
\e[0;38;5;208m]\e[m
EOTXT
];
yield [[], 0, "\e[0;38;5;208m\e[38;5;38m\e[0;38;5;208m[]\e[m\n"];
}
/**
* @dataProvider provideDumpArrayWithColor
*/
public function testDumpArrayWithColor($value, $flags, $expectedOut)
{
if ('\\' === \DIRECTORY_SEPARATOR) {
$this->markTestSkipped('Windows console does not support coloration');
}
$out = '';
$dumper = new CliDumper(function ($line, $depth) use (&$out) {
if ($depth >= 0) {
$out .= str_repeat(' ', $depth).$line."\n";
}
}, null, $flags);
$dumper->setColors(true);
$cloner = new VarCloner();
$dumper->dump($cloner->cloneVar($value));
$this->assertSame($expectedOut, $out);
}
private function getSpecialVars()
{
foreach (array_keys($GLOBALS) as $var) {