0

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:

enter image description here

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:

enter image description here

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

enter image description here

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
0

1 Answer 1

4

Even though my answer is a bit late, but just like me if anyone has busted his head with this problem, here is what worked for me:

Apparently you need to specify to phpstorm the network mode he should use for interacting with the container. Go to File > Settings > PHP and click on the folder icon of the "Docker Container" config. in the Network mode instead of "Bridge"use the network name you used in your docker-compose.yml;

#Docker Networks
networks:
   app-network:
      driver: bridge

To get the name of the network correctly use docker network ls

Additionnaly you can add links of the containers.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Very userful tip thanks a lot, it saves my day. I am using laradock, when I execute : docker network ls It gives me one laradock-backend and I used as a network name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.