Skip to content

Commit

Permalink
Merge pull request #61 from friends-of-phpspec/chore/phpunit-php-code…
Browse files Browse the repository at this point in the history
…-coverage-11

Compatibility with PHPUnit 11
  • Loading branch information
shulard authored Jul 16, 2024
2 parents 14ca8bb + 1dc3e64 commit ff137d7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![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)
[![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)
[![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)

# phpspec-code-coverage

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

## Compatibility

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

## Change Log

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

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"require": {
"php": ">= 7.3",
"phpspec/phpspec": "^6.0 || ^7.0",
"phpunit/php-code-coverage": "^9.2 || ^10.0"
"phpunit/php-code-coverage": "^9.2 || ^10.0 || ^11.0",
"phpunit/php-file-iterator": "^3.0 || ^4.0 || ^5.0"
},
"require-dev": {
"phpstan/phpstan": "^1.5",
Expand Down
11 changes: 11 additions & 0 deletions spec/Listener/CodeCoverageListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,23 @@ public function let(ConsoleIO $io, Driver $driver)
public function it_can_process_all_directory_filtering_options(SuiteEvent $event)
{
$this->setOptions([
'whitelist' => [
'src',
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
['directory' => 'src', 'suffix' => 'Test.php'],
['directory' => 'src'],
],
'whitelist_files' => 'path/to/file.php',
'blacklist' => [
'src',
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
['directory' => 'src', 'suffix' => 'Test.php'],
['directory' => 'src'],
],
'blacklist_files' => [
'path/to/file.php',
'path/to/file2.php'
]
]);

$this
Expand Down
47 changes: 29 additions & 18 deletions src/Listener/CodeCoverageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PhpSpec\Event\SuiteEvent;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Report;
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use function gettype;
Expand Down Expand Up @@ -138,10 +139,6 @@ public function beforeExample(ExampleEvent $event): void
$this->coverage->start($name);
}

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

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

foreach ($this->options['whitelist'] as $option) {
$settings = $this->filterDirectoryParams($option);

$filter->includeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']);
}

// We compute the list of file / folder to be excluded
// If the blacklist contains suffixes and/or prefixes, we extract an
// exhaustive list of files that match to be added in the excluded list.
$excludes = $this->options['blacklist_files'];
foreach ($this->options['blacklist'] as $option) {
$settings = $this->filterDirectoryParams($option);

$filter->excludeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']);
if (!empty($settings['suffix']) || !empty($settings['prefix'])) {
$excludes = $excludes + (new FileIteratorFacade())->getFilesAsArray(
$settings['directory'],
$settings['suffix'],
$settings['prefix']
);
} else {
$excludes[] = $settings['directory'];
}
}

$filter->includeFiles($this->options['whitelist_files']);

foreach ($this->options['blacklist_files'] as $option) {
$filter->excludeFile($option);
foreach ($this->options['whitelist'] as $option) {
$settings = $this->filterDirectoryParams($option);
$fileIterator = (new FileIteratorFacade())->getFilesAsArray(
[$settings['directory']] + $this->options['whitelist_files'],
$settings['suffix'],
$settings['prefix'],
// We exclude the files from the previously built list.
$excludes
);

foreach ($fileIterator as $file) {
$filter->includeFile($file);
}
}
}

Expand Down Expand Up @@ -193,7 +204,7 @@ public function setOptions(array $options): void
/**
* @param array<string, string>|string $option
*
* @return array{directory:string, prefix:string, suffix:string}
* @return array{directory:non-empty-string, prefix:string, suffix:string}
*/
protected function filterDirectoryParams($option): array
{
Expand All @@ -208,7 +219,7 @@ protected function filterDirectoryParams($option): array
));
}

if (!isset($option['directory'])) {
if (empty($option['directory'])) {
throw new ConfigurationException('Missing required directory path.');
}

Expand Down

0 comments on commit ff137d7

Please sign in to comment.