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: ['7.3', '7.4', '8.0', '8.1']
        include:
        - php: '7.4'
          symfony-version: '4.3.*'
        - php: '7.4'
          symfony-version: '5.0.*'
        - php: '8.1'
          symfony-version: '6.*'
        - php: '8.1'
          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@v2

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

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

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

  legacyPHP:
    name: PHP 7.2 Varnish 6
    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: 7.2
          tools: composer:v2
          coverage: none

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

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

      - name: Install composer dependencies
        run: |
          composer require --no-update ${DEPENDENCIES}
          composer update --prefer-dist --no-interaction --no-progress
          vendor/bin/simple-phpunit install

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

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

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

    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@v2

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

      - name: Install composer dependencies
        run: |
          composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress
          vendor/bin/simple-phpunit install

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

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

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

    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@v2

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

      - name: Install composer dependencies
        run: |
          composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress
          vendor/bin/simple-phpunit install

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

  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: ['7.2']

    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@v2

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

      - name: Install composer dependencies
        run: |
          composer update --prefer-dist --prefer-stable --prefer-lowest --no-interaction --no-progress
          vendor/bin/simple-phpunit install

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

  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: 7.4
          tools: composer:v2
          coverage: xdebug

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

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

      - name: Install dependencies
        run: |
          composer require "friends-of-phpspec/phpspec-code-coverage:^4.3.2" --no-interaction --no-update
          composer update --prefer-dist --no-interaction --no-progress
          vendor/bin/simple-phpunit install

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

      - name: Upload coverage
        run: |
          wget https://scrutinizer-ci.com/ocular.phar
          php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml --revision=${{ github.event.pull_request.head.sha || github.sha }}
