Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility with PHPUnit 11 #61

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add a dedicated blacklist-test.

Otherwise our code-coverage will drop.

Sadly the CI pipeline didn't upload the code-coverage to Scrutinizer (https://scrutinizer-ci.com/g/friends-of-phpspec/phpspec-code-coverage/), maybe something is broken there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has been pushed but you need to check the inspections. The dashboard page is only displaying master build.

Here is the result for the latest push: https://scrutinizer-ci.com/g/friends-of-phpspec/phpspec-code-coverage/inspections/8024bdbd-49db-4a89-91b3-d0a179fdf1e5

'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;
shulard marked this conversation as resolved.
Show resolved Hide resolved
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
Loading