name: "Continuous Integration"

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

env:
  COMPOSER_FLAGS: "--ansi --no-interaction --no-progress --prefer-dist"

jobs:
  tests:
    name: "PHP ${{ matrix.php-version }}, ${{ matrix.dependencies }} dependecies, experimental: ${{ matrix.experimental || false}}"

    runs-on: ubuntu-latest
    continue-on-error: ${{ matrix.experimental || false }}

    strategy:
      fail-fast: false
      matrix:
        php-version: [ '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']
        dependencies: [highest]
        include:
          - php-version: "7.2"
            dependencies: lowest
          - php-version: "8.6"
            dependencies: ignore
            experimental: true

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

      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "none"
          extensions: "intl, zip"
          ini-values: "memory_limit=-1, phar.readonly=0, error_reporting=E_ALL, display_errors=On"
          php-version: "${{ matrix.php-version }}"
          tools: composer

      - name: "Handle lowest dependencies update"
        if: "contains(matrix.dependencies, 'lowest')"
        run: "echo \"COMPOSER_UPDATE_FLAGS=$COMPOSER_UPDATE_FLAGS --prefer-lowest\" >> $GITHUB_ENV"

      - name: "Handle ignore-platform-reqs dependencies update"
        if: "contains(matrix.dependencies, 'ignore')"
        run: "echo \"COMPOSER_FLAGS=$COMPOSER_FLAGS --ignore-platform-req=php\" >> $GITHUB_ENV"

      - name: "Update dependencies"
        run: "composer update ${{ env.COMPOSER_UPDATE_FLAGS }} ${{ env.COMPOSER_FLAGS }}"

      - name: "Validate composer.json"
        run: "composer validate"

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

      - name: "Run tests"
        run: "composer test"