Problem/Motivation
Errors occur when using the latest stable version of PHPUnit 10 (which requires PHP 8.1) on Drupal 10.0.x. They are detailed below in the code comments.
Steps to reproduce
Install by using composer require --dev : drupal/core-dev, phpunit/phpunit, phpspec/prophecy-phpunit, symfony/phpunit-bridge, behat/mink
Remaining tasks
I was able to bypass the encountered errors by modifying 2 files :
- web/core/tests/Drupal/Tests/PhpUnitCompatibilityTrait.php
/*
* Warning: Class "Drupal\TestTools\PhpUnitCompatibility\PhpUnit10\TestCompatibilityTrait" not found in /var/www/html/web/core/tests/Drupal/Tests/PhpUnitCompatibilityTrait.php on line 10
* Fatal error: Trait "Drupal\Tests\PhpUnitCompatibilityTrait" not found in /var/www/html/web/core/tests/Drupal/Tests/UnitTestCase.php on line 27
*/
//class_alias("Drupal\TestTools\PhpUnitCompatibility\PhpUnit" . RunnerVersion::getMajor() . "\TestCompatibilityTrait", 'Drupal\Tests\PhpUnitCompatibilityTrait');
class_alias("Drupal\TestTools\PhpUnitCompatibility\PhpUnit9\TestCompatibilityTrait", 'Drupal\Tests\PhpUnitCompatibilityTrait');
- web/core/tests/Drupal/Tests/UnitTestCase.php
/*
* Fatal error: Trait "Prophecy\PhpUnit\ProphecyTrait" not found in /var/www/html/web/core/tests/Drupal/Tests/UnitTestCase.php on line 27
*/
//use Prophecy\PhpUnit\ProphecyTrait;
...
//use ProphecyTrait;
This just allows you to run unit tests without errors, but it requires work to make the versions compatible.
Comments
Comment #2
longwavePHPUnit 10 has a large number of changes and is not yet supported - Symfony does not yet work with it either, so we need to wait for their PHPUnit bridge to catch up first as well.
We don't even support PHPUnit 9.6 yet - please see #3338907: Update to PHPUnit 9.6.7, closing this as a duplicate of that issue.
Comment #3
benjbmc commentedThank you for your quick feedback @longwave, I downgraded PHPUnit to version 9.5.28 and it works fine.
There was just this error for some contrib modules :
...Test::setUp() must be compatible with Drupal\KernelTests\KernelTestBase::setUp(): voidTo be fixed in the respective test files by adding the void return type declaration.
Comment #4
gábor hojtsyThis comes up in google results ahead of the current issue at #3217904: [meta] Support PHPUnit 10 in Drupal 11 so cross-linking.
Comment #5
hiramanpatil@benJBmC,
I am using Drupal 10.1.7 version with below PHPUnit configuration.
"require-dev": {
"phpspec/prophecy": "^1.17",
"phpunit/phpunit": "9.5.28",
"symfony/phpunit-bridge": "^7.0"
}
I am trying to run the dev module test but getting errors.
Command - vendor/bin/phpunit web/modules/contrib/devel/tests/src/Functional/DevelControllerTest.php
Output
PHPUnit 9.5.28 by Sebastian Bergmann and contributors.
Testing Drupal\Tests\devel\Functional\DevelControllerTest
E 1 / 1 (100%)
Time: 00:48.149, Memory: 10.00 MB
There was 1 error:
1) Drupal\Tests\devel\Functional\DevelControllerTest::testRouteGeneration
Error: Class "Behat\Mink\Driver\BrowserKitDriver" not found
\web\core\tests\Drupal\Tests\BrowserTestBase.php:310
\web\core\tests\Drupal\Tests\BrowserTestBase.php:222
\web\core\tests\Drupal\Tests\BrowserTestBase.php:370
\web\modules\contrib\devel\tests\src\Functional\DevelBrowserTestBase.php:43
\web\modules\contrib\devel\tests\src\Functional\DevelControllerTest.php:57
\vendor\phpunit\phpunit\src\Framework\TestResult.php:728
ERRORS!
Tests: 1, Assertions: 1, Errors: 1.
Remaining self deprecation notices (1)
1x: The Drupal\Tests\devel\Functional\DevelControllerTest::$modules property must be declared protected. See https://www.drupal.org/node/2909426
1x in DrupalListener::startTest from Drupal\Tests\Listeners