From 16ac9816258a5bd3e688714006f098f3efafb683 Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Mon, 4 Apr 2016 13:47:13 -0700 Subject: [PATCH] Add json formatter. --- src/FormatterManager.php | 1 + src/Formatters/JsonFormatter.php | 16 +++++++++ tests/testFormatters.php | 62 ++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/Formatters/JsonFormatter.php diff --git a/src/FormatterManager.php b/src/FormatterManager.php index d0eba3c..8fdd1da 100644 --- a/src/FormatterManager.php +++ b/src/FormatterManager.php @@ -12,6 +12,7 @@ public function __construct() { $this->formatters = [ 'yaml' => \Consolidation\OutputFormatters\Formatters\YamlFormatter::class, + 'json' => \Consolidation\OutputFormatters\Formatters\JsonFormatter::class, 'table' => \Consolidation\OutputFormatters\Formatters\TableFormatter::class, ]; } diff --git a/src/Formatters/JsonFormatter.php b/src/Formatters/JsonFormatter.php new file mode 100644 index 0000000..d583ec9 --- /dev/null +++ b/src/Formatters/JsonFormatter.php @@ -0,0 +1,16 @@ +writeln(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + } +} diff --git a/tests/testFormatters.php b/tests/testFormatters.php index 219b4c6..f7b6c95 100644 --- a/tests/testFormatters.php +++ b/tests/testFormatters.php @@ -75,6 +75,68 @@ function testNestedYaml() $this->assertFormattedOutputMatches($expected, 'yaml', $data); } + function testSimpleJson() + { + $data = [ + 'one' => 'a', + 'two' => 'b', + 'three' => 'c', + ]; + + $expected = <<assertFormattedOutputMatches($expected, 'json', $data); + } + + function testNestedJson() + { + $data = [ + 'one' => [ + 'i' => ['a', 'b', 'c'], + ], + 'two' => [ + 'ii' => ['q', 'r', 's'], + ], + 'three' => [ + 'iii' => ['t', 'u', 'v'], + ], + ]; + + $expected = <<assertFormattedOutputMatches($expected, 'json', $data); + } + function testSimpleTable() { $data = [