Skip to content

Commit 47a98b9

Browse files
fix: Json output without errors (#475)
* fix json empty errors output * Add test for JSON output with no errors in CheckCommand This test ensures that the JSON output format properly handles and displays an empty result when no errors are present. It validates the success of the command and confirms the output is in valid JSON format with the expected structure.
1 parent e826a47 commit 47a98b9

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/CLI/Command/Check.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
200200
return self::ERROR_CODE;
201201
}
202202

203-
$this->printNoViolationsDetectedMessage($output, $onlyErrors);
203+
$this->printNoViolationsDetectedMessage($output, $onlyErrors, $format);
204204

205205
if (!$onlyErrors) {
206206
$this->printExecutionTime($output, $startTime);
@@ -266,6 +266,7 @@ private function printViolations(Violations $violations, OutputInterface $output
266266
if (!$onlyErrors) {
267267
$output->writeln('<error>ERRORS!</error>');
268268
}
269+
269270
$output->writeln(sprintf('%s', $violations->toString($format)));
270271
if (!$onlyErrors) {
271272
$output->writeln(sprintf('<error>%s VIOLATIONS DETECTED!</error>', \count($violations)));
@@ -280,10 +281,12 @@ private function printParsedErrors(ParsingErrors $parsingErrors, OutputInterface
280281
$output->writeln(sprintf('%s', $parsingErrors->toString()));
281282
}
282283

283-
private function printNoViolationsDetectedMessage(OutputInterface $output, bool $onlyErrors = false): void
284+
private function printNoViolationsDetectedMessage(OutputInterface $output, bool $onlyErrors = false, string $format = Printer::FORMAT_TEXT): void
284285
{
285286
if (!$onlyErrors) {
286287
$output->writeln('<info>NO VIOLATIONS DETECTED!</info>');
288+
} elseif (Printer::FORMAT_JSON === $format) {
289+
$output->writeln('<info>[]</info>');
287290
}
288291
}
289292
}

tests/E2E/Cli/CheckCommandTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ public function test_json_format_output(): void
179179
$this->assertJson($display);
180180
}
181181

182+
public function test_json_format_output_no_errors(): void
183+
{
184+
$configFilePath = __DIR__.'/../_fixtures/configMvcWithoutErrors.php';
185+
186+
$cmdTester = $this->runCheck($configFilePath, null, null, false, false, false, 'json');
187+
188+
$this->assertCheckHasSuccess($cmdTester);
189+
190+
$display = $cmdTester->getDisplay();
191+
192+
$this->assertJson($display);
193+
194+
$json = json_decode($display, true);
195+
$this->assertCount(0, $json);
196+
}
197+
182198
protected function runCheck(
183199
$configFilePath = null,
184200
?bool $stopOnFailure = null,

0 commit comments

Comments
 (0)