Github Actions で Laravel + MySQLのテスト

[前回](https://qiita.com/h_koya/items/206a0b0e484614187021)の環境を使用します。

# ワークフローの作成
```bash:wsl
mkdir　-p .github/workflows
touch laravel.yml
```

```yml:laravel.yml
name: Laravel

on:
  push:
    branches: [ main ]
    paths:
      - 'src/**'
  pull_request:
    branches: [ main ]
    paths:
      - 'src/**'
      
jobs:
  laravel-tests:

    runs-on: ubuntu-latest
    
    services:
      mysql:
        image: mysql:5.7
        ports:
        - 3306:3306
        env:
          MYSQL_DATABASE: laravel
          MYSQL_ROOT_PASSWORD: test
        options: --health-cmd "mysqladmin ping -h localhost" --health-interval 20s --health-timeout 10s --health-retries 10
    env:
        DB_CONNECTION: mysql
        DB_HOST: 127.0.0.1
        DB_PORT: 3306
        DB_DATABASE: laravel
        DB_USER_NAME: root
        DB_PASSWORD: test
    steps:
    - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
      with:
        php-version: '7.4'
    - uses: actions/checkout@v2
    - name: Initalize MySQL
      run: mysql -h 127.0.0.1 --port 3306 -uroot -ptest -e "$(cat docker/mysql/initdb.d/*.sql)"
    - name: Copy .env
      working-directory: ./src
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      working-directory: ./src
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - name: Generate key
      working-directory: ./src
      run: php artisan key:generate
    - name: Directory Permissions
      working-directory: ./src 
      run: chmod -R 777 storage bootstrap/cache
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      working-directory: ./src
      run: vendor/bin/phpunit
```

# 動作テスト
## 失敗するテストを試す
### テストファイルの作成
```bash:wsl
docker-compose exec php php artisan make:test GithubActionsTest
```

```php:./src/tests/Feature/GithubActionsTest
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class GithubActionsTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $this->assertTrue(false);
    }
}
```

### ローカルで実行

```bash:wsl
docker-compose exec php php artisan test
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1264045/76688a6f-dcfd-3417-4da8-c90485909de7.png)

### Github上で実行
変更をプッシュ
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1264045/f98dc923-4230-8a7a-fd9c-d6d47ce29477.png)

## DBの初期化&接続テスト
### テストファイルの作成
先ほど作成したファイルを編集します。

```php:./src/tests/Feature/GithubActionsTest
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class GithubActionsTest extends TestCase
{
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function test_example()
    {
        $this->assertDatabaseHas('users', [
            'name' => 'Dog',
        ]);
    }
}
```

### ローカルで実行

```bash:wsl
docker-compose exec php php artisan test
```
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1264045/2fba6dde-cc09-caad-a67a-1bb94d097cb1.png)



### Github上で実行
変更をプッシュ
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1264045/bd1507f1-421a-4fee-7049-50af02971fab.png)