Phpunit: Support adding directories to blacklist (of files that PHPUnit filters from stacktraces)

Created on 22 Apr 2020  路  4Comments  路  Source: sebastianbergmann/phpunit

| Q | A
| --------------------| ---------------
| PHPUnit version | 9.1.2
| PHP version | 7.3+
| Installation Method | Composer

Summary

As reported in https://github.com/symfony/symfony/issues/36499, 2b4d7b8bb3fd052095e64999990cc36b63aa0771 broke the Symfony PHPUnit bridge. The bridge was relying on the Blacklist class, which is internal, so it would be expected. The issue here is that there isn't any other way to do the same operation, adding new classes to the blacklist to exclude them from error stack traces.

Is it possible to have any kind of extension point to obtain the same result without relying on an internal class?

Current behavior

Crash like this: https://github.com/symfony/symfony/issues/36499#issuecomment-616365995

Error: Access to undeclared static property: PHPUnit\Util\Blacklist::$blacklistedClassNames

How to reproduce

  • Use PHPUnit 9.1.2 with the PHPUnit bridge
  • run any test
  • get a

Expected behavior

No issues

typenhancement

Most helpful comment

Would this help?

diff --git a/src/Util/Blacklist.php b/src/Util/Blacklist.php
index 8767a5d18..fcb71243f 100644
--- a/src/Util/Blacklist.php
+++ b/src/Util/Blacklist.php
@@ -145,6 +145,20 @@ final class Blacklist
      */
     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);
+    }
+

All 4 comments

This is not a bug.

Would this help?

diff --git a/src/Util/Blacklist.php b/src/Util/Blacklist.php
index 8767a5d18..fcb71243f 100644
--- a/src/Util/Blacklist.php
+++ b/src/Util/Blacklist.php
@@ -145,6 +145,20 @@ final class Blacklist
      */
     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);
+    }
+

Surely! @nicolas-grekas can you leverage that for your use case?

@sebastianbergmann yes, that would help, since we basically need a way to add to this list. Thanks for considering!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dciancu picture dciancu  路  3Comments

joubertredrat picture joubertredrat  路  4Comments

kunjalpopat picture kunjalpopat  路  4Comments

stephen-leavitt-sonyatv-com picture stephen-leavitt-sonyatv-com  路  4Comments

edyan picture edyan  路  4Comments