skip to Main Content

I’ve got a Dockerized Laravel app, here’s the docker compose file:

version: "3.9"
services:
  app:
    build:
      context: ./
      dockerfile: Dockerfile
    image: dmc
    container_name: dmc-app
    restart: unless-stopped
    working_dir: /var/www/
    # load development specific .env file
    env_file:
      - ./.env.development
    depends_on:
      - db
      - nginx
    volumes:
      - ./:/var/www/
      - ./docker/php/conf.d/xdebug.ini:/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
      - ./docker/php/conf.d/error_reporting.ini:/usr/local/etc/php/conf.d/error_reporting.ini
      - ./images:/public/images
    expose:
      - "9003"
    networks:
      - dmc-net

  nginx:
    image: nginx:1.23.2-alpine
    container_name: dmc-nginx
    restart: unless-stopped
    ports:
      - "8000:80"
    volumes:
      - ./:/var/www
      - ./docker-compose/nginx:/etc/nginx/conf.d
    networks:
      - dmc-net

  db:
    image: mysql:8.0.31
    container_name: dmc-db
    restart: unless-stopped
    ports:
      - "3307:3306"
    # use the variables declared in .env file
    environment:
      MYSQL_HOST: ${DB_HOST}
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_ROOT_PASSWORD: abcd1234
      MYSQL_USER: ${DB_USERNAME}
      SERVICE_TAGS: development
      SERVICE_NAME: mysql
    volumes:
      - ./docker-compose/mysql:/docker-entrypoint-initdb.d
      - mysql-data:/var/lib/mysql
    networks:
      - dmc-net

networks:
  dmc-net:
    driver: bridge

volumes:
  mysql-data:

When running my tests, ie docker-compose exec app ./vendor/bin/pest tests/Feature/ReturnsTest.ph, they are being run on my local DB which is MySQL and not using SQLite.

I added the following to one of my tests to validate what environment it is executing the tests in:

dd(App::environment());

and it turns out it is returning local instead of testing.

I haven’t touched the phpunit.xml file, and I already checked and it is part of the container (docker-compose exec app cat /var/www/phpunit.xml). Here’s that file’s content:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
         bootstrap="vendor/autoload.php"
         colors="true"
>
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>
        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">./app</directory>
        </include>
    </coverage>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="BCRYPT_ROUNDS" value="4"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="DB_CONNECTION" value="sqlite"/>
        <env name="DB_DATABASE" value=":memory:"/>
        <env name="MAIL_MAILER" value="array"/>
        <env name="QUEUE_CONNECTION" value="sync"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="TELESCOPE_ENABLED" value="false"/>
    </php>
</phpunit>

One of the suggestion I found online was to create .env.testing with the following elements:

APP_ENV=testing
DB_CONNECTION=sqlite
DB_DATABASE=:memory:

but that didn’t make any difference at all.

Any idea what am I missing?

Thanks

Update 1
Output of docker-compose exec app php artisan about added:

docker-compose exec app php artisan about        ✔  8s 
Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003 (through xdebug.client_host/xdebug.client_port).

  Environment ...............................................................................................
  Application Name ......................................................................... DunderMifflin-be
  Laravel Version .................................................................................... 9.40.1
  PHP Version ........................................................................................ 8.1.12
  Composer Version .................................................................................... 2.8.4
  Environment ......................................................................................... local
  Debug Mode ........................................................................................ ENABLED
  URL ............................................................................................. localhost
  Maintenance Mode ...................................................................................... OFF

  Cache .....................................................................................................
  Config ......................................................................................... NOT CACHED
  Events ......................................................................................... NOT CACHED
  Routes ......................................................................................... NOT CACHED
  Views .............................................................................................. CACHED

  Drivers ...................................................................................................
  Broadcasting .......................................................................................... log
  Cache ................................................................................................ file
  Database ............................................................................................ mysql
  Logs .................................................... stack / debug, info, warning, critical, emergency
  Mail ................................................................................................. smtp
  Queue ............................................................................................ database
  Session .............................................................................................. file

2

Answers


  1. This peaked my interest so I tested a bit and can confirm this behaviour.
    Even using <server name="APP_ENV" value="testing"/> in the phpunit.xml doesnt seem to help.

    Following the comment from matiaslauriti, I tried

    php artisan config:clear
    

    which did the trick. A half knowledge guess would be that the $_SERVER array is not re-populated within console commands.

    You can add this to your CMD in the dockerfile
    e.g.
    CMD php artisan migrate --force; php artisan config:clear; php-fpm

    Login or Signup to reply.
  2. Complete shot in the dark here because I know Symfony could show similar behavior for the same reason:

    Try clearing all of your caches with the following:

    php artisan cache:clear
    php artisan route:clear
    php artisan config:clear
    php artisan view:clear
    

    Also ensure APP_ENV is set correctly in your .env file.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search