Ensure database connection information is cleaned up properly after performing ConnectionTest

Created on 29 January 2024, 6 months ago
Updated 3 March 2024, 5 months ago

Problem/Motivation

When the \Drupal\Tests\Core\Database\ConnectionTest::testFindCallerFromDebugBacktrace() Unit test is performed, the database connection info added is not cleaned up. This can cause problems when additional Unit tests are conducted using the database connection similarly, such as the tests in \Drupal\Tests\Core\DrupalKernel\DiscoverServiceProvidersTest.

Steps to reproduce

Assuming a typical Drupal setup, based on drupal/core-recommended, drupal/core-composer-scaffold and drupal/core-dev. When executing ./vendor/bin/phpunit --configuration web/core --filter 'testFindCallerFromDebugBacktrace$|testDiscoverService' web/core/tests/Drupal/Tests/Core will provide:

PHPUnit 9.6.15 by Sebastian Bergmann and contributors.

Testing /var/www/app/web/core/tests/Drupal/Tests/Core
.EE                                                                 3 / 3 (100%)

Time: 00:00.133, Memory: 70.00 MB

There were 2 errors:

1) Drupal\Tests\Core\DrupalKernel\DiscoverServiceProvidersTest::testDiscoverServiceCustom
Error: Class "Drupal\Tests\Core\Database\Stub\Connection" not found

/var/www/app/web/core/lib/Drupal/Core/Database/Database.php:464
/var/www/app/web/core/lib/Drupal/Core/Database/Database.php:195
/var/www/app/web/core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php:40
/var/www/app/web/core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php:31
/var/www/app/web/core/lib/Drupal/Core/DrupalKernel.php:1409
/var/www/app/web/core/lib/Drupal/Core/DrupalKernel.php:622
/var/www/app/web/core/tests/Drupal/Tests/Core/DrupalKernel/DiscoverServiceProvidersTest.php:31
/var/www/app/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
/var/www/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
/var/www/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
/var/www/app/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651
/var/www/app/vendor/phpunit/phpunit/src/TextUI/Command.php:144
/var/www/app/vendor/phpunit/phpunit/src/TextUI/Command.php:97
/var/www/app/vendor/bin/phpunit:122

2) Drupal\Tests\Core\DrupalKernel\DiscoverServiceProvidersTest::testDiscoverServiceNoContainerYamls
Error: Class "Drupal\Tests\Core\Database\Stub\Connection" not found

/var/www/app/web/core/lib/Drupal/Core/Database/Database.php:464
/var/www/app/web/core/lib/Drupal/Core/Database/Database.php:195
/var/www/app/web/core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php:40
/var/www/app/web/core/lib/Drupal/Core/Config/BootstrapConfigStorageFactory.php:31
/var/www/app/web/core/lib/Drupal/Core/DrupalKernel.php:1409
/var/www/app/web/core/lib/Drupal/Core/DrupalKernel.php:622
/var/www/app/web/core/tests/Drupal/Tests/Core/DrupalKernel/DiscoverServiceProvidersTest.php:52
/var/www/app/vendor/phpunit/phpunit/src/Framework/TestResult.php:728
/var/www/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
/var/www/app/vendor/phpunit/phpunit/src/Framework/TestSuite.php:684
/var/www/app/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:651
/var/www/app/vendor/phpunit/phpunit/src/TextUI/Command.php:144
/var/www/app/vendor/phpunit/phpunit/src/TextUI/Command.php:97
/var/www/app/vendor/bin/phpunit:122

ERRORS!
Tests: 3, Assertions: 1, Errors: 2.

Proposed resolution

One way to solve this is to add a tearDown() function to \Drupal\Tests\Core\Database\ConnectionTest, where the default database connection is removed.

Merge request link

https://git.drupalcode.org/project/drupal/-/merge_requests/6372

Remaining tasks

  • Get feedback from community

User interface changes

N.a.

API changes

N.a.

Data model changes

N.a.

Release notes snippet

Added tearDown() to \Drupal\Tests\Core\Database\ConnectionTest to ensure database connection information is removed properly and doesn't affect additional Unit tests.

🐛 Bug report
Status

Fixed

Version

11.0 🔥

Component
PHPUnit 

Last updated 1 minute ago

Created by

🇳🇱Netherlands Watergate

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

Merge Requests

Comments & Activities

Production build 0.69.0 2024