这个问题已经被问了好几次了,但是我尝试了每种解决方案,发现并进行了具有类似设置的“正常” PHPUnit测试,因此并非我所做的一切都是错误的。
这是问题所在:
我知道,与Gitlab CI和Docker结合使用时,此问题的主要原因(仅报告了此原因)使用了错误的主机。在几乎每种情况下,OP都尝试使用某种本地主机来访问数据库。
这里不可能是这样,因为我的DB_HOST
中的variables
,DB_HOST
文件中的.env
和mysql服务的别名都是相同的。
我什至迷上了容器(通过在黄昏命令之前添加sleep 1h
并执行docker exec
)并成功登录到数据库,并查看了迁移的表和种子表。修补匠也可以解决。
唯一的问题是黄昏。
有趣的事实:转储我的DB_HOST
会得到mysql-test
(我在进行黄昏之前做了config:clear
,甚至将其添加到我的setUp
函数中。
我的想法真的用完了,希望从人群中吸取一些想法。
这是我的.yml文件
stages:
- build
- test
variables:
DB_HOST: mysql-test
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: secret
DB_CONNECTION: mysql
composer:
image: lorisleiva/laravel-docker:latest
stage: build
script:
- composer install --no-progress --no-interaction
- cp .env.gitlab-testing .env
- php artisan key:generate
artifacts:
paths:
- vendor/
- bootstrap/
- .env
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- vendor/
tags:
- docker
npm:
image: lorisleiva/laravel-docker:latest
stage: build
script:
- npm install
- npm run prod
artifacts:
paths:
- public/mix-manifest.json
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- node_modules/
tags:
- docker
dusk:
stage: test
dependencies:
- composer
- npm
tags:
- docker
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- vendor
- node_modules
policy: pull
services:
- name: mysql:5.7
alias: mysql-test
image: chilio/laravel-dusk-ci:latest
script:
- cp .env.dusk.gitlab-testing .env
- cp phpunit.dusk.xml phpunit.xml
- configure-laravel
- start-nginx-ci-project
- php artisan dusk --colors --debug
artifacts:
paths:
- ./storage/logs
- ./tests/Browser/screenshots
- ./tests/Browser/console
expire_in: 7 days
when: always
.env.dusk.gitlab-testing
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
LOG_CHANNEL=stack
HEADLESS=true
DB_CONNECTION=mysql
DB_HOST=mysql-test
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
phpunit.dusk.xml
<?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="Browser Tests">
<directory suffix="Test.php">./tests/Browser</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="local"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
</php>
</phpunit>
答案 0 :(得分:2)
现在确实可以使用。
这是我的文件现在的样子:
.env.dusk.gitlab-testing:
APP_ENV=local
APP_KEY=base64:WY4y4XWUAKbCPzf8XqC92z5wWMn7oOHXZMFMbg9al3E=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
LOG_CHANNEL=stack
DB_CONNECTION=mysql
DB_HOST=mysql-test
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret
BROADCAST_DRIVER=log
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
.gitlab-ci.yml
stages:
- build
- test
- deploy
# Variables
variables:
DB_HOST: mysql-test
MYSQL_DATABASE: laravel
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_ROOT_PASSWORD: secretroot
DB_CONNECTION: mysql
composer:
image: lorisleiva/laravel-docker:latest
stage: build
script:
- cp .env.gitlab-testing .env
- composer install --no-progress --no-interaction
- php artisan key:generate
artifacts:
paths:
- vendor/
- bootstrap/
- .env
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- vendor/
- bootstrap/
tags:
- docker
npm:
image: lorisleiva/laravel-docker:latest
stage: build
script:
- npm install
- npm run prod
artifacts:
paths:
- public/mix-manifest.json
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- node_modules/
tags:
- docker
phpunit:
image: lorisleiva/laravel-docker:latest
stage: test
services:
- name: mysql:5.7
alias: mysql-test
- redis:latest
artifacts:
when: always
paths:
- storage/logs/
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- vendor/
- node_modules/
policy: pull
dependencies:
- composer
- npm
script:
- php artisan migrate
- php artisan db:seed --class=TestingSeeder
- php vendor/bin/phpunit --coverage-text --colors
tags:
- docker
dusk:
stage: test
cache:
key: ${CI_BUILD_REF_NAME}
paths:
- vendor/
- node_modules/
policy: pull
services:
- name: mysql:5.7
alias: mysql-test
image: chilio/laravel-dusk-ci:stable
tags:
- docker
script:
- cp .env.dusk.gitlab-testing .env
- configure-laravel
- composer dump-autoload
- start-nginx-ci-project
- php artisan dusk --colors
artifacts:
paths:
- ./storage/logs # for debugging
- ./tests/Browser/screenshots
- ./tests/Browser/console
expire_in: 7 days
when: always
我猜想它在MYSQL_PASSWORD
文件中缺少.yml
变量,但是我不确定100%。很高兴它现在可以工作。