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: [ '7.1', '7.4', '8.0', '8.3' ]
        wp: [ '6.4', '6.5', '6.6', 'latest', 'nightly' ]
        multisite: [ '0', '1' ]
        exclude:
          # WordPress 6.6+ requires PHP 7.2+
          - php: 7.1
            wp: 6.6
          - php: 7.1
            wp: latest
          - php: 7.1
            wp: nightly
    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 composer cache directory
        id: composer-cache
        run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: ${{ steps.composer-cache.outputs.dir }}
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install dependencies
        run: composer install --prefer-dist

      - name: Setup PHPUnit 
        run: |
          # PHPUnit 7.5 when using PHP 7.1 - 7.4.
          if [ "$(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)
