admin管理员组文章数量:1135117
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=";
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'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
Share
Improve this question
edited Dec 29, 2024 at 2:30
MrCujo
asked Dec 27, 2024 at 22:47
MrCujoMrCujo
1,3134 gold badges42 silver badges84 bronze badges
3
|
4 Answers
Reset to default 0 +50I 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>
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
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.
You can pass the APP_ENV
variable to docker-compose
using the --env
(or -e
) flag like this:
docker-compose exec -e APP_ENV=testing app ./vendor/bin/pest tests/Feature/ReturnsTest.php
P.S.: The docker-compose
tool is now outdated and replaced by the docker compose
subcommand.
Update:
You can also try adding the force
attribute to the affected <env>
tags, like this:
<env name="APP_ENV" value="testing" force="true" />
本文标签:
版权声明:本文标题:php - Laravel is always returning local when checking the environment, even when tests are being executed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736909999a1956109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
phpunit.xml
? I know you didn't change it, but I have no idea what it has. Also, you never runphp artisan config:cache
, right? Runphp artisan about
and check if the config is cached or not. Nothing should be cached, runphp artisan optimize:clear
– matiaslauriti Commented Dec 27, 2024 at 23:02phpunit.xml
and alsophp artisan about
. No, I haven't runphp artisan config:cache
– MrCujo Commented Dec 27, 2024 at 23:11.env.development
to another folder that is not inside the laravel project? It should not exactly conflict in any way, but there is something really wrong in there going arround – matiaslauriti Commented Dec 28, 2024 at 14:36