name: Timber Tests

on:
  push:
    branches:
      - '*'
  pull_request:
    types:
      - opened
      - synchronize
      - ready_for_review

# Cancel previous workflow run groups that have not completed.
concurrency:
  # Group workflow runs by workflow name, along with the head branch ref of the pull request
  # or otherwise the branch or tag ref.
  group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.ref }}
  cancel-in-progress: true

jobs:
  phpunit:
    runs-on: ubuntu-latest

    services:
      mysql:
          image: mariadb:latest
          env:
              MYSQL_USER: root
              MYSQL_ALLOW_EMPTY_PASSWORD: true
              MYSQL_ROOT_PASSWORD: ''
              MYSQL_DATABASE: wordpress_test
          ports:
              - 3306
          options: --health-cmd="healthcheck.sh --connect --innodb_initialized" --health-interval=10s --health-timeout=5s --health-retries=3

    continue-on-error: ${{ matrix.experimental }}
    strategy:
      fail-fast: false
      matrix:
        php: ['7.2', '7.3', '7.4']
        wp: ['5.3', 'latest']
        multisite: ['0', '1']
        dependency-version: ['highest']
        webp: [false]
        coverage: [false]
        extensions: ['gd']
        experimental: [false]
        include:
          # WP Trunk
          - php: '7.4'
            wp: 'trunk'
            dependency-version: 'highest'
            multisite: '0'
            experimental: true
          # PHP 8.1 / expiremntal
          - php: '8.1'
            wp: 'trunk'
            dependency-version: 'highest'
            multisite: '0'
            experimental: true
          # PHP 8.0
          - php: '8.0'
            wp: 'latest'
            dependency-version: 'highest'
            multisite: '0'
            experimental: false
          # PHP 8.0 / Lowest dependencies
          - php: '8.0'
            wp: 'latest'
            dependency-version: 'lowest'
            multisite: '0'
            experimental: false
          # PHP 8.1
          - php: '8.1'
            wp: 'latest'
            dependency-version: 'highest'
            multisite: '0'
            experimental: false
          # PHP 8.1 / Lowest dependencies
          - php: '8.1'
            wp: 'latest'
            dependency-version: 'lowest'
            multisite: '0'
            experimental: false
          # PHP 8.2
          - php: '8.2'
            wp: 'latest'
            dependency-version: 'highest'
            multisite: '0'
            experimental: true
          # PHP with Imagick
          - php: '7.4'
            wp: 'latest'
            dependency-version: 'highest'
            multisite: '0'
            extensions: 'imagick'
            experimental: false
          # Coverage
          - php: '7.4'
            wp: 'latest'
            dependency-version: 'highest'
            multisite: '0'
            coverage: true
            experimental: false

    name: PHP ${{ matrix.php }} ${{ matrix.extensions == 'imagick' && ' (Imagick)' || '' }}${{ matrix.coverage && ' (with coverage)' || '' }} ${{ matrix.webp && ' (webp)' || '' }} | WP ${{ matrix.wp }}${{ matrix.multisite == 1 && ' (MS)' || '' }} | ${{ matrix.dependency-version }}

    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Install Subversion
        run: |
          sudo apt-get update
          sudo apt-get install -y subversion

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          coverage: ${{ matrix.coverage && 'pcov' || 'none' }}
          tools: composer:v2
          extensions: curl, date, dom, iconv, json, libxml, mysql, ${{ matrix.extensions }}
          ini-values: pcov.directory=lib

      - name: Shutdown default MySQL service
        run: sudo service mysql stop

      - name: Verify MariaDB connection
        run: |
          while ! mysqladmin ping -h"127.0.0.1" -P"${{ job.services.mysql.ports[3306] }}" --silent; do
            sleep 1
          done

      - name: Setup problem matchers for PHP
        run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

      - name: Setup problem matchers for PHPUnit
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      - uses: ramsey/composer-install@v3
        with:
          dependency-versions: ${{ matrix.dependency-version }}

      - name: Downgrade PHPUnit to ^7.5 when WP < 5.9
        if: ${{ matrix.wp < '5.9' }}
        run: composer req phpunit/phpunit:^7.5 yoast/wp-test-utils:^1.0 -W --dev

      - name: Upgrade PHPUnit to ^9.0 when prefer lowest and PHP 8.0+
        if: ${{ matrix.dependency-version == 'lowest' && matrix.php >= '8.0' }}
        run: composer req phpunit/phpunit:^9.0 yoast/wp-test-utils:^1.0 -W --dev

      - name: Install tests
        run: bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1:${{ job.services.mysql.ports['3306'] }} ${{ matrix.wp }} true

    #   - name: Setup PCOV
    #     if: ${{ matrix.coverage == true }}
    #     run: |
    #       composer require --dev --ignore-platform-reqs pcov/clobber
    #       vendor/bin/pcov clobber

      - name: Run tests
        if: ${{ !matrix.webp }}
        run: vendor/bin/phpunit -c phpunit-nocover.xml --verbose --colors=always
        env:
          WP_MULTISITE: ${{ matrix.multisite }}

      - name: Run tests with coverage
        if: ${{ matrix.coverage == true }}
        run: |
          XDEBUG_MODE=coverage vendor/bin/phpunit -c phpunit.xml --verbose --colors=always --coverage-clover ./build/logs/clover.xml

      - name: Upload coverage results to Coveralls
        if: ${{ matrix.coverage == true }}
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          composer global require php-coveralls/php-coveralls
          php-coveralls --coverage_clover=${GITHUB_WORKSPACE}/build/logs/clover.xml -v

      - name: Run webp tests
        if: ${{ matrix.webp }}
        run: vendor/phpunit/phpunit/phpunit -c phpunit.xml --verbose --filter 'TestTimberImageToWEBP'
