name: Run unit tests on PR
on: 
  pull_request
jobs:
  test:
    name: PHP ${{ matrix.php }} WP ${{ matrix.wp }} MU ${{ matrix.multisite }}
    timeout-minutes: 15
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        # We test against the earliest and latest PHP versions for each major supported version.
        php: [ '5.6', '7.0', '7.4', '8.0' ]
        wp: [ '5.2', '5.3', '5.4', '5.5', '5.6', 'latest' ]
        multisite: [ '0', '1' ]
        exclude:
          # Earlier versions of WordPress do not support PHP 7.4+
          - php: 7.4
            wp: 5.2
          - php: 8.0
            wp: 5.2
          - php: 8.0
            wp: 5.3
          - php: 8.0
            wp: 5.4
          - php: 8.0
            wp: 5.5
    services:
      database:
        image: mysql:5.6
        env:
          MYSQL_ROOT_PASSWORD: root
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          tools: composer
          extensions: mysql
          coverage: none

      - name: Tool versions
        run: |
          php --version
          composer --version

      - name: Get cached composer directories
        uses: actions/cache@v2
        with:
          path: ./vendor
          key: ${{ runner.os }}-${{ hashFiles('./composer.lock') }}

      - name: Setup and install composer
        run: composer install

      - name: Setup PHPUnit 
        run: |
          # PHPUnit 5.7 when using PHP 5.6 - 7.0.
          if [ "$(php -r "echo version_compare( PHP_VERSION, '7.1', '<' );")" ]; then
            curl -L https://phar.phpunit.de/phpunit-5.7.phar -o /tmp/phpunit
            OVERWRITE=1
          # PHPUnit 7.5 when using PHP 7.1 - 7.4.
          elif [ "$(php -r "echo version_compare( PHP_VERSION, '8.0', '<' );")" ]; then
            curl -L https://phar.phpunit.de/phpunit-7.5.phar -o /tmp/phpunit
            OVERWRITE=1
          # PHPUnit 7.5 (Custom Fork) when using PHP 8.0+.
          else
            curl -L https://github.com/woocommerce/phpunit/archive/add-compatibility-with-php8-to-phpunit-7.zip -o /tmp/phpunit-7.5-fork.zip
            unzip -d /tmp/phpunit-fork /tmp/phpunit-7.5-fork.zip
            composer --working-dir=/tmp/phpunit-fork/phpunit-add-compatibility-with-php8-to-phpunit-7 install
            rm ./vendor/bin/phpunit
            ln -sf /tmp/phpunit-fork/phpunit-add-compatibility-with-php8-to-phpunit-7/phpunit ./vendor/bin/phpunit
          fi
          
          if [ $OVERWRITE ]; then
            rm ./vendor/bin/phpunit
            chmod +x /tmp/phpunit
            mv /tmp/phpunit ./vendor/bin/phpunit
          fi

      - name: Init DB and WP
        run: ./tests/bin/install.sh woo_test root root 127.0.0.1 ${{ matrix.wp }}

      - name: Run tests
        run: |
          ./vendor/bin/phpunit --version
          WP_MULTISITE=${{ matrix.multisite }} ./vendor/bin/phpunit -c ./tests/phpunit.xml.dist

      - name: Code Coverage
        run: |
          bash <(curl -s https://codecov.io/bash)
