From ffe1f6aa9b13e67360d5562823521ec9440e27e3 Mon Sep 17 00:00:00 2001 From: Laurent Laville Date: Fri, 11 Oct 2024 07:03:10 +0000 Subject: [PATCH] fix issue #214 --- phpunit.xml | 3 ++ src/Output/JunitOutput.php | 2 +- tests/Finder/FinderTest.php | 1 + tests/Output/OutputTest.php | 91 +++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/Output/OutputTest.php diff --git a/phpunit.xml b/phpunit.xml index bf14340b..50280ec0 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -24,6 +24,9 @@ tests/Finder + + tests/Output + diff --git a/src/Output/JunitOutput.php b/src/Output/JunitOutput.php index 87a48f06..8516bfe7 100644 --- a/src/Output/JunitOutput.php +++ b/src/Output/JunitOutput.php @@ -66,6 +66,6 @@ public function format(LinterOutput $results): void $error->setAttribute('message', $value['error']); } - $this->write($document->saveXML()); + $this->write($document->saveXML(), false, self::OUTPUT_RAW); } } diff --git a/tests/Finder/FinderTest.php b/tests/Finder/FinderTest.php index c7b8dfd4..a27e3aa2 100644 --- a/tests/Finder/FinderTest.php +++ b/tests/Finder/FinderTest.php @@ -116,6 +116,7 @@ public function testSearchPhpFilesWithCondition(): void 'EndToEnd/LintCommandTest.php', 'EndToEnd/Reserved@Keywords.php', 'Finder/FinderTest.php', + 'Output/OutputTest.php', 'TestCase.php', ], $this->getRelativePathFiles($finder->getFiles()->getIterator(), $basePath) diff --git a/tests/Output/OutputTest.php b/tests/Output/OutputTest.php new file mode 100644 index 00000000..f97b2197 --- /dev/null +++ b/tests/Output/OutputTest.php @@ -0,0 +1,91 @@ + [$basePath], + '--no-configuration' => true, + '--no-cache' => true, + '--' . OptionDefinition::WARNING => true, + '--' . OptionDefinition::EXTENSIONS => ['php'] + ]; + $definition = (new LintCommand($dispatcher))->getDefinition(); + $input = new ArrayInput($arguments, $definition); + + $configResolver = new ConsoleOptionsResolver($input); + + $finder = new Finder($configResolver); + + $linter = new Linter($configResolver, $dispatcher); + + $startTime = microtime(true); + $defaults = ['application_version' => ['short' => '9.x-dev', 'long' => '9.x-dev']]; + + $this->linterOutput = $linter->lintFiles($finder->getFiles(), $startTime); + $this->linterOutput->setContext($configResolver, $startTime, 2, $defaults); + } + + /** + * @covers \Overtrue\PHPLint\Output\JunitOutput::format + */ + public function testJunitOutput(): void + { + $stream = fopen('php://memory', 'w+'); + $output = new JunitOutput($stream, OutputInterface::VERBOSITY_VERBOSE, false); + $output->format($this->linterOutput); + + rewind($stream); + $xml = stream_get_contents($stream); + + $this->assertStringContainsString('syntax_error.php', $xml); + $this->assertStringContainsString('syntax_warning.php', $xml); + } +}