name: Test

on:
  workflow_dispatch:
  push:
    # only run for branches, not tags
    branches-ignore: php8
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        # PHP Unit 4.x is needed for PHP 5.4 but is not supported on PHP 8.x
        php: [5.4, 5.6, 7.0, 7.4]
      fail-fast: false
    steps:
      - uses: actions/checkout@v3
      - uses: shivammathur/setup-php@v2
        with:
          php-version: ${{matrix.php}}
          coverage: xdebug
      - name: Cache Composer packages
        id: composer-cache
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php-
      - name: Install dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
      - name: PHP Unit
        run: |
          echo '{}' | jq '.http.upload.skip |= ${{ secrets.SKIP_HTTP_UPLOAD }}' > tests/config.json
          vendor/bin/phpunit tests

  test-php8:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: [8.0, 8.1, 8.2]
      fail-fast: false
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Rebase PHP8 branch
        run: |
          git config user.email "${{ github.event.pusher.email }}"
          git config user.name "${{ github.event.pusher.name }}"
          git pull
          git checkout php8
          git rebase ${{ github.head_ref || github.ref_name }} php8
          git status
          echo 'diff origin/php8:'
          git diff origin/php8
          echo 'diff current:'
          git diff ${{ github.head_ref || github.ref_name }}
      - uses: shivammathur/setup-php@v2
        with:
          php-version: ${{matrix.php}}
          coverage: xdebug
      - name: Cache Composer packages
        id: composer-cache
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php8-
      - name: Install dependencies
        run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
      - name: PHP Unit
        run: |
          echo '{}' | jq '.http.upload.skip |= ${{ secrets.SKIP_HTTP_UPLOAD }}' > tests/config.json
          vendor/bin/phpunit tests

  rebase-php8:
    runs-on: ubuntu-latest
    needs: test-php8
    if: github.ref_name == 'master'
    steps:
      - uses: actions/checkout@v3
        with:
          ref: php8
          fetch-depth: 0
      - name: Rebase PHP8 branch
        run: |
          git config user.email "${{ github.event.pusher.email }}"
          git config user.name "${{ github.event.pusher.name }}"
          git pull
          git rebase origin/master php8
          git status
          echo 'diff origin/php8:'
          git diff origin/php8
          echo 'diff master:'
          git diff origin/master
          git push --force-with-lease
