PHP Startup: Unable to load dynamic library, when running unit tests
You are staring at a wall of PHP Startup: Unable to load dynamic library warnings in your XAMPP environment on Windows 10. The stack traces point specifically to missing DLLs like bz2, curl, fileinfo, pdo_mysql, and php_openssl.dll. While this error often appears during standard web requests, seeing it flood your PHPUnit output while running unit tests is particularly frustrating because the logic of your application code isn't actually executing; the interpreter fails before it even gets there.
This specific cluster of errors usually stems from a mismatch between how XAMPP configures its php.ini and how the Windows loader resolves dynamic libraries. The error message explicitly states that PHP tried to load \xampp\php\ext\bz2 or \xampp\php\ext\php_bz2.dll but failed. This is a classic case of incorrect library naming conventions colliding with Windows' strict file system requirements.
The Core Issue: Naming Conventions and Paths
In the PHP source code, extensions are often compiled without the php_ prefix (e.g., bz2.dll). However, many third-party packages or older versions of XAMPP expect the library to be named with that prefix (e.g., php_bz2.dll). Windows is unforgiving here; it does not perform automatic name mangling like some Unix systems might.
When you see the error The specified module could not be found, it means the .dll file exists in the directory, but the filename PHP is looking for doesn't match what is on disk. This is frequently caused by editing an older version of php.ini that lists extensions using a naming convention incompatible with your current XAMPP installation.
For example, if your php.ini contains:
extension=bz2
And your disk has the file php_bz2.dll, PHP will fail to load it because it expects a file literally named bz2.dll. Conversely, if you have a legacy DLL named php_bz2.dll but your config says extension=php_bz2, you might be in luck, but mixing these conventions across multiple extensions leads to the "missing module" cascade you are seeing.
The Solution: Renaming and Re-enabling
The most reliable fix involves two steps: correcting the physical filenames of the DLLs and updating the configuration directives.
First, navigate to your XAMPP installation directory (typically C:\xampp\php). Look at the files listed in your error logs. You will likely find files like php_openssl.dll or php_fileinfo.dll. If your php.ini expects them without the prefix, you need to rename them.
Use PowerShell or Command Prompt to ensure atomicity and avoid locking issues:
cd "C:\xampp\php"
Rename-Item php_bz2.dll bz2.dll -WhatIf
# Remove -WhatIf after verifying
Repeat this for every extension reported in the warning log. Once the filenames on disk match the name specified in your php.ini (e.g., extension=bz2), the loader should succeed.
Second, verify that you are not enabling extensions twice. Some XAMPP configurations might list extension=php_bz2.dll in one section and extension=bz2 in another if a user manually added lines without checking what was already present. Ensure your php.ini contains exactly one line per active extension.
Configuration Order Matters
If you are running unit tests via PHPUnit from PHPStorm, ensure that the PHP interpreter selected is indeed the one pointing to your XAMPP installation (C:\xampp\php\php.exe). Sometimes, IDEs default to a bundled PHP version or a different path (like C:\ProgramData\chocolatey\bin\php.exe) which might have different extension sets.
Additionally, check if you are running the tests with the -c flag pointing to a custom php.ini. If you have separate configs for development and testing, ensure the testing config isn't inheriting incompatible settings from a production environment or vice versa.
Why This Happens Specifically in Unit Tests
Unit tests often run in isolation without the context of a running web server (Apache/Nginx). While Apache might have started successfully because it loaded extensions at a different stage or via .htaccess overrides that masked the error, PHPUnit starts PHP directly from the CLI. The CLI loader is strict and halts execution immediately upon failing to load a startup extension. This is why you see these errors exclusively during test runs but not necessarily when browsing your local site.
Fixing the naming convention and ensuring the php.ini matches the actual files on disk will clear these warnings, allowing PHPUnit to proceed to actually execute your test cases.