From 38beae297b86add52c7b59f874091be34257d00f Mon Sep 17 00:00:00 2001 From: Lapotor <17144397+Lapotor@users.noreply.github.com> Date: Mon, 11 Dec 2023 21:09:26 +0100 Subject: [PATCH] test: enhance automated Laravel test GitHub action (#62) This enhances the automated PHP test GitHub action with the following improvements: - **Test on Multiple PHP Versions:** The GitHub action now tests on multiple PHP versions, ensuring compatibility across a range of PHP environments. - **Integrate Databases for Testing:** A MariaDB & PostgreSQL database has been added to have real world testing. - **Include Caching for Optimization:** Caching mechanisms have been integrated to optimize the testing process, improving overall workflow efficiency. These changes collectively enhance the reliability and compatibility of the automated Laravel test GitHub action, providing a more comprehensive validation of the application across different PHP environments and database configurations. --------- Signed-off-by: Valentin Sickert <17144397+Lapotor@users.noreply.github.com> --- .github/workflows/laravel-test.yml | 79 ++++++++++++++++++++++++++++++ .github/workflows/laravel.yml | 49 +++++++++--------- 2 files changed, 101 insertions(+), 27 deletions(-) create mode 100644 .github/workflows/laravel-test.yml diff --git a/.github/workflows/laravel-test.yml b/.github/workflows/laravel-test.yml new file mode 100644 index 00000000..ddd4a1fd --- /dev/null +++ b/.github/workflows/laravel-test.yml @@ -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 diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml index a39ae2c2..99a47fda 100644 --- a/.github/workflows/laravel.yml +++ b/.github/workflows/laravel.yml @@ -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"]