Skip to content

Allow to use code coverage with PHPSpec 5.0. #38

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

Closed
wants to merge 4 commits into from
Closed
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
},
"require": {
"php": "^7.1",
"phpspec/phpspec": "^4.2",
"phpunit/php-code-coverage": "^5.0||^6.0"
"phpspec/phpspec": "^4.2||^5.0",
"phpunit/php-code-coverage": "^5.0||^6.0||^7.0"
},
"require-dev": {
"escapestudios/symfony2-coding-standard": "^3.1",
Expand Down
5 changes: 2 additions & 3 deletions spec/CodeCoverageExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PhpSpec\ObjectBehavior;
use PhpSpec\ServiceContainer\IndexedServiceContainer;
use Prophecy\Argument;
use LeanPHP\PhpSpec\CodeCoverage\CodeCoverageExtension;

/**
* @author Henrik Bjornskov
Expand All @@ -13,7 +14,7 @@ class CodeCoverageExtensionSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('LeanPHP\PhpSpec\CodeCoverage\CodeCoverageExtension');
$this->shouldHaveType(CodeCoverageExtension::class);
}

function it_should_use_html_format_by_default()
Expand Down Expand Up @@ -50,7 +51,5 @@ function it_should_use_singular_output()
if ($options['output'] !== ['foo' => 'test']) {
throw new Exception("Default format is not singular output");
}


}
}
18 changes: 18 additions & 0 deletions spec/Exception/NoCoverageDriverAvailableExceptionSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace spec\LeanPHP\PhpSpec\CodeCoverage\Exception;

use PhpSpec\ObjectBehavior;
use LeanPHP\PhpSpec\CodeCoverage\Exception\NoCoverageDriverAvailableException;

/**
* @author Stéphane Hulard
*/
class NoCoverageDriverAvailableExceptionSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldBeAnInstanceOf(\RuntimeException::class);
$this->shouldHaveType(NoCoverageDriverAvailableException::class);
}
}
13 changes: 12 additions & 1 deletion src/CodeCoverageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use SebastianBergmann\CodeCoverage\Report;
use SebastianBergmann\CodeCoverage\Version;
use Symfony\Component\Console\Input\InputOption;
use LeanPHP\PhpSpec\CodeCoverage\Exception\NoCoverageDriverAvailableException;

/**
* Injects Code Coverage Event Subscriber into the EventDispatcher.
Expand All @@ -43,7 +44,17 @@ public function load(ServiceContainer $container, array $params = [])
});

$container->define('code_coverage', function ($container) {
return new CodeCoverage(null, $container->get('code_coverage.filter'));
try {
$coverage = new CodeCoverage(null, $container->get('code_coverage.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', function ($container) use ($params) {
Expand Down
25 changes: 25 additions & 0 deletions src/Exception/NoCoverageDriverAvailableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* This file is part of the leanphp/phpspec-code-coverage package
*
* @author shulard <[email protected]>
*
* @license MIT
*
* For the full copyright and license information, please see the LICENSE file
* that was distributed with this source code.
*
*/
namespace LeanPHP\PhpSpec\CodeCoverage\Exception;

use \RuntimeException;

/**
* When PHPUnit/CodeCoverage trigger an exception which says that's no driver
* can be found, we catch it to decorate with our own.
*
* @author Stéphane Hulard
*/
class NoCoverageDriverAvailableException extends RuntimeException
{
}
16 changes: 5 additions & 11 deletions src/Listener/CodeCoverageListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class CodeCoverageListener implements EventSubscriberInterface
private $reports;
private $io;
private $options;
private $enabled;
private $skipCoverage;

/**
Expand All @@ -51,7 +50,6 @@ public function __construct(ConsoleIO $io, CodeCoverage $coverage, array $report
'format' => ['html'],
];

$this->enabled = extension_loaded('xdebug') || (PHP_SAPI === 'phpdbg');
$this->skipCoverage = $skipCoverage;
}

Expand All @@ -63,7 +61,7 @@ public function __construct(ConsoleIO $io, CodeCoverage $coverage, array $report
*/
public function beforeSuite(SuiteEvent $event) : void
{
if (!$this->enabled || $this->skipCoverage) {
if ($this->skipCoverage) {
return;
}

Expand Down Expand Up @@ -92,7 +90,7 @@ public function beforeSuite(SuiteEvent $event) : void
*/
public function beforeExample(ExampleEvent $event): void
{
if (!$this->enabled || $this->skipCoverage) {
if ($this->skipCoverage) {
return;
}

Expand All @@ -111,7 +109,7 @@ public function beforeExample(ExampleEvent $event): void
*/
public function afterExample(ExampleEvent $event): void
{
if (!$this->enabled || $this->skipCoverage) {
if ($this->skipCoverage) {
return;
}

Expand All @@ -123,13 +121,9 @@ public function afterExample(ExampleEvent $event): void
*/
public function afterSuite(SuiteEvent $event): void
{
if (!$this->enabled || $this->skipCoverage) {
if ($this->skipCoverage) {
if ($this->io && $this->io->isVerbose()) {
if (!$this->enabled) {
$this->io->writeln('No code coverage will be generated as neither Xdebug nor phpdbg was detected.');
} elseif ($this->skipCoverage) {
$this->io->writeln('Skipping code coverage generation');
}
$this->io->writeln('Skipping code coverage generation');
}

return;
Expand Down