PHPUnit functional tests cannot connect to MySQL database with a specified unix_socket

Created on 15 September 2025, 21 days ago

Problem/Motivation

When using a non-standard Unix socket for a database connection, PHPUnit functional tests fail, unable to reach the database.

The Drupal Flake β†’ project creates a local development environment, using a socket in a subdirectory of the project. This works fine for both php-fpm connections, and CLI connections (for drush) -- however, PHPUnit does not preserve any unix_socket parameters set in the db connection string, and so tests that need to bootstrap Drupal into a database fail in the setup phase.

This occurs because:

1. The parent process correctly parses SIMPLETEST_DB containing unix_socket parameter (e.g., mysql://user@host/db?unix_socket=/path/to/socket)

2. PHPUnit's PhpUnitTestRunner calls Database::getConnectionInfoAsUrl() to create SIMPLETEST_DB for child processes

3. Database::getConnectionInfoAsUrl() calls Connection::createUrlFromConnectionOptions() which ignores the unix_socket parameter

4. Child processes receive incomplete database URLs without socket information, causing connection failures

This affects:

- Functional tests in containerized environments (Docker, Nix, etc.)
- Development environments using Unix sockets instead of TCP connections
- CI/CD pipelines with custom MySQL configurations

PHPUnit currently uses the pdo_mysql.default_socket value in the running php, overriding any unix_socket specified in any environment variable, phpunit.xml, DSN.

Steps to reproduce

With nix installed you can set up a local dev Drupal environment:

1. nix flake init -t git+https://git.drupalcode.org/project/drupal_flake
2. nix develop
3. start-detached
4. wait until pc-status shows the servers are running, usually 10 - 30 seconds after everything is built
5. phpunit-setup (custom wrapper that creates phpunit.xml, simpletest directories)
6. phpunit path/to/any/functional/test

Result: tests run, but any database interaction fails to connect.

Proposed resolution

Modify Drupal\Core\Database\Connection::createUrlFromConnectionOptions() to preserve the unix_socket parameter (and other query parameters) when reconstructing database URLs.

Changes needed:

1. Build query parameters array from connection options
2. Include unix_socket parameter if present in connection options
3. Use http_build_query() to properly encode parameters in the URL

This ensures that child processes receive complete database connection information including Unix socket paths.

Remaining tasks

- Create patch for Connection::createUrlFromConnectionOptions()
- Add unit test to verify unix_socket parameter preservation
- Add unit test to verify other query parameters are preserved
- Review for other database drivers (PostgreSQL, SQLite) that might need similar fixes
- Update documentation if needed

User interface changes

None.

Introduced terminology

None.

API changes

The change is backward compatible. The createUrlFromConnectionOptions() method signature remains unchanged, but the returned URL will now include query parameters that were previously omitted.

Before:

  $url = Connection::createUrlFromConnectionOptions([
    'driver' => 'mysql',
    'host' => 'localhost',
    'database' => 'test',
    'unix_socket' => '/tmp/mysql.sock',
    'module' => 'mysql'
  ]);
  // Returns: mysql://localhost/test?module=mysql

After:

  // Returns: mysql://localhost/test?module=mysql&unix_socket=%2Ftmp%2Fmysql.sock

Data model changes

None.

Release notes snippet

Fixed database connection URL reconstruction for child processes: Database connection URLs created for child processes (such as during functional tests) now properly preserve connection parameters like unix_socket. This fixes functional test failures in environments using Unix socket database connections instead of TCP.

πŸ› Bug report
Status

Active

Version

11.2 πŸ”₯

Component

database system

Created by

πŸ‡ΊπŸ‡ΈUnited States freelock Seattle

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.

No activities found.

Production build 0.71.5 2024