PHPUnittests via docker by PHPStorm Configuration

Hey guys, 

I'm trying to run my PHPUnit tests in PHPStorm. Currently, I run them by executing a Docker container via the command line and accessing a test database in another Docker container. This approach works. However, when I try to run the tests using a PHPStorm configuration, it can't connect to the database:


Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo for database failed: Name does not resolve (Connection: mysql, SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

Any advices what could be wrong, thanks in advance. 

I also tried to connect via:

    <env name="DB_HOST" value="127.0.0.1/localhost"/>
    <env name="DB_PORT" value="40001/3306"/>

The Errormessage is varies a bit than:

Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

My Setup:

CLI Interpreter

Test Framework

Run Configuration

docker-compose.yaml
services:
  app:
    image: 'php-fpm-laravel:8.2-local'
    depends_on:
      - database
    environment:
      APP_DEBUG: 'true'
      APP_URL: 'http://localhost:40000'
      ASSET_URL: 'http://localhost:40000'
    working_dir: /var/www
    volumes:
      - ../../:/var/www
  web:
    image: 'nginx-laravel:master'
    pull_policy: always
    restart: always
    ports:
      - "40000:80"
    volumes:
      - ../../:/var/www
  database:
    image: mariadb:10.6-focal
    pull_policy: always
    restart: always
    ports:
      - "40001:3306"
    environment:
      MYSQL_ROOT_PASSWORD: 'password'
      MYSQL_DATABASE: 'app'
    volumes:
      - crm-core:/var/lib/mysql
phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
        bootstrap="../vendor/autoload.php"
        colors="true"
        defaultTestSuite="unit_and_integration"
        cacheDirectory=".phpunit.cache">
  <testsuites>
    <testsuite name="Unit">
      <directory suffix="Test.php">Unit</directory>
      <exclude>Unit/Services/External</exclude>
    </testsuite>
    <testsuite name="Unit_External">
      <directory suffix="Test.php">Unit/Services/External</directory>
    </testsuite>
    <testsuite name="Integration">
      <directory suffix="Test.php">Integration</directory>
    </testsuite>
    <testsuite name="unit_and_integration">
      <directory suffix="Test.php">Unit</directory>
      <directory suffix="Test.php">Integration</directory>
      <exclude>Unit/Services/External</exclude>
    </testsuite>
    <testsuite name="all">
      <directory suffix="Test.php">Unit</directory>
      <directory suffix="Test.php">Integration</directory>
    </testsuite>
  </testsuites>
  <coverage>
    <include>
      <directory suffix=".php">../app</directory>
    </include>
    <exclude>
      <directory suffix=".php">../app/Providers</directory>
    </exclude>
  </coverage>
  <php>
    <env name="APP_ENV" value="testing"/>
    <env name="DB_HOST" value="database"/>
    <env name="DB_DATABASE" value="app_test"/>
    <env name="DB_USERNAME" value="root"/>
    <env name="DB_PASSWORD" value="password"/>
    <env name="DB_PORT" value="3306"/>
    <env name="REDIS_PASSWORD" value="" />
    <env name="REDIS_HOST" value="redis"/>
  </php>
</phpunit>
0

The issue here is that you are trying to run the tests with a Docker remote interpreter, while your Docker environment is deployed via Docker Compose.

With a Docker Compose interpreter, PhpStorm just starts one more container from the image, and this container isn't connected with the Docker Compose services in any way.

Please use a Docker Compose interpreter instead, in the ‘exec’ mode.

2

Thanks! It works for me.

0

请先登录再写评论。