Skip to content

Commit

Permalink
Initial work on removing the blacklist/whitelist terminology
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Jun 7, 2020
1 parent 9855caf commit 8e9c76d
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 224 deletions.
1 change: 1 addition & 0 deletions ChangeLog-9.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ All notable changes of the PHPUnit 9.3 release series are documented in this fil
### Changed

* [#4264](https://github.com/sebastianbergmann/phpunit/pull/4264): Refactor logical operator constraints
* `PHPUnit\Util\Blacklist` is now deprecated, please use `PHPUnit\Util\ExcludeList` instead

[9.3.0]: https://github.com/sebastianbergmann/phpunit/compare/9.2...master
8 changes: 4 additions & 4 deletions src/Framework/MockObject/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class Generator
/**
* @var array
*/
private const BLACKLISTED_METHOD_NAMES = [
private const EXCLUDED_METHOD_NAMES = [
'__CLASS__' => true,
'__DIR__' => true,
'__FILE__' => true,
Expand Down Expand Up @@ -899,12 +899,12 @@ private function generateMockClassDeclaration(array $mockClassName, bool $isInte

private function canMockMethod(\ReflectionMethod $method): bool
{
return !($this->isConstructor($method) || $method->isFinal() || $method->isPrivate() || $this->isMethodNameBlacklisted($method->getName()));
return !($this->isConstructor($method) || $method->isFinal() || $method->isPrivate() || $this->isMethodNameExcluded($method->getName()));
}

private function isMethodNameBlacklisted(string $name): bool
private function isMethodNameExcluded(string $name): bool
{
return isset(self::BLACKLISTED_METHOD_NAMES[$name]);
return isset(self::EXCLUDED_METHOD_NAMES[$name]);
}

private function getTemplate(string $template): Template
Expand Down
6 changes: 3 additions & 3 deletions src/Framework/TestResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
namespace PHPUnit\Framework;

use PHPUnit\Framework\MockObject\Exception as MockObjectException;
use PHPUnit\Util\Blacklist;
use PHPUnit\Util\ErrorHandler;
use PHPUnit\Util\ExcludeList;
use PHPUnit\Util\Printer;
use PHPUnit\Util\Test as TestUtil;
use SebastianBergmann\CodeCoverage\CodeCoverage;
Expand Down Expand Up @@ -744,7 +744,7 @@ public function run(Test $test): void
$test->addToAssertionCount(Assert::getCount());

if ($monitorFunctions) {
$blacklist = new Blacklist;
$excludeList = new ExcludeList;

/** @noinspection ForgottenDebugOutputInspection */
$functions = \xdebug_get_monitored_functions();
Expand All @@ -753,7 +753,7 @@ public function run(Test $test): void
\xdebug_stop_function_monitor();

foreach ($functions as $function) {
if (!$blacklist->isBlacklisted($function['filename'])) {
if (!$excludeList->isExcluded($function['filename'])) {
$this->addFailure(
$test,
new RiskyTestError(
Expand Down
204 changes: 6 additions & 198 deletions src/Util/Blacklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,151 +9,14 @@
*/
namespace PHPUnit\Util;

use Composer\Autoload\ClassLoader;
use DeepCopy\DeepCopy;
use Doctrine\Instantiator\Instantiator;
use PharIo\Manifest\Manifest;
use PharIo\Version\Version as PharIoVersion;
use PHP_Token;
use phpDocumentor\Reflection\DocBlock;
use phpDocumentor\Reflection\Project;
use phpDocumentor\Reflection\Type;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophet;
use SebastianBergmann\CodeCoverage\CodeCoverage;
use SebastianBergmann\CodeUnit\CodeUnit;
use SebastianBergmann\CodeUnitReverseLookup\Wizard;
use SebastianBergmann\Comparator\Comparator;
use SebastianBergmann\Diff\Diff;
use SebastianBergmann\Environment\Runtime;
use SebastianBergmann\Exporter\Exporter;
use SebastianBergmann\FileIterator\Facade as FileIteratorFacade;
use SebastianBergmann\GlobalState\Snapshot;
use SebastianBergmann\Invoker\Invoker;
use SebastianBergmann\ObjectEnumerator\Enumerator;
use SebastianBergmann\RecursionContext\Context;
use SebastianBergmann\ResourceOperations\ResourceOperations;
use SebastianBergmann\Template\Template;
use SebastianBergmann\Timer\Timer;
use SebastianBergmann\Type\TypeName;
use SebastianBergmann\Version;
use TheSeer\Tokenizer\Tokenizer;
use Webmozart\Assert\Assert;

/**
* @deprecated Use ExcludeList instead
*/
final class Blacklist
{
/**
* @var array<string,int>
*/
private const BLACKLISTED_CLASS_NAMES = [
// composer
ClassLoader::class => 1,

// doctrine/instantiator
Instantiator::class => 1,

// myclabs/deepcopy
DeepCopy::class => 1,

// phar-io/manifest
Manifest::class => 1,

// phar-io/version
PharIoVersion::class => 1,

// phpdocumentor/reflection-common
Project::class => 1,

// phpdocumentor/reflection-docblock
DocBlock::class => 1,

// phpdocumentor/type-resolver
Type::class => 1,

// phpspec/prophecy
Prophet::class => 1,

// phpunit/phpunit
TestCase::class => 2,

// phpunit/php-code-coverage
CodeCoverage::class => 1,

// phpunit/php-file-iterator
FileIteratorFacade::class => 1,

// phpunit/php-invoker
Invoker::class => 1,

// phpunit/php-text-template
Template::class => 1,

// phpunit/php-timer
Timer::class => 1,

// phpunit/php-token-stream
PHP_Token::class => 1,

// sebastian/code-unit
CodeUnit::class => 1,

// sebastian/code-unit-reverse-lookup
Wizard::class => 1,

// sebastian/comparator
Comparator::class => 1,

// sebastian/diff
Diff::class => 1,

// sebastian/environment
Runtime::class => 1,

// sebastian/exporter
Exporter::class => 1,

// sebastian/global-state
Snapshot::class => 1,

// sebastian/object-enumerator
Enumerator::class => 1,

// sebastian/recursion-context
Context::class => 1,

// sebastian/resource-operations
ResourceOperations::class => 1,

// sebastian/type
TypeName::class => 1,

// sebastian/version
Version::class => 1,

// theseer/tokenizer
Tokenizer::class => 1,

// webmozart/assert
Assert::class => 1,
];

/**
* @var string[]
*/
private static $directories;

public static function addDirectory(string $directory): void
{
if (!\is_dir($directory)) {
throw new Exception(
\sprintf(
'"%s" is not a directory',
$directory
)
);
}

self::$directories[] = \realpath($directory);
ExcludeList::addDirectory($directory);
}

/**
Expand All @@ -163,69 +26,14 @@ public static function addDirectory(string $directory): void
*/
public function getBlacklistedDirectories(): array
{
$this->initialize();

return self::$directories;
return (new ExcludeList)->getExcludedDirectories();
}

/**
* @throws Exception
*/
public function isBlacklisted(string $file): bool
{
if (\defined('PHPUNIT_TESTSUITE')) {
return false;
}

$this->initialize();

foreach (self::$directories as $directory) {
if (\strpos($file, $directory) === 0) {
return true;
}
}

return false;
}

/**
* @throws Exception
*/
private function initialize(): void
{
if (self::$directories === null) {
self::$directories = [];

foreach (self::BLACKLISTED_CLASS_NAMES as $className => $parent) {
if (!\class_exists($className)) {
continue;
}

try {
$directory = (new \ReflectionClass($className))->getFileName();
// @codeCoverageIgnoreStart
} catch (\ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd

for ($i = 0; $i < $parent; $i++) {
$directory = \dirname($directory);
}

self::$directories[] = $directory;
}

// Hide process isolation workaround on Windows.
if (\DIRECTORY_SEPARATOR === '\\') {
// tempnam() prefix is limited to first 3 chars.
// @see https://php.net/manual/en/function.tempnam.php
self::$directories[] = \sys_get_temp_dir() . '\\PHP';
}
}
return (new ExcludeList)->isExcluded($file);
}
}
Loading

2 comments on commit 8e9c76d

@localheinz
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸ‘

@coquer
Copy link

@coquer coquer commented on 8e9c76d Jun 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you fail english class? it does not mean what you believe it means.

Please sign in to comment.