Skip to content

Commit

Permalink
Simplify the implementation of #3272
Browse files Browse the repository at this point in the history
XDebugFilterScriptGenerator now completely ignores the <exclude>
part of the filter configuration and always creates the script
based on the whitelist only. Letting PHPUnit filter out the
excluded paths proved to be much faster than providing a long
list of individual files to xdebug_set_filter().
  • Loading branch information
sebastianheuer authored and sebastianbergmann committed Sep 8, 2018
1 parent ea28bb9 commit e8937fb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 78 deletions.
5 changes: 1 addition & 4 deletions src/TextUI/TestRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,10 +544,7 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te

if (isset($arguments['xdebugFilterFile'], $filterConfiguration)) {
$filterScriptGenerator = new XDebugFilterScriptGenerator();
$script = $filterScriptGenerator->generate(
$filterConfiguration['whitelist'],
$this->codeCoverageFilter->getWhitelist()
);
$script = $filterScriptGenerator->generate($filterConfiguration['whitelist']);
\file_put_contents($arguments['xdebugFilterFile'], $script);

$this->write("\n");
Expand Down
30 changes: 3 additions & 27 deletions src/Util/XDebugFilterScriptGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

class XDebugFilterScriptGenerator
{
public function generate(array $filterData, array $whitelistedFiles): string
public function generate(array $filterData): string
{
$items = $this->getItems($filterData, $whitelistedFiles);
$items = $this->getWhitelistItems($filterData);

$files = \array_map(function ($item) {
return \sprintf(" '%s'", $item);
Expand All @@ -37,16 +37,7 @@ public function generate(array $filterData, array $whitelistedFiles): string
EOF;
}

private function getItems(array $filterData, array $whitelistedFiles): array
{
if ($this->canUseRawFilterData($filterData)) {
return $this->getItemsFromRawFilterData($filterData);
}

return $whitelistedFiles;
}

private function getItemsFromRawFilterData(array $filterData): array
private function getWhitelistItems(array $filterData): array
{
$files = [];

Expand All @@ -64,19 +55,4 @@ private function getItemsFromRawFilterData(array $filterData): array

return $files;
}

private function canUseRawFilterData(array $filterData): bool
{
if (\count($filterData['exclude']['directory']) > 0 || \count($filterData['exclude']['file'])) {
return false;
}

foreach ($filterData['include']['directory'] as $directory) {
if (($directory['suffix'] !== '' && $directory['suffix'] !== '.php') || $directory['prefix'] !== '') {
return false;
}
}

return true;
}
}
3 changes: 3 additions & 0 deletions tests/_files/configuration_whitelist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<filter>
<whitelist>
<file>AbstractTest.php</file>
<exclude>
<file>Foo.php</file>
</exclude>
</whitelist>
</filter>

Expand Down
65 changes: 18 additions & 47 deletions tests/unit/Util/XDebugFilterScriptGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,61 +15,32 @@ class XDebugFilterScriptGeneratorTest extends TestCase
{
/**
* @covers \PHPUnit\Util\XDebugFilterScriptGenerator::generate
*
* @dataProvider scriptGeneratorTestDataProvider
*/
public function testReturnsExpectedScript(array $filterConfiguration, array $resolvedWhitelist): void
public function testReturnsExpectedScript(): void
{
$writer = new XDebugFilterScriptGenerator();
$actual = $writer->generate($filterConfiguration, $resolvedWhitelist);

$this->assertStringEqualsFile(__DIR__ . '/_files/expectedXDebugFilterScript.txt', $actual);
}

public function scriptGeneratorTestDataProvider(): array
{
return [
[
[
'include' => [
'directory' => [
[
'path' => 'src/somePath',
'suffix' => '.php',
'prefix' => '',
],
],
'file' => [
'src/foo.php',
'src/bar.php',
],
],
'exclude' => [
'directory' => [],
'file' => [],
],
],
[],
__DIR__ . '/_files/expectedXDebugFilterScript.php',
],
[
[
'include' => [
'directory' => ['src/'],
'file' => ['src/foo.php'],
],
'exclude' => [
'directory' => [],
'file' => ['src/baz.php'],
$filterConfiguration = [
'include' => [
'directory' => [
[
'path' => 'src/somePath',
'suffix' => '.php',
'prefix' => '',
],
],
[
'src/somePath',
'file' => [
'src/foo.php',
'src/bar.php',
],
__DIR__ . '/_files/expectedXDebugFilterScript.php',
],
'exclude' => [
'directory' => [],
'file' => [],
],
];

$writer = new XDebugFilterScriptGenerator();
$actual = $writer->generate($filterConfiguration);

$this->assertStringEqualsFile(__DIR__ . '/_files/expectedXDebugFilterScript.txt', $actual);
}
}

0 comments on commit e8937fb

Please sign in to comment.