Skip to content

Commit

Permalink
TestLocator returns non-unique tests, but Mutation didn't know that
Browse files Browse the repository at this point in the history
Fixes #1304

TestLocator returns non-unique tests, and JUnitTestCaseSorter works around that; we have to do that too.
  • Loading branch information
sanmai committed Aug 24, 2020
1 parent 31e4af4 commit 32274f8
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/Mutation/Mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@

use function array_intersect_key;
use function array_keys;
use function array_map;
use function array_sum;
use function implode;
use Infection\AbstractTestFramework\Coverage\TestLocation;
use Infection\Mutator\ProfileList;
use Infection\PhpParser\MutatedNode;
use function md5;
use PhpParser\Node;
use function Pipeline\take;
use function Safe\array_flip;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -165,11 +165,13 @@ public function getAllTests(): array
*/
public function getNominalTestExecutionTime(): float
{
return $this->nominalTimeToTest ?? $this->nominalTimeToTest = array_sum(array_map(
static function (TestLocation $testLocation) {
return $testLocation->getExecutionTime();
},
$this->tests
// TestLocator returns non-unique tests, and JUnitTestCaseSorter works around that; we have to do that too.
return $this->nominalTimeToTest ?? $this->nominalTimeToTest = array_sum(iterator_to_array(
take($this->tests)
->map(static function (TestLocation $testLocation) {
yield $testLocation->getMethod() => $testLocation->getExecutionTime();
}),
true
));
}

Expand Down
15 changes: 15 additions & 0 deletions tests/e2e/TimeoutSkipped/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"require-dev": {
"phpunit/phpunit": "^8"
},
"autoload": {
"psr-4": {
"TimeoutSkipped\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"TimeoutSkipped\\Test\\": "tests/"
}
}
}
8 changes: 8 additions & 0 deletions tests/e2e/TimeoutSkipped/expected-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Total: 5

Killed: 2
Errored: 0
Escaped: 2
Timed Out: 1
Skipped: 0
Not Covered: 0
12 changes: 12 additions & 0 deletions tests/e2e/TimeoutSkipped/infection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"timeout": 2,
"source": {
"directories": [
"src"
]
},
"logs": {
"summary": "infection.log"
},
"tmpDir": "."
}
18 changes: 18 additions & 0 deletions tests/e2e/TimeoutSkipped/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
12 changes: 12 additions & 0 deletions tests/e2e/TimeoutSkipped/src/SourceClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace TimeoutSkipped;

class SourceClass
{
public function add(int $a, int $b): int
{
sleep(1);
return $a + $b;
}
}
18 changes: 18 additions & 0 deletions tests/e2e/TimeoutSkipped/tests/SourceClassTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace TimeoutSkipped\Test;

use TimeoutSkipped\SourceClass;
use PHPUnit\Framework\TestCase;

class SourceClassTest extends TestCase
{
public function test_it_adds_2_numbers(): void
{
$source = new SourceClass();

$result = $source->add(1, 2);

self::assertSame(3, $result);
}
}
15 changes: 14 additions & 1 deletion tests/phpunit/Mutation/MutationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function test_it_can_be_instantiated(
MutatedNode $mutatedNode,
int $mutationByMutatorIndex,
array $tests,
float $timeToTest,
array $expectedAttributes,
int $expectedOriginalStartingLine,
bool $expectedCoveredByTests,
Expand All @@ -88,6 +89,7 @@ public function test_it_can_be_instantiated(
$this->assertSame($mutatedNodeClass, $mutation->getMutatedNodeClass());
$this->assertSame($mutatedNode, $mutation->getMutatedNode());
$this->assertSame($tests, $mutation->getAllTests());
$this->assertSame($timeToTest, $mutation->getNominalTestExecutionTime());
$this->assertSame($expectedCoveredByTests, $mutation->isCoveredByTest());
$this->assertSame($expectedHash, $mutation->getHash());
}
Expand All @@ -112,6 +114,7 @@ public function valuesProvider(): iterable
MutatedNode::wrap(new Node\Scalar\LNumber(1)),
-1,
[],
0.0,
$nominalAttributes,
$originalStartingLine,
false,
Expand All @@ -136,6 +139,7 @@ public function valuesProvider(): iterable
0.01
),
],
0.01,
$nominalAttributes,
$originalStartingLine,
true,
Expand All @@ -160,6 +164,7 @@ public function valuesProvider(): iterable
0.01
),
],
0.01,
$nominalAttributes,
$originalStartingLine,
true,
Expand All @@ -181,9 +186,15 @@ public function valuesProvider(): iterable
new TestLocation(
'FooTest::test_it_can_instantiate',
'/path/to/acme/FooTest.php',
0.01
1.1
),
new TestLocation(
'FooTest::test_it_can_instantiate',
'/path/to/acme/FooTest.php',
1.1
),
],
1.1,
$nominalAttributes,
$originalStartingLine,
true,
Expand All @@ -202,6 +213,7 @@ public function valuesProvider(): iterable
MutatedNode::wrap(new Node\Scalar\LNumber(1)),
0,
[],
0.0,
$nominalAttributes,
$originalStartingLine,
false,
Expand Down Expand Up @@ -229,6 +241,7 @@ public function valuesProvider(): iterable
0.01
),
],
0.01,
$nominalAttributes,
$originalStartingLine,
true,
Expand Down

0 comments on commit 32274f8

Please sign in to comment.