From b31f3776128c0f2d6e21ec2577b149532df87047 Mon Sep 17 00:00:00 2001 From: Otto Rask Date: Tue, 27 Aug 2019 15:32:56 +0300 Subject: [PATCH] Add support for multiple whitelisted directories in CLI args Previously inserting multiple `--whitelist` args would result in the last one being the only effective one. New implementation aggregates them and adds them to the code coverage filtering one by one, allowing developers to use multiple directories in whitelisting without relying on the XML configuration file. This introduces no BC breaks, as the old behavior works in the same way as always from the end user's perspective, internally an array with a single item is used instead. --- src/TextUI/Command.php | 6 ++- src/TextUI/TestRunner.php | 10 +++- .../phpt-for-multiwhitelist-coverage.phpt | 10 ++++ .../code-coverage-multiple-whitelist.phpt | 46 +++++++++++++++++++ 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 tests/_files/phpt-for-multiwhitelist-coverage.phpt create mode 100644 tests/end-to-end/code-coverage-multiple-whitelist.phpt diff --git a/src/TextUI/Command.php b/src/TextUI/Command.php index 4b7a788348a..d91c8f893c4 100644 --- a/src/TextUI/Command.php +++ b/src/TextUI/Command.php @@ -724,7 +724,11 @@ protected function handleArguments(array $argv): void break; case '--whitelist': - $this->arguments['whitelist'] = $option[1]; + if (!isset($this->arguments['whitelist'])) { + $this->arguments['whitelist'] = []; + } + + $this->arguments['whitelist'][] = $option[1]; break; diff --git a/src/TextUI/TestRunner.php b/src/TextUI/TestRunner.php index 8372d2c23e3..6fabf739b9a 100644 --- a/src/TextUI/TestRunner.php +++ b/src/TextUI/TestRunner.php @@ -457,7 +457,15 @@ public function doRun(Test $suite, array $arguments = [], bool $exit = true): Te $whitelistFromOption = false; if (isset($arguments['whitelist'])) { - $this->codeCoverageFilter->addDirectoryToWhitelist($arguments['whitelist']); + if (!\is_array($arguments['whitelist'])) { + $whitelistDirectories = [$arguments['whitelist']]; + } else { + $whitelistDirectories = $arguments['whitelist']; + } + + foreach ($whitelistDirectories as $whitelistDirectory) { + $this->codeCoverageFilter->addDirectoryToWhitelist($whitelistDirectory); + } $whitelistFromOption = true; } diff --git a/tests/_files/phpt-for-multiwhitelist-coverage.phpt b/tests/_files/phpt-for-multiwhitelist-coverage.phpt new file mode 100644 index 00000000000..7dff83ea98f --- /dev/null +++ b/tests/_files/phpt-for-multiwhitelist-coverage.phpt @@ -0,0 +1,10 @@ +--TEST-- +PHPT for testing coverage using multiple whitespace arguments +--FILE-- +publicMethod(); +$anotherCoveredClass = new SampleClass(1, 2, 'a'); +$testing = $anotherCoveredClass->a; +--EXPECT-- diff --git a/tests/end-to-end/code-coverage-multiple-whitelist.phpt b/tests/end-to-end/code-coverage-multiple-whitelist.phpt new file mode 100644 index 00000000000..6d8b58742f5 --- /dev/null +++ b/tests/end-to-end/code-coverage-multiple-whitelist.phpt @@ -0,0 +1,46 @@ +--TEST-- +phpunit --colors=never --coverage-text=php://stdout ../../_files/phpt-for-coverage.phpt --whitelist ../../_files/CoveredClass.php +--SKIPIF-- +