- Notifications
You must be signed in to change notification settings - Fork 542
add JUnit error formatter #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ondrejmirtes merged 3 commits into phpstan:master from mavimo:feature/add-junit-error-formatter Dec 24, 2019
Merged
Changes from all commits
Commits
Show all changes
3 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php declare(strict_types = 1); | ||
| ||
namespace PHPStan\Command\ErrorFormatter; | ||
| ||
use PHPStan\Command\AnalysisResult; | ||
use PHPStan\Command\Output; | ||
use PHPStan\File\RelativePathHelper; | ||
use function sprintf; | ||
| ||
class JunitErrorFormatter implements ErrorFormatter | ||
{ | ||
| ||
/** @var \PHPStan\File\RelativePathHelper */ | ||
private $relativePathHelper; | ||
| ||
public function __construct(RelativePathHelper $relativePathHelper) | ||
{ | ||
$this->relativePathHelper = $relativePathHelper; | ||
} | ||
| ||
public function formatErrors( | ||
AnalysisResult $analysisResult, | ||
Output $output | ||
): int | ||
{ | ||
$result = '<?xml version="1.0" encoding="UTF-8"?>'; | ||
$result .= sprintf( | ||
'<testsuite failures="%d" name="phpstan" tests="%d" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd">', | ||
$analysisResult->getTotalErrorsCount(), | ||
$analysisResult->getTotalErrorsCount() | ||
); | ||
| ||
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) { | ||
$fileName = $this->relativePathHelper->getRelativePath($fileSpecificError->getFile()); | ||
$result .= $this->createTestCase( | ||
sprintf('%s:%s', $fileName, (string) $fileSpecificError->getLine()), | ||
$this->escape($fileSpecificError->getMessage()) | ||
); | ||
} | ||
| ||
foreach ($analysisResult->getNotFileSpecificErrors() as $notFileSpecificError) { | ||
$result .= $this->createTestCase('General error', $this->escape($notFileSpecificError)); | ||
} | ||
| ||
if (!$analysisResult->hasErrors()) { | ||
$result .= $this->createTestCase('phpstan'); | ||
} | ||
| ||
$result .= '</testsuite>'; | ||
| ||
$output->writeRaw($result); | ||
| ||
return intval($analysisResult->hasErrors()); | ||
} | ||
| ||
/** | ||
* Format a single test case | ||
* | ||
* @param string $reference | ||
* @param string|null $message | ||
* | ||
* @return string | ||
*/ | ||
private function createTestCase(string $reference, ?string $message = null): string | ||
{ | ||
$result = sprintf('<testcase name="%s">', $this->escape($reference)); | ||
| ||
if ($message !== null) { | ||
$result .= sprintf('<failure message="%s" />', $this->escape($message)); | ||
} | ||
| ||
$result .= '</testcase>'; | ||
| ||
return $result; | ||
} | ||
| ||
/** | ||
* Escapes values for using in XML | ||
* | ||
* @param string $string | ||
* @return string | ||
*/ | ||
protected function escape(string $string): string | ||
{ | ||
return htmlspecialchars($string, ENT_XML1 | ENT_COMPAT, 'UTF-8'); | ||
} | ||
| ||
} |
168 changes: 168 additions & 0 deletions 168 tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
<?php declare(strict_types = 1); | ||
| ||
namespace PHPStan\Command\ErrorFormatter; | ||
| ||
use DOMDocument; | ||
use Generator; | ||
use PHPStan\File\SimpleRelativePathHelper; | ||
use PHPStan\Testing\ErrorFormatterTestCase; | ||
| ||
class JunitErrorFormatterTest extends ErrorFormatterTestCase | ||
{ | ||
| ||
/** @var \PHPStan\Command\ErrorFormatter\JunitErrorFormatter */ | ||
private $formatter; | ||
| ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
| ||
$this->formatter = new JunitErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH)); | ||
} | ||
| ||
/** | ||
* @return \Generator<array<int, string|int>> | ||
*/ | ||
public function dataFormatterOutputProvider(): Generator | ||
{ | ||
yield 'No errors' => [ | ||
0, | ||
0, | ||
0, | ||
'<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuite failures="0" name="phpstan" tests="0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd"> | ||
<testcase name="phpstan"/> | ||
</testsuite> | ||
', | ||
]; | ||
| ||
yield 'One file error' => [ | ||
1, | ||
1, | ||
0, | ||
'<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuite failures="1" name="phpstan" tests="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd"> | ||
<testcase name="folder with unicode 😃/file name with "spaces" and unicode 😃.php:4"> | ||
<failure message="Foo" /> | ||
</testcase> | ||
</testsuite> | ||
', | ||
]; | ||
| ||
yield 'One generic error' => [ | ||
1, | ||
0, | ||
1, | ||
'<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuite failures="1" name="phpstan" tests="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd"> | ||
<testcase name="General error"> | ||
<failure message="first generic error" /> | ||
</testcase> | ||
</testsuite> | ||
', | ||
]; | ||
| ||
yield 'Multiple file errors' => [ | ||
1, | ||
4, | ||
0, | ||
'<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuite failures="4" name="phpstan" tests="4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd"> | ||
<testcase name="folder with unicode 😃/file name with "spaces" and unicode 😃.php:2"> | ||
<failure message="Bar" /> | ||
</testcase> | ||
<testcase name="folder with unicode 😃/file name with "spaces" and unicode 😃.php:4"> | ||
<failure message="Foo" /> | ||
</testcase> | ||
<testcase name="foo.php:1"> | ||
<failure message="Foo"/> | ||
</testcase> | ||
<testcase name="foo.php:5"> | ||
<failure message="Bar"/> | ||
</testcase> | ||
</testsuite> | ||
', | ||
]; | ||
| ||
yield 'Multiple generic errors' => [ | ||
1, | ||
0, | ||
2, | ||
'<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuite failures="2" name="phpstan" tests="2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd"> | ||
<testcase name="General error"> | ||
<failure message="first generic error" /> | ||
</testcase> | ||
<testcase name="General error"> | ||
<failure message="second generic error"/> | ||
</testcase> | ||
</testsuite> | ||
', | ||
]; | ||
| ||
yield 'Multiple file, multiple generic errors' => [ | ||
1, | ||
4, | ||
2, | ||
'<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuite failures="6" name="phpstan" tests="6" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd"> | ||
<testcase name="folder with unicode 😃/file name with "spaces" and unicode 😃.php:2"> | ||
<failure message="Bar" /> | ||
</testcase> | ||
<testcase name="folder with unicode 😃/file name with "spaces" and unicode 😃.php:4"> | ||
<failure message="Foo" /> | ||
</testcase> | ||
<testcase name="foo.php:1"> | ||
<failure message="Foo"/> | ||
</testcase> | ||
<testcase name="foo.php:5"> | ||
<failure message="Bar"/> | ||
</testcase> | ||
<testcase name="General error"> | ||
<failure message="first generic error" /> | ||
</testcase> | ||
<testcase name="General error"> | ||
<failure message="second generic error"/> | ||
</testcase> | ||
</testsuite> | ||
', | ||
]; | ||
} | ||
| ||
/** | ||
* Test generated use cases for JUnit output format. | ||
* | ||
* @dataProvider dataFormatterOutputProvider | ||
*/ | ||
public function testFormatErrors( | ||
int $exitCode, | ||
int $numFileErrors, | ||
int $numGeneralErrors, | ||
string $expected | ||
): void | ||
{ | ||
$this->assertSame( | ||
$exitCode, | ||
$this->formatter->formatErrors( | ||
$this->getAnalysisResult($numFileErrors, $numGeneralErrors), | ||
$this->getOutput() | ||
), | ||
'Response code do not match' | ||
); | ||
| ||
$xml = new DOMDocument(); | ||
$xml->loadXML($this->getOutputContent()); | ||
| ||
$this->assertTrue( | ||
$xml->schemaValidate('https://raw.githubusercontent.com/junit-team/junit5/r5.5.1/platform-tests/src/test/resources/jenkins-junit.xsd'), | ||
'Schema do not validate' | ||
); | ||
| ||
$this->assertXmlStringEqualsXmlString( | ||
$expected, | ||
$this->getOutputContent(), | ||
'XML do not match' | ||
); | ||
} | ||
| ||
} |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK this should not be added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you need it for in dev?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes It's useful to do XML validation, see https://github.com/phpstan/phpstan-src/pull/65/files#diff-ba74c4a649ddce093d63acd494b761d6R157 but we can remove this assertion if you think is not useful