Skip to content

Commit 7ba5183

Browse files
committed
Adding Codeception tests to improve the feature
1 parent 0c4c8c0 commit 7ba5183

File tree

2 files changed

+99
-10
lines changed

2 files changed

+99
-10
lines changed

spec/Listener/CodeCoverageListenerSpec.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
namespace spec\FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener;
66

77
use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener\CodeCoverageListener;
8+
use InvalidArgumentException;
89
use PhpSpec\Console\ConsoleIO;
10+
use PhpSpec\Event\SuiteEvent;
911
use PhpSpec\ObjectBehavior;
1012
use SebastianBergmann\CodeCoverage\CodeCoverage;
1113
use SebastianBergmann\CodeCoverage\Driver\Driver;
1214
use SebastianBergmann\CodeCoverage\Filter;
1315
use SebastianBergmann\CodeCoverage\RawCodeCoverageData;
16+
use stdClass;
17+
use Throwable;
18+
use TypeError;
1419

1520
/**
1621
* Disabled due to tests breaking as php-code-coverage marked their classes
@@ -23,11 +28,66 @@
2328
*/
2429
class CodeCoverageListenerSpec extends ObjectBehavior
2530
{
31+
public function it_can_process_all_directory_filtering_options(SuiteEvent $event)
32+
{
33+
$this->setOptions([
34+
'blacklist' => [
35+
'src',
36+
['directory' => 'src', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
37+
['directory' => 'src', 'suffix' => 'Test.php'],
38+
['directory' => 'src'],
39+
],
40+
]);
41+
42+
$this
43+
->shouldNotThrow(TypeError::class)
44+
->during('beforeSuite', [$event]);
45+
}
46+
2647
public function it_is_initializable()
2748
{
2849
$this->shouldHaveType(CodeCoverageListener::class);
2950
}
3051

52+
public function it_will_ignore_unknown_directory_filtering_options(SuiteEvent $event)
53+
{
54+
$this->setOptions([
55+
'whitelist' => [
56+
['directory' => 'test', 'foobar' => 'baz'],
57+
],
58+
]);
59+
60+
$this
61+
->shouldNotThrow(Throwable::class)
62+
->during('beforeSuite', [$event]);
63+
}
64+
65+
public function it_will_throw_if_the_directory_filter_option_type_is_not_supported(SuiteEvent $event)
66+
{
67+
$this->setOptions([
68+
'whitelist' => [
69+
new stdClass(),
70+
],
71+
]);
72+
73+
$this
74+
->shouldThrow(TypeError::class)
75+
->during('beforeSuite', [$event]);
76+
}
77+
78+
public function it_will_throw_if_the_directory_parameter_is_missing(SuiteEvent $event)
79+
{
80+
$this->setOptions([
81+
'whitelist' => [
82+
['foobar' => 'baz', 'suffix' => 'Spec.php', 'prefix' => 'Get'],
83+
],
84+
]);
85+
86+
$this
87+
->shouldThrow(InvalidArgumentException::class)
88+
->during('beforeSuite', [$event]);
89+
}
90+
3191
public function let(ConsoleIO $io)
3292
{
3393
$codeCoverage = new CodeCoverage(new DriverStub(), new Filter());

src/Listener/CodeCoverageListener.php

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener;
1616

17+
use InvalidArgumentException;
1718
use PhpSpec\Console\ConsoleIO;
1819
use PhpSpec\Event\ExampleEvent;
1920
use PhpSpec\Event\SuiteEvent;
2021
use SebastianBergmann\CodeCoverage\CodeCoverage;
2122
use SebastianBergmann\CodeCoverage\Report;
2223
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
24+
use TypeError;
2325

26+
use function gettype;
27+
use function is_array;
2428
use function is_string;
2529

2630
/**
@@ -148,19 +152,15 @@ public function beforeSuite(SuiteEvent $event): void
148152
$filter = $this->coverage->filter();
149153

150154
foreach ($this->options['whitelist'] as $option) {
151-
if (is_string($option)) {
152-
$option = ['directory' => $option];
153-
}
154-
$option = $option + ['suffix' => '.php', 'prefix' => ''];
155-
$filter->includeDirectory($option['directory'], $option['suffix'], $option['prefix']);
155+
$settings = $this->filterDirectoryParams($option);
156+
157+
$filter->includeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']);
156158
}
157159

158160
foreach ($this->options['blacklist'] as $option) {
159-
if (is_string($option)) {
160-
$option = ['directory' => $option];
161-
}
162-
$option = $option + ['suffix' => '.php', 'prefix' => ''];
163-
$filter->excludeDirectory($option['directory'], $option['suffix'], $option['prefix']);
161+
$settings = $this->filterDirectoryParams($option);
162+
163+
$filter->excludeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']);
164164
}
165165

166166
$filter->includeFiles($this->options['whitelist_files']);
@@ -190,4 +190,33 @@ public function setOptions(array $options): void
190190
{
191191
$this->options = $options + $this->options;
192192
}
193+
194+
/**
195+
* @param array<string, string>|string $option
196+
*
197+
* @return array{directory:string, prefix:string, suffix:string}
198+
*/
199+
protected function filterDirectoryParams($option): array
200+
{
201+
if (is_string($option)) {
202+
$option = ['directory' => $option];
203+
}
204+
205+
if (!is_array($option)) {
206+
throw new TypeError(sprintf(
207+
'Directory filtering options must be a string or an associated array, %s given instead.',
208+
gettype($option)
209+
));
210+
}
211+
212+
if (!isset($option['directory'])) {
213+
throw new InvalidArgumentException('Missing required directory path.');
214+
}
215+
216+
return [
217+
'directory' => $option['directory'],
218+
'suffix' => $option['suffix'] ?? '.php',
219+
'prefix' => $option['prefix'] ?? '',
220+
];
221+
}
193222
}

0 commit comments

Comments
 (0)