拉取合并之前的单元测试无法连接到数据库

问题描述 投票:0回答:1

我已经设置了一个 GitHub Actions 工作流程,在将功能分支合并到 Laravel 应用程序的主分支之前运行

name: Test before Merge
on:
  pull_request:
    branches: [ main ]
jobs:
  test:
    name: Deploy
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: test_root_password
          MYSQL_DATABASE: test_database
          MYSQL_USER: test_user
          MYSQL_PASSWORD: test_password
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: '8.1'

      - name: Install dependencies
        run: composer install

      - name: Create .env file
        run: cp .env.ci .env

      - name: Generate application key
        run: php artisan key:generate

      - name: Migrate and seed database
        run: php artisan migrate:fresh --seed

      - name: Run PHPUnit
        run: vendor/bin/phpunit

单元测试访问 MySQL 数据库并与之交互,每当我运行测试时,都会收到以下错误:

SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: select table_name as `name`, (data_length + index_length) as `size`, table_comment as `comment`, engine as `engine`, table_collation as `collation` from information_schema.tables where table_schema = 'test_database' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') order by table_name)

以下是

env.ci
文件数据:

APP_KEY=
APP_ENV=local
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=test_database
DB_USERNAME=test_user
DB_PASSWORD=test_password
php mysql laravel github github-actions
1个回答
0
投票

通过将 .env.ci 中的 DB_HOST=localhost 更改为 DB_HOST=127.0.0.1

修复了该错误

修复上述与无法连接到数据库相关的错误后,还出现了另一个错误。为了正确连接到 MySQL 数据库,在 phpunit.xml 中,我删除了

<env name="DB_DATABASE" value="testing"/>
行,以便从 Github Action

中的 .env 获取 DB_DATABASE 值
© www.soinside.com 2019 - 2024. All rights reserved.