Skip to content

Commit ff137d7

Browse files
authored
Merge pull request #61 from friends-of-phpspec/chore/phpunit-php-code-coverage-11
Compatibility with PHPUnit 11
2 parents 14ca8bb + 1dc3e64 commit ff137d7

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![GitHub Workflow Status](https://github.com/friends-of-phpspec/phpspec-code-coverage/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/friends-of-phpspec/phpspec-code-coverage/actions)
55
[![Scrutinizer code quality](https://img.shields.io/scrutinizer/quality/g/friends-of-phpspec/phpspec-code-coverage/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/friends-of-phpspec/phpspec-code-coverage/?branch=master)
66
[![License](https://img.shields.io/packagist/l/friends-of-phpspec/phpspec-code-coverage.svg?style=flat-square)](https://packagist.org/packages/friends-of-phpspec/phpspec-code-coverage)
7-
7+
88
# phpspec-code-coverage
99

1010
[phpspec-code-coverage][0] is a [PhpSpec][2] extension that generates Code
@@ -22,14 +22,14 @@ used as a single metric defining how good your tests are.
2222
- PHP 7+ (for [PhpSpec][2] v4+) or PHP 5.6+ (for [PhpSpec][2] v3)
2323
- [Xdebug][3], [phpdbg][4] or [pcov][6] extension enabled (PHP 7+ is required for code
2424
generation to work with [phpdbg][4]).
25-
25+
2626
## Compatibility
2727

28-
| phpspec-code-coverage | PHP | phpspec | phpunit |
29-
|-----------------------|----------|----------------------------|----------------------------|
30-
| 4.x | `^7.1` | `^4.2 \|\| ^5.0 \|\| ^6.0` | `^5.0 \|\| ^6.0 \|\| ^7.0` |
31-
| 5.x | `>= 7.2` | `^5.0 \|\| ^6.0 \|\| ^7.0` | `^6.0 \|\| ^7.0 \|\| ^8.0` |
32-
| 6.x | `>= 7.3` | `^6.0 \|\| ^7.0` | `^9.0` |
28+
| phpspec-code-coverage | PHP | phpspec | phpunit |
29+
|-----------------------|----------|----------------------------|------------------------------|
30+
| 4.x | `^7.1` | `^4.2 \|\| ^5.0 \|\| ^6.0` | `^5.0 \|\| ^6.0 \|\| ^7.0` |
31+
| 5.x | `>= 7.2` | `^5.0 \|\| ^6.0 \|\| ^7.0` | `^6.0 \|\| ^7.0 \|\| ^8.0` |
32+
| 6.x | `>= 7.3` | `^6.0 \|\| ^7.0` | `^9.2 \|\| ^10.0 \|\| ^11.0` |
3333

3434
## Change Log
3535

@@ -171,11 +171,11 @@ extensions:
171171
* `high_lower_bound` (optional) sets high lower bound for code coverage
172172
(default `70`)
173173
* `whitelist` takes an array of directories to whitelist (default: `lib`,
174-
`src`). The array can be made more specific if an associative array is
174+
`src`). The array can be made more specific if an associative array is
175175
given with the following keys (`directory`, `prefix`, `suffix`)
176176
* `whitelist_files` takes an array of files to whitelist (default: none).
177177
* `blacklist` takes an array of directories to blacklist (default: `test,
178-
vendor, spec`). The array can be made more specific if an associative
178+
vendor, spec`). The array can be made more specific if an associative
179179
array is given with the following keys (`directory`, `prefix`, `suffix`)
180180
* `blacklist_files` takes an array of files to blacklist
181181

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"require": {
4646
"php": ">= 7.3",
4747
"phpspec/phpspec": "^6.0 || ^7.0",
48-
"phpunit/php-code-coverage": "^9.2 || ^10.0"
48+
"phpunit/php-code-coverage": "^9.2 || ^10.0 || ^11.0",
49+
"phpunit/php-file-iterator": "^3.0 || ^4.0 || ^5.0"
4950
},
5051
"require-dev": {
5152
"phpstan/phpstan": "^1.5",

spec/Listener/CodeCoverageListenerSpec.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,23 @@ public function let(ConsoleIO $io, Driver $driver)
3535
public function it_can_process_all_directory_filtering_options(SuiteEvent $event)
3636
{
3737
$this->setOptions([
38+
'whitelist' => [
39+
'src',
40+
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
41+
['directory' => 'src', 'suffix' => 'Test.php'],
42+
['directory' => 'src'],
43+
],
44+
'whitelist_files' => 'path/to/file.php',
3845
'blacklist' => [
3946
'src',
4047
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
4148
['directory' => 'src', 'suffix' => 'Test.php'],
4249
['directory' => 'src'],
4350
],
51+
'blacklist_files' => [
52+
'path/to/file.php',
53+
'path/to/file2.php'
54+
]
4455
]);
4556

4657
$this

src/Listener/CodeCoverageListener.php

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PhpSpec\Event\SuiteEvent;
2121
use SebastianBergmann\CodeCoverage\CodeCoverage;
2222
use SebastianBergmann\CodeCoverage\Report;
23+
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
2324
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2425

2526
use function gettype;
@@ -138,10 +139,6 @@ public function beforeExample(ExampleEvent $event): void
138139
$this->coverage->start($name);
139140
}
140141

141-
/**
142-
* Note: We use array_map() instead of array_walk() because the latter expects
143-
* the callback to take the value as the first and the index as the seconds parameter.
144-
*/
145142
public function beforeSuite(SuiteEvent $event): void
146143
{
147144
if ($this->skipCoverage) {
@@ -150,22 +147,36 @@ public function beforeSuite(SuiteEvent $event): void
150147

151148
$filter = $this->coverage->filter();
152149

153-
foreach ($this->options['whitelist'] as $option) {
154-
$settings = $this->filterDirectoryParams($option);
155-
156-
$filter->includeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']);
157-
}
158-
150+
// We compute the list of file / folder to be excluded
151+
// If the blacklist contains suffixes and/or prefixes, we extract an
152+
// exhaustive list of files that match to be added in the excluded list.
153+
$excludes = $this->options['blacklist_files'];
159154
foreach ($this->options['blacklist'] as $option) {
160155
$settings = $this->filterDirectoryParams($option);
161-
162-
$filter->excludeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']);
156+
if (!empty($settings['suffix']) || !empty($settings['prefix'])) {
157+
$excludes = $excludes + (new FileIteratorFacade())->getFilesAsArray(
158+
$settings['directory'],
159+
$settings['suffix'],
160+
$settings['prefix']
161+
);
162+
} else {
163+
$excludes[] = $settings['directory'];
164+
}
163165
}
164166

165-
$filter->includeFiles($this->options['whitelist_files']);
166-
167-
foreach ($this->options['blacklist_files'] as $option) {
168-
$filter->excludeFile($option);
167+
foreach ($this->options['whitelist'] as $option) {
168+
$settings = $this->filterDirectoryParams($option);
169+
$fileIterator = (new FileIteratorFacade())->getFilesAsArray(
170+
[$settings['directory']] + $this->options['whitelist_files'],
171+
$settings['suffix'],
172+
$settings['prefix'],
173+
// We exclude the files from the previously built list.
174+
$excludes
175+
);
176+
177+
foreach ($fileIterator as $file) {
178+
$filter->includeFile($file);
179+
}
169180
}
170181
}
171182

@@ -193,7 +204,7 @@ public function setOptions(array $options): void
193204
/**
194205
* @param array<string, string>|string $option
195206
*
196-
* @return array{directory:string, prefix:string, suffix:string}
207+
* @return array{directory:non-empty-string, prefix:string, suffix:string}
197208
*/
198209
protected function filterDirectoryParams($option): array
199210
{
@@ -208,7 +219,7 @@ protected function filterDirectoryParams($option): array
208219
));
209220
}
210221

211-
if (!isset($option['directory'])) {
222+
if (empty($option['directory'])) {
212223
throw new ConfigurationException('Missing required directory path.');
213224
}
214225

0 commit comments

Comments
 (0)