name: PHPUnit

on:
  pull_request:
    branches:
      - develop
    paths:
      - '**.php'
      - 'composer.*'
      - 'phpunit*'
      - '.github/workflows/phpunit.yml'
  push:
    branches:
      - develop
    paths:
      - '**.php'
      - 'composer.*'
      - 'phpunit*'
      - '.github/workflows/phpunit.yml'

jobs:
  main:
    name: PHP ${{ matrix.php-versions }} Unit Tests
    runs-on: ubuntu-latest

    services:
      postgres:
        # Docker Hub image
        image: postgres
        # Provide the password for postgres
        env:
          POSTGRES_PASSWORD: postgres
        ports:
          # Maps tcp port 5432 on service container to the host
          - 5432:5432
        # Set health checks to wait until postgres has started
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
      mysql:
        image: mysql:latest
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: false
          MYSQL_ROOT_PASSWORD: mythauth
          MYSQL_DATABASE: mythauth
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3

    if: "!contains(github.event.head_commit.message, '[ci skip]')"
    strategy:
      matrix:
        php-versions: ['7.4', '8.0', '8.1']

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Set up PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-versions }}
          tools: composer, phive, phpunit
          extensions: intl, json, mbstring, gd, mysqlnd, pgsql, xdebug, xml, sqlite3
          coverage: xdebug
        env:
          COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Get composer cache directory
        id: composer-cache
        run: echo "::set-output name=dir::$(composer config cache-files-dir)"

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

      - name: Install dependencies
        run: |
          if [ -f composer.lock ]; then
            composer install --no-progress --no-interaction --prefer-dist --optimize-autoloader
          else
            composer update --no-progress --no-interaction --prefer-dist --optimize-autoloader
          fi

      - name: Test with PHPUnit (SQLite)
        run: vendor/bin/phpunit --verbose --coverage-text
        env:
          TERM: xterm-256color
          TACHYCARDIA_MONITOR_GA: enabled

      - name: Test with PHPUnit (MySQL/MariaDB)
        run: vendor/bin/phpunit --verbose --coverage-text
        env:
          TERM: xterm-256color
          TACHYCARDIA_MONITOR_GA: enabled
          database_tests_DBDriver: MySQLi
          database.tests.hostname: 127.0.0.1
          database_tests_port: 3306
          database_tests_database: mythauth
          database_tests_username: root
          database_tests_password: mythauth
          database.tests.DBPrefix: tests_

      - name: Test with PHPUnit (PostgreSQL)
        run: vendor/bin/phpunit --verbose --coverage-text
        env:
          TERM: xterm-256color
          TACHYCARDIA_MONITOR_GA: enabled
          database_tests_DBDriver: Postgre
          database.tests.hostname: localhost
          database_tests_port: 5432
          database_tests_database: postgres
          database_tests_username: postgres
          database_tests_password: postgres
          database.tests.DBPrefix: tests_

      - if: matrix.php-versions == '8.0'
        name: Run Coveralls
        continue-on-error: true
        run: |
          sudo phive --no-progress install --global --trust-gpg-keys E82B2FB314E9906E php-coveralls
          php-coveralls --verbose --coverage_clover=build/phpunit/clover.xml --json_path build/phpunit/coveralls-upload.json
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          COVERALLS_PARALLEL: true
          COVERALLS_FLAG_NAME: PHP ${{ matrix.php-versions }}

  coveralls:
    needs: [main]
    name: Coveralls Finished
    runs-on: ubuntu-latest
    steps:
      - name: Upload Coveralls results
        uses: coverallsapp/github-action@master
        continue-on-error: true
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          parallel-finished: true
