name: PHP

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  run:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mysql:5.7
        ports:
          - 3306:3306
        env:
          MYSQL_ALLOW_EMPTY_PASSWORD: yes
      redis:
        image: redis:6.0
        ports:
          - 6379:6379
      mongo:
        image: mongo:4.2-bionic
        ports:
          - 27017:27017
      memcached:
        image: memcached:1.6
        ports:
          - 11211:11211

    strategy:
      fail-fast: false
      matrix:
        include:
          - php: 7.2
            composer: '--prefer-lowest'
            desc: "Lowest versions"
          - php: 7.4
            composer: '--prefer-lowest'
            desc: "Lowest versions"
          - php: 7.2
          - php: 7.3
          - php: 7.4
            coverage: '--coverage-clover /tmp/clover.xml'
          - php: 8.0
    name: PHP ${{ matrix.php }} ${{ matrix.desc }}

    steps:
    - uses: actions/checkout@v2

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ matrix.php }}
        coverage: xdebug
        extensions: apcu, mongodb, memcached
        ini-values: apc.enable_cli=1,mysqli.default_host=127.0.0.1,mysqli.default_port=3306,mysqli.default_user=root

    - name: Validate composer.json and composer.lock
      run: composer validate

    - name: Install dependencies
      run: composer update --prefer-dist --no-progress ${{ matrix.composer }}

    - name: Run PHPUnit
      run: vendor/bin/phpunit ${{ matrix.coverage }}

    - name: Upload coverage to Scrutinizer
      if: ${{ matrix.coverage }}
      run: >
        wget https://scrutinizer-ci.com/ocular.phar -O "/tmp/ocular.phar" &&
        php "/tmp/ocular.phar" code-coverage:upload --format=php-clover /tmp/clover.xml
