I have set up a GitHub Actions workflow to run before merging a feature branch to main branch of my Laravel application
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
The unit tests access and interact with MySQL database, whenever I run the test I get the error below:
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)
Below is env.ci
file data:
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
The error was fixed by changing the DB_HOST=localhost to DB_HOST=127.0.0.1 in the .env.ci. I also changed the mysql version from 8.0
to latest
on the github action yml
Also another error showed up after fixing the above relating to failing to connect to the database. In order to connect correctly to the MySQL database, In the phpunit.xml I removed the <env name="DB_DATABASE" value="testing"/>
row so that the DB_DATABASE value is take from the .env in the Github Action