Skip to content
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

Set failOnRisky, failOnWarning to true if parameters are not already set for mutants #1630

Merged
merged 1 commit into from Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -24,7 +24,7 @@ PHP_CS_FIXER_CACHE=.php_cs.cache
PHPSTAN=./vendor/bin/phpstan

PSALM=./.tools/psalm
PSALM_URL="https://github.com/vimeo/psalm/releases/download/4.12.0/psalm.phar"
PSALM_URL="https://github.com/vimeo/psalm/releases/download/v4.15.0/psalm.phar"

PHPUNIT=vendor/phpunit/phpunit/phpunit
PARATEST=vendor/bin/paratest --runner=WrapperRunner
Expand Down
Expand Up @@ -101,7 +101,7 @@ public function build(string $version): string

$this->addCoverageNodes($version, $xPath);
$this->addRandomTestsOrderAttributesIfNotSet($version, $xPath);
$this->addFailOnAttributesIfNotSet($version, $xPath);
$this->configManipulator->addFailOnAttributesIfNotSet($version, $xPath);
$this->configManipulator->replaceWithAbsolutePaths($xPath);
$this->configManipulator->setStopOnFailure($xPath);
$this->configManipulator->deactivateColours($xPath);
Expand Down Expand Up @@ -155,16 +155,6 @@ private function addRandomTestsOrderAttributesIfNotSet(string $version, SafeDOMX
}
}

private function addFailOnAttributesIfNotSet(string $version, SafeDOMXPath $xPath): void
{
if (version_compare($version, '5.2', '<')) {
return;
}

$this->addAttributeIfNotSet('failOnRisky', 'true', $xPath);
$this->addAttributeIfNotSet('failOnWarning', 'true', $xPath);
}

private function addAttributeIfNotSet(string $attribute, string $value, SafeDOMXPath $xPath): bool
{
$nodeList = $xPath->query(sprintf('/phpunit/@%s', $attribute));
Expand Down
Expand Up @@ -101,6 +101,7 @@ public function build(

// activate PHPUnit's result cache and order tests by running defects first, then sorted by fastest first
$this->configManipulator->handleResultCacheAndExecutionOrder($version, $xPath, $mutationHash);
$this->configManipulator->addFailOnAttributesIfNotSet($version, $xPath);
$this->configManipulator->setStopOnFailure($xPath);
$this->configManipulator->deactivateColours($xPath);
$this->configManipulator->deactivateStderrRedirection($xPath);
Expand Down
24 changes: 24 additions & 0 deletions src/TestFramework/PhpUnit/Config/XmlConfigurationManipulator.php
Expand Up @@ -200,6 +200,16 @@ public function removeDefaultTestSuite(SafeDOMXPath $xPath): void
);
}

public function addFailOnAttributesIfNotSet(string $version, SafeDOMXPath $xPath): void
{
if (version_compare($version, '5.2', '<')) {
return;
}

$this->addAttributeIfNotSet('failOnRisky', 'true', $xPath);
$this->addAttributeIfNotSet('failOnWarning', 'true', $xPath);
}

/**
* @param string[] $srcDirs
* @param list<string> $filteredSourceFilesToMutate
Expand Down Expand Up @@ -359,4 +369,18 @@ private function getOrCreateNode(SafeDOMXPath $xPath, DOMDocument $dom, string $

return $this->createNode($dom, $nodeName);
}

private function addAttributeIfNotSet(string $attribute, string $value, SafeDOMXPath $xPath): bool
{
$nodeList = $xPath->query(sprintf('/phpunit/@%s', $attribute));

if ($nodeList->length === 0) {
$node = $xPath->query('/phpunit')[0];
$node->setAttribute($attribute, $value);

return true;
}

return false;
}
}
Expand Up @@ -9,7 +9,7 @@
processIsolation="false"
syntaxCheck="false"
executionOrder="reverse"
failOnRisky="true"
failOnRisky="false"
>
<testsuites>
<testsuite name="Application Test Suite">
Expand Down
Expand Up @@ -9,7 +9,7 @@
processIsolation="false"
syntaxCheck="false"
executionOrder="reverse"
failOnWarning="true"
failOnWarning="false"
>
<testsuites>
<testsuite name="Application Test Suite">
Expand Down
Expand Up @@ -407,7 +407,7 @@ public function test_it_does_not_update_fail_on_risky_attributes_if_it_is_alread
$failOnRisky = $this->queryXpath($xml, sprintf('/phpunit/@%s', 'failOnRisky'));

$this->assertInstanceOf(DOMNodeList::class, $failOnRisky);
$this->assertSame('true', $failOnRisky[0]->value);
$this->assertSame('false', $failOnRisky[0]->value);
}

public function test_it_does_not_update_fail_on_warning_attributes_if_it_is_already_set(): void
Expand All @@ -421,7 +421,7 @@ public function test_it_does_not_update_fail_on_warning_attributes_if_it_is_alre
$failOnRisky = $this->queryXpath($xml, sprintf('/phpunit/@%s', 'failOnWarning'));

$this->assertInstanceOf(DOMNodeList::class, $failOnRisky);
$this->assertSame('true', $failOnRisky[0]->value);
$this->assertSame('false', $failOnRisky[0]->value);
}

public function test_it_creates_a_configuration(): void
Expand Down
Expand Up @@ -135,7 +135,7 @@ public function test_it_preserves_white_spaces_and_formatting(): void
~
~ License: https://opensource.org/licenses/BSD-3-Clause New BSD License
-->
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.a1b2c3.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" stopOnFailure="true" stderr="false">
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.a1b2c3.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" failOnRisky="true" failOnWarning="true" stopOnFailure="true" stderr="false">
<testsuites>
<testsuite name="Infection testsuite with filtered tests"/>
</testsuites>
Expand Down Expand Up @@ -171,7 +171,7 @@ public function test_it_can_build_the_config_for_multiple_mutations(): void
~
~ License: https://opensource.org/licenses/BSD-3-Clause New BSD License
-->
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.hash1.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" stopOnFailure="true" stderr="false">
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.hash1.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" failOnRisky="true" failOnWarning="true" stopOnFailure="true" stderr="false">
<testsuites>
<testsuite name="Infection testsuite with filtered tests">
<file>/path/to/FooTest.php</file>
Expand Down Expand Up @@ -241,7 +241,7 @@ public function test_it_can_build_the_config_for_multiple_mutations(): void
~
~ License: https://opensource.org/licenses/BSD-3-Clause New BSD License
-->
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.hash2.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" stopOnFailure="true" stderr="false">
<phpunit backupGlobals="false" backupStaticAttributes="false" bootstrap="$tmp/interceptor.autoload.hash2.infection.php" colors="false" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" syntaxCheck="false" failOnRisky="true" failOnWarning="true" stopOnFailure="true" stderr="false">
<testsuites>
<testsuite name="Infection testsuite with filtered tests">
<file>/path/to/BarTest.php</file>
Expand Down Expand Up @@ -622,6 +622,108 @@ public function test_interceptor_is_included(): void
);
}

/**
* @dataProvider failOnProvider
*/
public function test_it_adds_fail_on_risky_and_warning_for_proper_phpunit_versions(
string $version,
string $attributeName,
int $expectedNodeCount
): void {
$xml = file_get_contents($this->builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
$version
));

$nodes = $this->queryXpath($xml, sprintf('/phpunit/@%s', $attributeName));

$this->assertInstanceOf(DOMNodeList::class, $nodes);

$this->assertSame($expectedNodeCount, $nodes->length);
}

public function test_it_does_not_update_fail_on_risky_attributes_if_it_is_already_set(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_with_fail_on_risky_set.xml';

$builder = $this->createConfigBuilder($phpunitXmlPath);

$xml = file_get_contents($builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'5.2'
));

$failOnRisky = $this->queryXpath($xml, sprintf('/phpunit/@%s', 'failOnRisky'));

$this->assertInstanceOf(DOMNodeList::class, $failOnRisky);
$this->assertSame('false', $failOnRisky[0]->value);
}

public function test_it_does_not_update_fail_on_warning_attributes_if_it_is_already_set(): void
{
$phpunitXmlPath = self::FIXTURES . '/phpunit_with_fail_on_warning_set.xml';

$builder = $this->createConfigBuilder($phpunitXmlPath);

$xml = file_get_contents($builder->build(
[],
self::MUTATED_FILE_PATH,
self::HASH,
self::ORIGINAL_FILE_PATH,
'5.2'
));

$failOnRisky = $this->queryXpath($xml, sprintf('/phpunit/@%s', 'failOnWarning'));

$this->assertInstanceOf(DOMNodeList::class, $failOnRisky);
$this->assertSame('false', $failOnRisky[0]->value);
}

public function failOnProvider(): iterable
{
yield 'PHPUnit 5.1.99 runs without failOnRisky' => [
'5.1.99',
'failOnRisky',
0,
];

yield 'PHPUnit 5.2 runs with failOnRisky' => [
'5.2',
'failOnRisky',
1,
];

yield 'PHPUnit 5.3.1 runs with failOnRisky' => [
'5.3.1',
'failOnRisky',
1,
];

yield 'PHPUnit 5.1.99 runs without resolveDependencies' => [
'5.1.99',
'failOnWarning',
0,
];

yield 'PHPUnit 5.2 runs with resolveDependencies' => [
'5.2',
'failOnWarning',
1,
];

yield 'PHPUnit 5.3.1 runs resolveDependencies' => [
'5.3.1',
'failOnWarning',
1,
];
}

public function locationsProvider(): iterable
{
yield [
Expand Down