Skip to content

Commit

Permalink
Merge pull request #39 from jaylinski/php8
Browse files Browse the repository at this point in the history
Add support for PHP 8 and phpunit/php-code-coverage 9
  • Loading branch information
shulard authored Nov 30, 2020
2 parents ada115c + 0fc140f commit d1957ce
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 186 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ jobs:
fail-fast: false
matrix:
operating-system: [ubuntu-latest, windows-latest, macOS-latest]
php-versions: ['7.2', '7.3', '7.4']
php-versions: ['7.3', '7.4', '8.0']
composer-versions: ['composer:v1', 'composer:v2']

steps:
- name: Set git to use LF
Expand All @@ -31,7 +32,8 @@ jobs:
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring,xdebug,pcov
tools: ${{ matrix.composer-versions }}
extensions: mbstring, xdebug, pcov

- name: Get Composer Cache Directory
id: composer-cache
Expand All @@ -45,16 +47,20 @@ jobs:
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
if: ${{ matrix.php-versions != '8.0' }} # Remove this as soon as `drupol/php-conventions` supports PHP 8
run: composer install --no-progress --no-suggest --prefer-dist --optimize-autoloader

- name: Run Grumphp
if: ${{ matrix.php-versions != '8.0' }} # Remove this as soon as `drupol/php-conventions` supports PHP 8
run: vendor/bin/grumphp run --no-ansi -n

- name: Send PSALM data
if: ${{ matrix.php-versions != '8.0' }} # Remove this as soon as `drupol/php-conventions` supports PHP 8
run: vendor/bin/psalm --shepherd --stats
continue-on-error: true

- name: Send Scrutinizer data
if: ${{ matrix.php-versions != '8.0' }} # Remove this as soon as `drupol/php-conventions` supports PHP 8
run: |
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover build/logs/clover.xml
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [6.0.0] - 2020-11-28

- Support PHP 8
- Extension requires PHP7.3+ (due to `phpunit/php-code-coverage` v9 depending on it) #36
Version 5.x will still be maintained

## [5.0.0] - 2020-11-16

- Extension requires PHP7.2+ (due to PhpSpec v7 depending on it) #37, #35
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +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` |

## Change Log

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
}
],
"require": {
"php": ">= 7.2",
"phpspec/phpspec": "^5.0 || ^6.0 || ^7.0",
"phpunit/php-code-coverage": "^6.0 || ^7.0 || ^8.0"
"php": ">= 7.3",
"phpspec/phpspec": "^6.0 || ^7.0",
"phpunit/php-code-coverage": "^9.0"
},
"conflict": {
"sebastian/comparator": "< 2.0"
Expand All @@ -54,7 +54,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.x-dev"
"dev-master": "6.x-dev"
}
},
"autoload": {
Expand Down
198 changes: 27 additions & 171 deletions spec/Listener/CodeCoverageListenerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@

use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener\CodeCoverageListener;
use PhpSpec\Console\ConsoleIO;
use PhpSpec\Event\SuiteEvent;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\Driver;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report;
use SebastianBergmann\CodeCoverage\RawCodeCoverageData;

/**
* Disabled due to tests breaking as php-code-coverage marked their classes
* final and we cannot mock them. The tests should be converted into proper
* functional (integration) tests instead. This file is left for reference.
*
* @see https://github.com/leanphp/phpspec-code-coverage/issues/19
*
* @author Henrik Bjornskov
*/
class CodeCoverageListenerSpec extends ObjectBehavior
Expand All @@ -25,174 +30,25 @@ public function it_is_initializable()

public function let(ConsoleIO $io)
{
$this->beConstructedWith($io, new CodeCoverage(), []);
$codeCoverage = new CodeCoverage(new DriverStub(), new Filter());

$this->beConstructedWith($io, $codeCoverage, []);
}
}

class DriverStub extends Driver
{
public function nameAndVersion(): string
{
return 'DriverStub';
}

/**
* Disabled due to tests breaking as php-code-coverage marked their classes
* final and we cannot mock them. The tests should be converted into proper
* functional (integration) tests instead. This file is left for reference.
*
* @see https://github.com/leanphp/phpspec-code-coverage/issues/19
*
* function let(ConsoleIO $io, CodeCoverage $coverage)
* {
* $this->beConstructedWith($io, $coverage, array());
* }
*
* function it_is_initializable()
* {
* $this->shouldHaveType('LeanPHP\PhpSpec\CodeCoverage\Listener\CodeCoverageListener');
* }
*
* function it_should_run_all_reports(
* CodeCoverage $coverage,
* Report\Clover $clover,
* Report\PHP $php,
* SuiteEvent $event,
* ConsoleIO $io
* ) {
* $reports = array(
* 'clover' => $clover,
* 'php' => $php
* );
*
* $io->isVerbose()->willReturn(false);
*
* $this->beConstructedWith($io, $coverage, $reports);
* $this->setOptions(array(
* 'format' => array('clover', 'php'),
* 'output' => array(
* 'clover' => 'coverage.xml',
* 'php' => 'coverage.php'
* )
* ));
*
* $clover->process($coverage, 'coverage.xml')->shouldBeCalled();
* $php->process($coverage, 'coverage.php')->shouldBeCalled();
*
* $this->afterSuite($event);
* }
*
* function it_should_color_output_text_report_by_default(
* CodeCoverage $coverage,
* Report\Text $text,
* SuiteEvent $event,
* ConsoleIO $io
* ) {
* $reports = array(
* 'text' => $text
* );
*
* $this->beConstructedWith($io, $coverage, $reports);
* $this->setOptions(array(
* 'format' => 'text'
* ));
*
* $io->isVerbose()->willReturn(false);
* $io->isDecorated()->willReturn(true);
*
* $text->process($coverage, true)->willReturn('report');
* $io->writeln('report')->shouldBeCalled();
*
* $this->afterSuite($event);
* }
*
* function it_should_not_color_output_text_report_unless_specified(
* CodeCoverage $coverage,
* Report\Text $text,
* SuiteEvent $event,
* ConsoleIO $io
* ) {
* $reports = array(
* 'text' => $text
* );
*
* $this->beConstructedWith($io, $coverage, $reports);
* $this->setOptions(array(
* 'format' => 'text'
* ));
*
* $io->isVerbose()->willReturn(false);
* $io->isDecorated()->willReturn(false);
*
* $text->process($coverage, false)->willReturn('report');
* $io->writeln('report')->shouldBeCalled();
*
* $this->afterSuite($event);
* }
*
* function it_should_output_html_report(
* CodeCoverage $coverage,
* Report\Html\Facade $html,
* SuiteEvent $event,
* ConsoleIO $io
* ) {
* $reports = array(
* 'html' => $html
* );
*
* $this->beConstructedWith($io, $coverage, $reports);
* $this->setOptions(array(
* 'format' => 'html',
* 'output' => array('html' => 'coverage'),
* ));
*
* $io->isVerbose()->willReturn(false);
* $io->writeln(Argument::any())->shouldNotBeCalled();
*
* $html->process($coverage, 'coverage')->willReturn('report');
*
* $this->afterSuite($event);
* }
*
* function it_should_provide_extra_output_in_verbose_mode(
* CodeCoverage $coverage,
* Report\Html\Facade $html,
* SuiteEvent $event,
* ConsoleIO $io
* ) {
* $reports = array(
* 'html' => $html,
* );
*
* $this->beConstructedWith($io, $coverage, $reports);
* $this->setOptions(array(
* 'format' => 'html',
* 'output' => array('html' => 'coverage'),
* ));
*
* $io->isVerbose()->willReturn(true);
* $io->writeln('')->shouldBeCalled();
* $io->writeln('Generating code coverage report in html format ...')->shouldBeCalled();
*
* $this->afterSuite($event);
* }
*
* function it_should_correctly_handle_black_listed_files_and_directories(
* CodeCoverage $coverage,
* SuiteEvent $event,
* Filter $filter,
* ConsoleIO $io
* )
* {
* $this->beConstructedWith($io, $coverage, array());
*
* $coverage->filter()->willReturn($filter);
*
* $this->setOptions(array(
* 'whitelist' => array('src'),
* 'blacklist' => array('src/filter'),
* 'whitelist_files' => array('src/filter/whilelisted_file'),
* 'blacklist_files' => array('src/filtered_file')
* ));
*
* $filter->addDirectoryToWhitelist('src')->shouldBeCalled();
* $filter->removeDirectoryFromWhitelist('src/filter')->shouldBeCalled();
* $filter->addFileToWhitelist('src/filter/whilelisted_file')->shouldBeCalled();
* $filter->removeFileFromWhitelist('src/filtered_file')->shouldBeCalled();
*
* $this->beforeSuite($event);
* }
*/
public function start(bool $determineUnusedAndDead = true): void
{
}

public function stop(): RawCodeCoverageData
{
return RawCodeCoverageData::fromXdebugWithoutPathCoverage([]);
}
}
8 changes: 5 additions & 3 deletions src/CodeCoverageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use PhpSpec\ServiceContainer;
use RuntimeException;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeCoverage\Driver\Selector;
use SebastianBergmann\CodeCoverage\Filter;
use SebastianBergmann\CodeCoverage\Report;
use SebastianBergmann\CodeCoverage\Version;
Expand Down Expand Up @@ -50,17 +51,18 @@ public function load(ServiceContainer $container, array $params = []): void
});

$container->define('code_coverage', static function ($container) {
/** @var Filter $filter */
$filter = $container->get('code_coverage.filter');

try {
$coverage = new CodeCoverage(null, $container->get('code_coverage.filter'));
return new CodeCoverage((new Selector())->forLineCoverage($filter), $filter);
} catch (RuntimeException $error) {
throw new NoCoverageDriverAvailableException(
'There is no available coverage driver to be used.',
0,
$error
);
}

return $coverage;
});

$container->define('code_coverage.options', static function ($container) use ($params) {
Expand Down
10 changes: 4 additions & 6 deletions src/Listener/CodeCoverageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,17 @@ public function beforeSuite(SuiteEvent $event): void
$filter = $this->coverage->filter();

foreach ($this->options['whitelist'] as $option) {
$filter->addDirectoryToWhitelist($option);
$filter->includeDirectory($option);
}

foreach ($this->options['blacklist'] as $option) {
$filter->removeDirectoryFromWhitelist($option);
$filter->excludeDirectory($option);
}

foreach ($this->options['whitelist_files'] as $option) {
$filter->addFilesToWhitelist($option);
}
$filter->includeFiles($this->options['whitelist_files']);

foreach ($this->options['blacklist_files'] as $option) {
$filter->removeFileFromWhitelist($option);
$filter->excludeFile($option);
}
}

Expand Down

0 comments on commit d1957ce

Please sign in to comment.