on:
  push:

jobs:
  tests:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php-versions: [ '7.3', '7.4', '8.1', '8.2', '8.3' ]
        phpunit-versions: [ 'latest' ]
        include:
          - operating-system: 'ubuntu-latest'
            php-versions: '8.0'
            phpunit-versions: 9
    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, intl, json
          ini-values: post_max_size=256M, max_execution_time=180
          coverage: xdebug
          tools: php-cs-fixer, phpunit:${{ matrix.phpunit-versions }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      - name: Checkout code
        uses: actions/checkout@v4
      - name: Get composer cache directory
        id: composer-cache
        run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

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

      - name: Install dependencies
        run: composer install --prefer-dist

      - uses: GuillaumeFalourd/wait-sleep-action@v1
        with:
          time: '5' # wait 5 seconds before running tests (to avoid Flarum too-many-requests error)

      - name: Run tests
        run: composer test
        env:
          FLARUM_USERNAME: ${{ secrets.FLARUM_USERNAME }}
          FLARUM_PASSWORD: ${{ secrets.FLARUM_PASSWORD }}
