From fc3fe7f42ecdb4d0ba161a4216124848014a12ac Mon Sep 17 00:00:00 2001 From: Amrouche Hamza Date: Sat, 28 Oct 2017 21:55:36 +0200 Subject: [PATCH] [VarDumper] fix trailling comma when dumping an exception --- .../Component/VarDumper/Dumper/CliDumper.php | 11 ++++--- .../VarDumper/Tests/Dumper/CliDumperTest.php | 30 +++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php index 69e2c88e5ed25..cda6dbb31a974 100644 --- a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php @@ -12,6 +12,7 @@ namespace Symfony\Component\VarDumper\Dumper; use Symfony\Component\VarDumper\Cloner\Cursor; +use Symfony\Component\VarDumper\Cloner\Stub; /** * CliDumper dumps variables for command line output. @@ -494,10 +495,12 @@ protected function dumpLine($depth, $endOfValue = false) protected function endValue(Cursor $cursor) { - if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) { - $this->line .= ','; - } elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) { - $this->line .= ','; + if (Stub::ARRAY_INDEXED === $cursor->hashType || Stub::ARRAY_ASSOC === $cursor->hashType) { + if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) { + $this->line .= ','; + } elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) { + $this->line .= ','; + } } $this->dumpLine($cursor->depth, true); diff --git a/src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php b/src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php index f9a208c6303bf..d736bd552a06b 100644 --- a/src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php @@ -130,6 +130,36 @@ public function testDumpWithCommaFlag($expected, $flags) $this->assertSame($expected, $dump); } + public function testDumpWithCommaFlagsAndExceptionCodeExcerpt() + { + $dumper = new CliDumper(null, null, CliDumper::DUMP_TRAILING_COMMA); + $dumper->setColors(false); + $cloner = new VarCloner(); + + $ex = new \RuntimeException('foo'); + + $dump = $dumper->dump($cloner->cloneVar($ex)->withRefHandles(false), true); + + $this->assertStringMatchesFormat(<<<'EOTXT' +RuntimeException { + #message: "foo" + #code: 0 + #file: "%ACliDumperTest.php" + #line: %d + trace: { + %ACliDumperTest.php:%d: { + : + : $ex = new \RuntimeException('foo'); + : + } + %A + } +} + +EOTXT + , $dump); + } + public function provideDumpWithCommaFlagTests() { $expected = <<<'EOTXT'