4

I am trying to find out why my PHPUnit tests fail with the following messages:

PHP Warning:  Class 'PHPUnit\Framework\MockObject\Matcher\InvokedRecorder' not found in /var/www/html/core/tests/bootstrap.php on line 197

Warning: Class 'PHPUnit\Framework\MockObject\Matcher\InvokedRecorder' not found in /var/www/html/core/tests/bootstrap.php on line 197
PHP Warning:  Class 'Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\HtmlOutputPrinter' not found in /var/www/html/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter.php on line 15

Warning: Class 'Drupal\TestTools\PhpUnitCompatibility\PhpUnit8\HtmlOutputPrinter' not found in /var/www/html/core/tests/Drupal/Tests/Listeners/HtmlOutputPrinter.php on line 15
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

Could not use "\Drupal\Tests\Listeners\HtmlOutputPrinter" as printer: class does not exist

I executed ../vendor/bin/phpunit ../modules/custom/drupalup_fibo_test/ from inside the core directory and the errors above came pouring out even though I followed this tutorial and used the tutors finished code available from github.

I also get the same messages with my own custom module.

The last line of the output complaining about HtmlOutputPrinter can be silenced by commenting out a relevant line in core/phpunit.xml.

My research led me to this issue log and other similar issues where PHPUnit 6 and PHP compatibility issues seemed to be the problem. My Drupal site is Docker powered, using PHPUnit 8.5.2 and running PHP 7.3.13.

halfer
  • 19,824
  • 17
  • 99
  • 186
sisko
  • 9,604
  • 20
  • 67
  • 139

2 Answers2

2

Update 11/2020: Drupal 9 is compatible with PhpUnit 8.
-> I suggest upgrading to Drupal 9.

Old answer below:

Drupal 8.8 does not support PhpUnit 8 - yet.

PHPUnit 7 will be end of support Feb 7, 2020

https://www.drupal.org/project/drupal/issues/3063887

However, if you take a look into the location from stacktrace core/tests/bootstrap.php you will find:

// PHPUnit 4 to PHPUnit 6 bridge. Tests written for PHPUnit 4 need to work on
// PHPUnit 6 with a minimum of fuss.
// @todo provided for BC; remove in Drupal 9.
class_alias(AssertionFailedError::class, '\PHPUnit_Framework_AssertionFailedError');
class_alias(Count::class, '\PHPUnit_Framework_Constraint_Count');
class_alias(Error::class, '\PHPUnit_Framework_Error');
class_alias(Warning::class, '\PHPUnit_Framework_Error_Warning');
class_alias(ExpectationFailedException::class, '\PHPUnit_Framework_ExpectationFailedException');
class_alias(Exception::class, '\PHPUnit_Framework_Exception');
class_alias(InvokedRecorder::class, '\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder');
class_alias(SkippedTestError::class, '\PHPUnit_Framework_SkippedTestError');
class_alias(TestCase::class, '\PHPUnit_Framework_TestCase');
class_alias(Test::class, '\PHPUnit_Util_Test');
class_alias(Xml::class, '\PHPUnit_Util_XML');

So you could simply remove the line InvokedRecorder manually or apply one of the patches from issue #3063887 (see above).

For now, I created a minimal patch just to remove this one reference...

diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php
index 467e6af6e6..ca3b579b6c 100644
--- a/core/tests/bootstrap.php
+++ b/core/tests/bootstrap.php
@@ -14,7 +14,6 @@
 use PHPUnit\Framework\Error\Warning;
 use PHPUnit\Framework\ExpectationFailedException;
 use PHPUnit\Framework\Exception;
-use PHPUnit\Framework\MockObject\Matcher\InvokedRecorder;
 use PHPUnit\Framework\SkippedTestError;
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Util\Test;
@@ -194,7 +193,6 @@ class_alias(Error::class, '\PHPUnit_Framework_Error');
 class_alias(Warning::class, '\PHPUnit_Framework_Error_Warning');
 class_alias(ExpectationFailedException::class, '\PHPUnit_Framework_ExpectationFailedException');
 class_alias(Exception::class, '\PHPUnit_Framework_Exception');
-class_alias(InvokedRecorder::class, '\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder');
 class_alias(SkippedTestError::class, '\PHPUnit_Framework_SkippedTestError');
 class_alias(TestCase::class, '\PHPUnit_Framework_TestCase');
 class_alias(Test::class, '\PHPUnit_Util_Test');

...which can be added to composer.json:

{
  "patches": {
    "drupal/core": {
      "Remove InvokedRecorder for PhpUnit 8": "src/patches/3063887-InvokedRecorder.patch"
    }
  }
}
maosmurf
  • 1,858
  • 17
  • 16
0

If you are usind Drupal 9 try

composer require phpunit/phpunit:^8.0 --with-all-dependencies
Marcelo Vani
  • 56
  • 1
  • 2