Ensure correct signature for setUp(), tearDown(), setUpBeforeClass(), and assertPostConditions() methods

Created on 12 November 2020, over 3 years ago
Updated 2 February 2023, over 1 year ago

Problem/Motivation

According to the change record Updated to PHPUnit 9 :

Drupal 9.1.x has updated to PHPUnit 9. Installs on PHP 7.3 will continue to use PHPUnit 8.4 for compatibility reasons.

As of PHPUnit 8, \PHPUnit\Framework\TestCase added a void return type hint to the following methods:

  • protected function setUp(): void
  • protected function tearDown(): void
  • public static function setUpBeforeClass(): void
  • protected function assertPostConditions(): void
  • protected function assertPreConditions(): void (not currently used by core Drupal)
  • public static function tearDownAfterClass(): void (not currently used by core Drupal)
  • protected function onNotSuccessfulTest(): void (not currently used by core Drupal)

See https://phpunit.de/announcements/phpunit-8.html

This issue is to correct the signature used for these methods in core, for all classes that extend \PHPUnit\Framework\TestCase, which includes classes that extend \Drupal\Tests\UnitTestCase, \Drupal\KernelTests\KernelTestBase, and \Drupal\Tests\BrowserTestBase.

Specifically, this issue is to ensure that the void return type hint is used and that the protected visibility is declared properly. The protected visibility mainly affects core's setUp() and tearDown() methods because there are still a large number of cases where public has been improperly used.

User interface changes

None.

API changes

The void return type hint technically constitutes an API change, but the protected visibility does not - setUp() and tearDown() have been declared as protected in core Drupal since early in Drupal 7. Fixing visibility for these methods is just adhering to our existing API.

Data model changes

None.

Original issue summary:

On Drupal 9.0.7, I tried to run Upgrade Rector, and got the following fatal error:

PHP Fatal error: Declaration of Drupal\Tests\BrowserTestBase::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void in /Users/dan/coding/drupal-ch/web/core/tests/Drupal/Tests/BrowserTestBase.php on line 388

I see that we applied fixes in #3114640: Declaration of Drupal\Tests\UnitTestCase::setUp() must be compatible with PHPUnit\Framework\TestCase::setUp(): void and 🐛 PhpUnit 8 tests breaking because of compatibility issue with setUp() Fixed but it looks like we missed this file and many others.

When I do a search for setUp() { I get 86 results in 86 files, do we want to patch all of them in this one issue?

The fix is to simply add the return type of void:

- protected function setUp() {
+ protected function setUp(): void {
🐛 Bug report
Status

Fixed

Version

10.1

Component
PHPUnit 

Last updated about 3 hours ago

Created by

🇨🇭Switzerland dan2k3k4 Zurich

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024