Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"phpstan/phpstan": "self.version"
},
"require-dev": {
"ext-dom": "*",
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

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

"ext-intl": "*",
"ext-mysqli": "*",
"ext-simplexml": "*",
Expand Down
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,11 @@ services:
arguments:
pretty: false

errorFormatter.junit:
class: PHPStan\Command\ErrorFormatter\JunitErrorFormatter
arguments:
relativePathHelper: @simpleRelativePathHelper

errorFormatter.prettyJson:
class: PHPStan\Command\ErrorFormatter\JsonErrorFormatter
arguments:
Expand Down
88 changes: 88 additions & 0 deletions src/Command/ErrorFormatter/JunitErrorFormatter.php
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 tests/PHPStan/Command/ErrorFormatter/JunitErrorFormatterTest.php
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 &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.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 &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:2">
<failure message="Bar" />
</testcase>
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.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 &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.php:2">
<failure message="Bar" />
</testcase>
<testcase name="folder with unicode &#x1F603;/file name with &quot;spaces&quot; and unicode &#x1F603;.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'
);
}

}