Skip to content

Commit 6891acb

Browse files
Fix code coverage filter not being configured when an extension requires code coverage collection
1 parent b44cfea commit 6891acb

6 files changed

Lines changed: 119 additions & 1 deletion

File tree

src/Runner/CodeCoverage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static function instance(): self
8080

8181
public function init(Configuration $configuration, CodeCoverageFilterRegistry $codeCoverageFilterRegistry, bool $extensionRequiresCodeCoverageCollection): CodeCoverageInitializationStatus
8282
{
83-
$codeCoverageFilterRegistry->init($configuration);
83+
$codeCoverageFilterRegistry->init($configuration, $extensionRequiresCodeCoverageCollection);
8484

8585
if (!$configuration->hasCoverageReport() && !$extensionRequiresCodeCoverageCollection) {
8686
return CodeCoverageInitializationStatus::NOT_REQUESTED;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="../../../../../phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
cacheDirectory=".phpunit.cache.with-extension-that-requires-code-coverage-collection">
6+
<testsuites>
7+
<testsuite name="default">
8+
<directory>tests</directory>
9+
</testsuite>
10+
</testsuites>
11+
12+
<source>
13+
<include>
14+
<file>src/Foo.php</file>
15+
</include>
16+
</source>
17+
18+
<coverage driver="PHPUnit\TestFixture\CodeCoverageDriver\CustomDriverWithFakeData"/>
19+
20+
<extensions>
21+
<bootstrap class="PHPUnit\TestFixture\CodeCoverageDriver\ExtensionThatRequiresCodeCoverageCollection"/>
22+
</extensions>
23+
</phpunit>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\CodeCoverageDriver;
11+
12+
use PHPUnit\Runner\Extension\Extension;
13+
use PHPUnit\Runner\Extension\Facade;
14+
use PHPUnit\Runner\Extension\ParameterCollection;
15+
use PHPUnit\TextUI\Configuration\Configuration;
16+
17+
final class ExtensionThatRequiresCodeCoverageCollection implements Extension
18+
{
19+
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
20+
{
21+
$facade->requireCodeCoverageCollection();
22+
23+
$facade->registerSubscriber(new PrintCollectedCodeCoverageSubscriber);
24+
}
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace PHPUnit\TestFixture\CodeCoverageDriver;
11+
12+
use const PHP_EOL;
13+
use function array_keys;
14+
use function basename;
15+
use function printf;
16+
use PHPUnit\Event\TestRunner\ExecutionFinished;
17+
use PHPUnit\Event\TestRunner\ExecutionFinishedSubscriber;
18+
use PHPUnit\Runner\CodeCoverage;
19+
20+
final class PrintCollectedCodeCoverageSubscriber implements ExecutionFinishedSubscriber
21+
{
22+
public function notify(ExecutionFinished $event): void
23+
{
24+
$codeCoverage = CodeCoverage::instance();
25+
26+
if (!$codeCoverage->isActive()) {
27+
print 'code coverage is not being collected' . PHP_EOL;
28+
29+
return;
30+
}
31+
32+
foreach (array_keys($codeCoverage->codeCoverage()->getData()->lineCoverage()) as $file) {
33+
printf(
34+
'code coverage was collected for %s%s',
35+
basename($file),
36+
PHP_EOL,
37+
);
38+
}
39+
}
40+
}

tests/end-to-end/code-coverage/_files/code-coverage-driver/vendor/autoload.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
require __DIR__ . '/../src/CustomDriverWithFilter.php';
88
require __DIR__ . '/../src/CustomDriverWithFakeData.php';
99
require __DIR__ . '/../src/CustomDriverWithBranchCoverage.php';
10+
require __DIR__ . '/../src/PrintCollectedCodeCoverageSubscriber.php';
11+
require __DIR__ . '/../src/ExtensionThatRequiresCodeCoverageCollection.php';
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Code coverage is collected for an extension that requires it when no code coverage report is configured
3+
--FILE--
4+
<?php declare(strict_types=1);
5+
$_SERVER['argv'][] = '--do-not-record-test-run-history';
6+
$_SERVER['argv'][] = '--no-progress';
7+
$_SERVER['argv'][] = '--colors=never';
8+
$_SERVER['argv'][] = '--configuration';
9+
$_SERVER['argv'][] = __DIR__ . '/_files/code-coverage-driver/phpunit-with-extension-that-requires-code-coverage-collection.xml';
10+
11+
require __DIR__ . '/../../bootstrap.php';
12+
13+
(new PHPUnit\TextUI\Application)->run($_SERVER['argv']);
14+
--CLEAN--
15+
<?php declare(strict_types=1);
16+
require __DIR__ . '/../../_files/delete_directory.php';
17+
18+
delete_directory(__DIR__ . '/_files/code-coverage-driver/.phpunit.cache.with-extension-that-requires-code-coverage-collection');
19+
--EXPECTF--
20+
PHPUnit %s by Sebastian Bergmann and contributors.
21+
22+
Runtime: %s with CustomDriverWithFakeData 1.0.0
23+
Configuration: %s
24+
25+
code coverage was collected for Foo.php
26+
Time: %s, Memory: %s
27+
28+
OK (1 test, 1 assertion)

0 commit comments

Comments
 (0)