-
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TestLocator returns non-unique tests, but Mutation didn't know that (#…
…1310) * TestLocator returns non-unique tests, but Mutation didn't know that Fixes #1304 TestLocator returns non-unique tests, and JUnitTestCaseSorter works around that; we have to do that too. * Extend test case to include summation * Timings are per test suite, not per test, therefore we have to unique by test suite name * Fix PHPStan warning * Extract adding logic into JUnitTestCaseTimeAdder
- Loading branch information
Showing
10 changed files
with
305 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
91 changes: 91 additions & 0 deletions
91
src/TestFramework/Coverage/JUnit/JUnitTestCaseTimeAdder.php
This file contains 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,91 @@ | ||
<?php | ||
/** | ||
* This code is licensed under the BSD 3-Clause License. | ||
* | ||
* Copyright (c) 2017, Maks Rafalko | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Infection\TestFramework\Coverage\JUnit; | ||
|
||
use function array_sum; | ||
use Infection\AbstractTestFramework\Coverage\TestLocation; | ||
use function Safe\substr; | ||
use Traversable; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class JUnitTestCaseTimeAdder | ||
{ | ||
/** | ||
* @var TestLocation[] | ||
*/ | ||
private $tests; | ||
|
||
/** | ||
* @param TestLocation[] $tests | ||
*/ | ||
public function __construct(array $tests) | ||
{ | ||
$this->tests = $tests; | ||
} | ||
|
||
public function getTotalTestTime(): float | ||
{ | ||
return array_sum( | ||
iterator_to_array( | ||
$this->uniqueTestLocations(), | ||
true // Duplicate keys must be overwritten. | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Returns unique'd test cases with timings. Timings are per test suite, not per test, therefore we have to unique by test suite name. | ||
* | ||
* @return Traversable<string, float|null> | ||
*/ | ||
private function uniqueTestLocations(): Traversable | ||
{ | ||
foreach ($this->tests as $testLocation) { | ||
$methodName = $testLocation->getMethod(); | ||
$methodSeparatorPos = strpos($methodName, '::'); | ||
|
||
if ($methodSeparatorPos === false) { | ||
// Just for the off case where we have rubbish in the test method name | ||
continue; | ||
} | ||
|
||
// For each test we discard method name, and return a single timing for an entire suite | ||
yield substr($methodName, 0, $methodSeparatorPos) => $testLocation->getExecutionTime(); | ||
} | ||
} | ||
} |
This file contains 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,15 @@ | ||
{ | ||
"require-dev": { | ||
"phpunit/phpunit": "^8" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"TimeoutSkipped\\": "src/" | ||
} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": { | ||
"TimeoutSkipped\\Test\\": "tests/" | ||
} | ||
} | ||
} |
This file contains 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,8 @@ | ||
Total: 5 | ||
|
||
Killed: 2 | ||
Errored: 0 | ||
Escaped: 2 | ||
Timed Out: 1 | ||
Skipped: 0 | ||
Not Covered: 0 |
This file contains 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,12 @@ | ||
{ | ||
"timeout": 2, | ||
"source": { | ||
"directories": [ | ||
"src" | ||
] | ||
}, | ||
"logs": { | ||
"summary": "infection.log" | ||
}, | ||
"tmpDir": "." | ||
} |
This file contains 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,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> |
This file contains 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,12 @@ | ||
<?php | ||
|
||
namespace TimeoutSkipped; | ||
|
||
class SourceClass | ||
{ | ||
public function add(int $a, int $b): int | ||
{ | ||
sleep(1); | ||
return $a + $b; | ||
} | ||
} |
This file contains 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,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); | ||
} | ||
} |
This file contains 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
109 changes: 109 additions & 0 deletions
109
tests/phpunit/TestFramework/Coverage/JUnit/JUnitTestCaseTimeAdderTest.php
This file contains 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,109 @@ | ||
<?php | ||
/** | ||
* This code is licensed under the BSD 3-Clause License. | ||
* | ||
* Copyright (c) 2017, Maks Rafalko | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* * Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* | ||
* * Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* * Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Infection\Tests\TestFramework\Coverage\JUnit; | ||
|
||
use Infection\AbstractTestFramework\Coverage\TestLocation; | ||
use Infection\TestFramework\Coverage\JUnit\JUnitTestCaseTimeAdder; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class JUnitTestCaseTimeAdderTest extends TestCase | ||
{ | ||
public function test_it_returns_time_for_the_only_test(): void | ||
{ | ||
$coverageTestCases = [ | ||
new TestLocation( | ||
'Test::testMethod1', | ||
'/path/to/test-file-1', | ||
0.000234 | ||
), | ||
]; | ||
|
||
$adder = new JUnitTestCaseTimeAdder($coverageTestCases); | ||
|
||
$total = $adder->getTotalTestTime(); | ||
|
||
$this->assertSame(0.000234, $total); | ||
} | ||
|
||
public function test_it_ignores_tests_without_valid_suite_name(): void | ||
{ | ||
$coverageTestCases = [ | ||
new TestLocation( | ||
'rubbish', | ||
'/path/to/test-file-1', | ||
0.000234 | ||
), | ||
]; | ||
|
||
$adder = new JUnitTestCaseTimeAdder($coverageTestCases); | ||
|
||
$total = $adder->getTotalTestTime(); | ||
|
||
$this->assertSame(0.0, $total); | ||
} | ||
|
||
public function test_it_returns_sum_for_uniqued_test_cases(): void | ||
{ | ||
$coverageTestCases = [ | ||
new TestLocation( | ||
'FooTest::testMethod1', | ||
'/path/to/test-file-1', | ||
0.9 | ||
), | ||
new TestLocation( | ||
'FooTest::testMethod2', | ||
'/path/to/test-file-2', | ||
0.9 | ||
), | ||
new TestLocation( | ||
'BarTest::testMethod3_1', | ||
'/path/to/test-file-3', | ||
0.2 | ||
), | ||
new TestLocation( | ||
'BarTest::testMethod3_2', | ||
'/path/to/test-file-4', | ||
0.2 | ||
), | ||
]; | ||
|
||
$adder = new JUnitTestCaseTimeAdder($coverageTestCases); | ||
|
||
$total = $adder->getTotalTestTime(); | ||
|
||
$this->assertSame(0.9 + 0.2, $total); | ||
} | ||
} |