name: CI

on:
  push:
    branches:
      - "*.x"
  pull_request:

env:
  DEPENDENCIES: 'toflar/psr6-symfony-http-cache-store:^2|^3|^4'
  SYMFONY_DEPRECATIONS_HELPER: 'max[self]=0'

jobs:
  latest:
    env:
      VARNISH_VERSION: ${{ matrix.varnish-version }}
      VARNISH_MODULES_VERSION: ${{ matrix.varnish-modules-version }}
    name: PHP ${{ matrix.php }} Varnish ${{ matrix.varnish-version }} SF ${{ matrix.symfony-version }}
    runs-on: ubuntu-20.04
    strategy:
      fail-fast: false
      matrix:
        varnish-version: ['6.6']
        varnish-modules-version: ['0.18.0']
        php: ['8.1', '8.2', '8.3']
        include:
        - php: '8.1'
          symfony-version: '6.*'
        - php: '8.1'
          varnish-version: '7.1'
          varnish-modules-version: '0.20.0'
        - php: '8.2'
          symfony-version: '7.*'
        - php: '8.2'
          varnish-version: '7.1'
          varnish-modules-version: '0.20.0'
        - php: '8.3'
          symfony-version: '7.*'
        - php: '8.3'
          varnish-version: '7.1'
          varnish-modules-version: '0.20.0'

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer:v2, flex
          coverage: none

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

      - name: Cache module compilation
        uses: actions/cache@v4
        with:
          path: /tmp/varnish-modules-${{ matrix.varnish-modules-version }}
          key: varnish-modules-${{ matrix.varnish-modules-version }}

      - name: Setup Varnish and Nginx
        run: |
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-varnish.sh
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-nginx.sh

      - name: Cache Vendor
        id: cache-vendor
        uses: actions/cache@v4
        with:
          path: vendor
          key: ${{ runner.os }}-vendor

      - name: Install composer dependencies
        env:
          SYMFONY_REQUIRE: ${{ matrix.symfony-version }}
        run: |
          composer require --no-update ${DEPENDENCIES}
          composer update --prefer-dist --no-interaction --no-progress

      - name: Execute tests
        run: vendor/bin/phpunit

  varnish5:
    name: PHP ${{ matrix.php }} Legacy Varnish 5
    runs-on: ubuntu-20.04
    env:
      VARNISH_VERSION: '5.1'
      VARNISH_MODULES_VERSION: '0.14.0'

    strategy:
      fail-fast: false
      matrix:
        include:
          - php: '8.1'

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer:v2
          coverage: none

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

      - name: Cache module compilation
        uses: actions/cache@v4
        with:
          path: /tmp/varnish-modules-${{ matrix.varnish-modules-version }}
          key: varnish-modules-${{ matrix.varnish-modules-version }}

      - name: Setup Varnish and Nginx
        run: |
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-varnish-legacy.sh
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-nginx.sh

      - name: Cache Vendor
        id: cache-vendor
        uses: actions/cache@v4
        with:
          path: vendor
          key: ${{ runner.os }}-vendor

      - name: Install composer dependencies
        # Lowest discovery can end up with an incompatible psr7 implementation, see discussion in https://github.com/FriendsOfSymfony/FOSHttpCache/pull/567
        run: |
          composer require --no-update --no-interaction "guzzlehttp/psr7:2.*"
          composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress

      - name: Execute tests
        run: vendor/bin/phpunit

  varnish4:
    name: PHP ${{ matrix.php }} Legacy Varnish 4
    runs-on: ubuntu-20.04
    env:
      VARNISH_VERSION: '4.1'
      VARNISH_MODULES_VERSION: '' # varnish modules compilation fails, not sure why

    strategy:
      fail-fast: false
      matrix:
        include:
          - php: '8.1'

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer:v2
          coverage: none

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

      - name: Setup Varnish and Nginx
        run: |
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-varnish-legacy.sh
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-nginx.sh

      - name: Cache Vendor
        id: cache-vendor
        uses: actions/cache@v4
        with:
          path: vendor
          key: ${{ runner.os }}-vendor

      - name: Install composer dependencies
        run: |
          composer require --no-update --no-interaction "guzzlehttp/psr7:2.*"
          composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress

      - name: Execute tests
        run: vendor/bin/phpunit

  lowest:
    name: PHP ${{ matrix.php }} Lowest, Varnish 3
    runs-on: ubuntu-20.04
    env:
      VARNISH_VERSION: '3.0'
      VARNISH_MODULES_VERSION: ''
      DEPENDENCIES: ''

    strategy:
      fail-fast: false
      matrix:
        php: ['8.1']

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer:v2
          coverage: none

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

      - name: Setup Varnish and Nginx
        run: |
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-varnish-legacy.sh
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-nginx.sh

      - name: Cache Vendor
        id: cache-vendor
        uses: actions/cache@v4
        with:
          path: vendor
          key: ${{ runner.os }}-vendor-lowest

      - name: Install composer dependencies
        run: |
          composer require --no-update --no-interaction "guzzlehttp/psr7:2.*"
          composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress

      - name: Execute tests
        run: vendor/bin/phpunit

  coverage:
    name: Code Coverage
    runs-on: ubuntu-20.04
    env:
      VARNISH_VERSION: '6.6'
      VARNISH_MODULES_VERSION: '0.18.0'

    steps:
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: 8.2
          tools: composer:v2
          coverage: xdebug

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

      - name: Cache module compilation
        uses: actions/cache@v4
        with:
          path: /tmp/varnish-modules-${{ matrix.varnish-modules-version }}
          key: varnish-modules-${{ matrix.varnish-modules-version }}

      - name: Setup Varnish and Nginx
        run: |
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-varnish.sh
          sh ${GITHUB_WORKSPACE}/.github/workflows/setup-nginx.sh

      - name: Cache Vendor
        id: cache-vendor
        uses: actions/cache@v4
        with:
          path: vendor
          key: ${{ runner.os }}-vendor

      - name: Install dependencies
        run: |
          composer require "friends-of-phpspec/phpspec-code-coverage:^6.3.0" --no-interaction --no-update
          composer update --prefer-dist --no-interaction --no-progress

      - name: Execute tests
        run: vendor/bin/phpunit --coverage-text --coverage-clover build/coverage.xml
