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
I encountered the same issue while developing Laravel apps with Docker. For me, the solution was simply to replace the <env />
attributes in the phpunit.xml file by <server \>
.
Here is my phpunit.xml file:
<?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>tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory>tests/Feature</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>app</directory>
</include>
</source>
<php>
<server name="APP_ENV" value="testing"/>
<server name="APP_MAINTENANCE_DRIVER" value="file"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_STORE" value="array"/>
<server name="DB_HOST" value="mysql_test"/>
<server name="MAIL_MAILER" value="array"/>
<server name="PULSE_ENABLED" value="false"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="TELESCOPE_ENABLED" value="false"/>
</php>
</phpunit>