fruivita/corporate

View on GitHub
.github/workflows/tests.yml

Summary

Maintainability
Test Coverage
###############################################################################
# About GitHub Actions                                                        #
# https://docs.github.com/en/actions/learn-github-actions                     #
# https://www.youtube.com/watch?v=TLB5MY9BBa4                                 #
###############################################################################
#
# Run unit and functional tests and generate code coverage rate.
# PestPhp: https://pestphp.com/docs/installation
#
# Coverage rate is generated and exported to code analysis service 'Code
# Climate' only in ubuntu-latest environment and php version 8.1
#
# - About: https://codeclimate.com/
# - How to upload: https://docs.codeclimate.com/docs/github-actions-test-coverage

name: Unit and Feature tests

on: ['workflow_dispatch', 'push', 'pull_request']

concurrency:
  group: tests-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  test:
    runs-on: ${{ matrix.os }}

    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest]
        php: [8.0, 8.1]
        laravel: [9.*]
        stability: [prefer-stable]
        include:
          - laravel: 9.*
            testbench: ^7.4

    name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

    steps:

      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup MySql
        run: |
          sudo /etc/init.d/mysql start
          mysql -e 'CREATE DATABASE package;' -uroot -proot
          mysql -e 'SHOW DATABASES;' -uroot -proot
          mysql -V

      - name: Cache dependencies
        uses: actions/cache@v3
        with:
          path: ~/.composer/cache/files
          key: dependencies-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

      - name: Setup PHP with coverage
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: bcmath, ctype, curl, dom, fileinfo, filter, json, libxml, mbstring, openssl, pcre, pdo, pdo_mysql, phar, tokenizer, xml, xmlreader, xmlwriter
          tools: composer:v2
          coverage: xdebug
        if: matrix.os == 'ubuntu-latest' && matrix.php == '8.1'

      - name: Setup PHP without coverage
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: bcmath, ctype, curl, dom, fileinfo, filter, json, libxml, mbstring, openssl, pcre, pdo, pdo_mysql, phar, tokenizer, xml, xmlreader, xmlwriter
          tools: composer:v2
          coverage: xdebug
        if: (matrix.os == 'ubuntu-latest' && matrix.php == '8.1') == false

      - name: Setup problem matchers
        run: |
          echo "::add-matcher::${{ runner.tool_cache }}/php.json"
          echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      - name: Install dependencies
        run: |
          composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
          composer update --${{ matrix.stability }} --prefer-stable --no-interaction --no-progress

      - name: Execute tests with coverage
        run: |
          php --version
          ./vendor/bin/pest --coverage --colors=always --stop-on-failure
        if: matrix.os == 'ubuntu-latest' && matrix.php == '8.1'

      - name: Execute tests without coverage
        run: |
          php --version
          ./vendor/bin/pest --colors=always --stop-on-failure
        if: (matrix.os == 'ubuntu-latest' && matrix.php == '8.1') == false

      - name: Upload coverage to Code Climate
        uses: paambaati/codeclimate-action@v3.0.0
        env:
          CC_TEST_REPORTER_ID: ${{ secrets.CORPORATE_CC_TEST_REPORTER_ID }}
        with:
          debug: true
        if: matrix.os == 'ubuntu-latest' && matrix.php == '8.1'