我试图找出为什么在我的测试套件到达时它在第一次测试中失败的原因,该测试说从Laravel应用程序运行我的测试套件时数据库失去了连接。
The command "php artisan key:generate" exited with 0.
31.27s$ vendor/bin/phpunit
PHPUnit Pretty Result Printer 0.19.14 by Codedungeon and contributors.
==> Configuration:~/build/me/app/vendor/codedungeon/phpunit-result-printer/phpunit-printer.yml
PHPUnit 7.3.5 by Sebastian Bergmann and contributors.
==> Tests\Feature\Module\MyTest⚈
Time: 31.12 seconds, Memory: 24.00MB
There was 1 error:
1) Tests\Feature\Module\Test::my_test_method
Illuminate\Database\QueryException: SQLSTATE[HY000] [2002] Connection timed out (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
Caused by
Doctrine\DBAL\Driver\PDOException: SQLSTATE[HY000] [2002] Connection timed out
.travis.yml
language: php
php:
- 7.1
services:
- mysql
before_script:
- cp .env.travis .env
- mysql -e 'CREATE DATABASE my_database;'
- composer self-update
- composer install --no-interaction
- php artisan key:generate
- php artisan migrate
script:
- vendor/bin/phpunit
cache:
directories:
- vendor
.env.travis
APP_ENV=testing
APP_KEY=
APP_DEBUG=true
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=my_database
DB_USERNAME=root
DB_PASSWORD=
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
以下内容仅包含我已更改或添加到这些文件的行。
phpunit.xml
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="my_database"/>
config / database.php
'mysql_testing' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '3306'),
'database' => env('DB_DATABASE_TESTING', 'forge'),
'username' => env('DB_USERNAME_TESTING', 'forge'),
'password' => env('DB_PASSWORD_TESTING', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => 'InnoDB',
],