I have set up of PHP/MySQL/Apache/Laravel stack through the Docker without problems, everything up and running. I have also set up Docker in PhpStorm: everything works good.
Now I'm trying to set up PHPUnit in PhpStorm which I never set up before especially with the Docker container so error could be very simple I just can't understand what is wrong and where :(
When I run any test (which is working well for sure) I'm receiving this kind of errors:
It looks like something is wrong with the DB connection settings but I can't understand what is wrong because the only change from what main application uses is database name and database with this name exists for sure. Both databases uses same host, username, password, port etc and user root have permissions to do whatever he wants:
Here is how I set up PHPUnit in PhpStorm. As you can see the path to the phpunit.xml is set up and everything look good
Here is .env
DB_CONNECTION=mysql
DB_HOST=xcard_db
DB_PORT=3306
DB_DATABASE=xcard
DB_TEST_DATABASE=xcard_test
DB_USERNAME=root
DB_PASSWORD=jackjack
This is Laravel's config/database.php, as you can see I use DB_TEST_DATABASE and this is the only difference (I have cleaned some unnecessary settings, errors are the same with or without them):
'xcard_test' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_TEST_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
],
'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
'options' => extension_loaded('pdo_mysql') ? array_filter([
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
]) : [],
],
Here is my phpunit.xml in which I use DB_CONNECTION="xcard_test". Probably I'm using wrong variable in this xml file and should be used something else than DB_CONNECTION ? or maybe I didn't set up something somewhere?
Perhaps PHPUnit doesn't know what is xcard_test and where to find this connection settings?
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="xcard_test"/>
<server name="LOG_CHANNEL" value="test"/>
</php>
</phpunit>
Finally I have run these 2 commands (it is not production environment but just in case)
artisan config:clear
artisan config:cache --env=xcard_test



