Skip to content

Commit 69eab89

Browse files
committed
Create a custom Exception when Coverage driver can’t be found.
1 parent ca94167 commit 69eab89

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace spec\LeanPHP\PhpSpec\CodeCoverage\Exception;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use LeanPHP\PhpSpec\CodeCoverage\Exception\NoCoverageDriverAvailableException;
7+
8+
/**
9+
* @author Stéphane Hulard
10+
*/
11+
class NoCoverageDriverAvailableExceptionSpec extends ObjectBehavior
12+
{
13+
function it_is_initializable()
14+
{
15+
$this->shouldBeAnInstanceOf(\RuntimeException::class);
16+
$this->shouldHaveType(NoCoverageDriverAvailableException::class);
17+
}
18+
}

src/CodeCoverageExtension.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use SebastianBergmann\CodeCoverage\Report;
2121
use SebastianBergmann\CodeCoverage\Version;
2222
use Symfony\Component\Console\Input\InputOption;
23+
use LeanPHP\PhpSpec\CodeCoverage\Exception\NoCoverageDriverAvailableException;
2324

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

4546
$container->define('code_coverage', function ($container) {
46-
return new CodeCoverage(null, $container->get('code_coverage.filter'));
47+
try {
48+
$coverage = new CodeCoverage(null, $container->get('code_coverage.filter'));
49+
} catch (\RuntimeException $error) {
50+
throw new NoCoverageDriverAvailableException(
51+
"There is no available coverage driver to be used.",
52+
0,
53+
$error
54+
);
55+
}
56+
57+
return $coverage;
4758
});
4859

4960
$container->define('code_coverage.options', function ($container) use ($params) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
/**
3+
* This file is part of the leanphp/phpspec-code-coverage package
4+
*
5+
* @author shulard <s.hulard@chstudio.fr>
6+
*
7+
* @license MIT
8+
*
9+
* For the full copyright and license information, please see the LICENSE file
10+
* that was distributed with this source code.
11+
*
12+
*/
13+
namespace LeanPHP\PhpSpec\CodeCoverage\Exception;
14+
15+
use \RuntimeException;
16+
17+
/**
18+
* When PHPUnit/CodeCoverage trigger an exception which says that's no driver
19+
* can be found, we catch it to decorate with our own.
20+
*
21+
* @author Stéphane Hulard
22+
*/
23+
class NoCoverageDriverAvailableException extends RuntimeException
24+
{
25+
}

0 commit comments

Comments
 (0)