|
14 | 14 |
|
15 | 15 | namespace FriendsOfPhpSpec\PhpSpec\CodeCoverage\Listener; |
16 | 16 |
|
| 17 | +use FriendsOfPhpSpec\PhpSpec\CodeCoverage\Exception\ConfigurationException; |
17 | 18 | use PhpSpec\Console\ConsoleIO; |
18 | 19 | use PhpSpec\Event\ExampleEvent; |
19 | 20 | use PhpSpec\Event\SuiteEvent; |
20 | 21 | use SebastianBergmann\CodeCoverage\CodeCoverage; |
21 | 22 | use SebastianBergmann\CodeCoverage\Report; |
22 | 23 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
23 | 24 |
|
| 25 | +use function gettype; |
| 26 | +use function is_array; |
| 27 | +use function is_string; |
| 28 | + |
24 | 29 | /** |
25 | 30 | * @author Henrik Bjornskov |
26 | 31 | */ |
@@ -146,11 +151,15 @@ public function beforeSuite(SuiteEvent $event): void |
146 | 151 | $filter = $this->coverage->filter(); |
147 | 152 |
|
148 | 153 | foreach ($this->options['whitelist'] as $option) { |
149 | | - $filter->includeDirectory($option); |
| 154 | + $settings = $this->filterDirectoryParams($option); |
| 155 | + |
| 156 | + $filter->includeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']); |
150 | 157 | } |
151 | 158 |
|
152 | 159 | foreach ($this->options['blacklist'] as $option) { |
153 | | - $filter->excludeDirectory($option); |
| 160 | + $settings = $this->filterDirectoryParams($option); |
| 161 | + |
| 162 | + $filter->excludeDirectory($settings['directory'], $settings['suffix'], $settings['prefix']); |
154 | 163 | } |
155 | 164 |
|
156 | 165 | $filter->includeFiles($this->options['whitelist_files']); |
@@ -180,4 +189,33 @@ public function setOptions(array $options): void |
180 | 189 | { |
181 | 190 | $this->options = $options + $this->options; |
182 | 191 | } |
| 192 | + |
| 193 | + /** |
| 194 | + * @param array<string, string>|string $option |
| 195 | + * |
| 196 | + * @return array{directory:string, prefix:string, suffix:string} |
| 197 | + */ |
| 198 | + protected function filterDirectoryParams($option): array |
| 199 | + { |
| 200 | + if (is_string($option)) { |
| 201 | + $option = ['directory' => $option]; |
| 202 | + } |
| 203 | + |
| 204 | + if (!is_array($option)) { |
| 205 | + throw new ConfigurationException(sprintf( |
| 206 | + 'Directory filtering options must be a string or an associated array, %s given instead.', |
| 207 | + gettype($option) |
| 208 | + )); |
| 209 | + } |
| 210 | + |
| 211 | + if (!isset($option['directory'])) { |
| 212 | + throw new ConfigurationException('Missing required directory path.'); |
| 213 | + } |
| 214 | + |
| 215 | + return [ |
| 216 | + 'directory' => $option['directory'], |
| 217 | + 'suffix' => $option['suffix'] ?? '.php', |
| 218 | + 'prefix' => $option['prefix'] ?? '', |
| 219 | + ]; |
| 220 | + } |
183 | 221 | } |
0 commit comments