Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: enhance automated Laravel test GitHub action #62

Merged
merged 15 commits into from
Dec 11, 2023
79 changes: 79 additions & 0 deletions .github/workflows/laravel-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Laravel Application Tests

on:
workflow_call:
inputs:
db-type:
description: "Database type"
required: true
type: string
db-version:
description: "Database version"
required: true
type: string

jobs:
laravel-test:
name: Laravel (PHP ${{ matrix.php-versions }}) - ${{ inputs.db-type }} ${{ inputs.db-version }}
runs-on: ubuntu-latest
env:
DB_CONNECTION: ${{ inputs.db-type }}
DB_PORT: ${{ inputs.db-type == 'mysql' && '3306' || inputs.db-type == 'pgsql' && '5432' }}
DB_USERNAME: radioroster
DB_PASSWORD: testPassword
DB_DATABASE: radioroster
APP_ENV: testing

strategy:
fail-fast: false
matrix:
php-versions: ["8.1", "8.2", "8.3"]

steps:
- if: ${{ inputs.db-type == 'mysql' }}
name: Setup MariaDB ${{ inputs.db-version }}
run: |
docker run -d --name mariadb -e MARIADB_RANDOM_ROOT_PASSWORD=true -e MARIADB_DATABASE=${{ env.DB_DATABASE }} -e MARIADB_USER=${{ env.DB_USERNAME }} -e MARIADB_PASSWORD=${{ env.DB_PASSWORD }} --publish 3306:3306 mariadb:${{ inputs.db-version }}

- if: ${{ inputs.db-type == 'pgsql' }}
name: Setup PostgreSQL ${{ inputs.db-version }}
run: |
docker run -d --name postgres -e POSTGRES_DB=${{ env.DB_DATABASE }} -e POSTGRES_USER=${{ env.DB_USERNAME }} -e POSTGRES_PASSWORD=${{ env.DB_PASSWORD }} --publish 5432:5432 postgres:${{ inputs.db-version }}

- name: Checkout
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4

- name: Setup PHP ${{ matrix.php-versions }}
uses: shivammathur/setup-php@e8cd65f444039503a75cf4057d55442fc4316f78
with:
php-version: ${{ matrix.php-versions }}
extensions: pcntl, zip, intl, exif, mbstring, dom, fileinfo, ${{ inputs.db-type == 'mysql' && 'pdo_mysql' || inputs.db-type == 'pgsql' && 'pdo_pgsql' }}
coverage: xdebug

- name: Get Composer Cache Directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache Composer Dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install Composer Dependencies
run: composer install -q --no-interaction --no-scripts --no-progress --prefer-dist --optimize-autoloader

- name: Prepare Laravel Application
run: |
php -r "file_exists('.env') || copy('.env.example', '.env');"
php artisan key:generate

- name: Clear config
run: php artisan config:clear

- name: Run Migrations
run: php artisan migrate -v

- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit --coverage-text
49 changes: 22 additions & 27 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
name: Laravel
name: Laravel Test - MariaDB & PostgreSQL

on:
push:
branches: [ "main" ]
branches: [main]
pull_request:
branches: [ "main" ]
branches: [main]

jobs:
laravel-tests:
laravel-mariadb:
name: Laravel with MariaDB ${{ matrix.mariadb-versions }}
uses: ./.github/workflows/laravel-test.yml
with:
db-type: mysql
db-version: ${{ matrix.mariadb-versions }}

runs-on: ubuntu-latest
strategy:
matrix:
mariadb-versions: ["10", "11"]

steps:
- uses: shivammathur/setup-php@e8cd65f444039503a75cf4057d55442fc4316f78
with:
php-version: '8.1'
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
laravel-psql:
name: Laravel with PostgreSQL ${{ matrix.pgsql-versions }}
uses: ./.github/workflows/laravel-test.yml
with:
db-type: pgsql
db-version: ${{ matrix.pgsql-versions }}

strategy:
matrix:
pgsql-versions: ["14", "15", "16"]