name: Automated-Testing

on:
  schedule:
    - cron:  '0 4 * * 5'
  push:
    branches:
      - develop
      - master
  pull_request:
    branches:
      - develop
    paths:
      - '**.php'
      - '!docs/**'

jobs:
  Automated-Testing:
    env:
      STRIPE_API_PUBLISHABLE_KEY: ${{ secrets.STRIPE_API_PUBLISHABLE_KEY }}
      STRIPE_API_SECRET_KEY: ${{ secrets.STRIPE_API_SECRET_KEY }}
    runs-on: ubuntu-latest
    strategy:
      matrix:
        php: ['8.3', '8.2', '8.1', '7.4']
        wordpress: ['6.7', '6.3', '6.1']
        composer_version: ['v2']
        include:
          - php: '8.3'
            wordpress: '6.7'
            coverage: '--coverage --coverage-xml'
            xdebug: 1
          - php: '8.1'
            wordpress: '6.1'
            debug: '--debug'
          - wordpress: '6.3'
            hpos: 1

      fail-fast: false
    name: Running Automated Tests w/ WPBrowser in WordPress v${{ matrix.wordpress }} on PHP${{ matrix.php }} ${{ matrix.hpos == 1 && 'with HPOS activated ' || '' }}${{ matrix.coverage == '--coverage --coverage-xml' && 'with Codecoverage ' || '' }}${{ matrix.debug == '--debug' && 'in Debug Mode ' || '' }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Install PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: json, mbstring
          tools: composer

      - name: Install dependencies
        run: |
          cp .env.testing .env
          rm -rf composer.lock
          composer install
          WP_VERSION=${{ matrix.wordpress }} SKIP_DB_CREATE=true SKIP_WP_SETUP=true composer installTestEnv

      - name: Run Tests.
        env:
          USING_XDEBUG: ${{ matrix.xdebug }}
          WP_VERSION: ${{ matrix.wordpress }}
          PHP_VERSION: ${{ matrix.php }}
        run: HPOS="${{ matrix.hpos }}" FILTER="${{ matrix.coverage }} ${{ matrix.debug }}" composer dRunTestStandalone

      - name: Push Codecoverage to Coveralls.io
        if: ${{ matrix.coverage == '--coverage --coverage-xml' && env.STRIPE_API_PUBLISHABLE_KEY != null }}
        env:
          COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          composer global require php-coveralls/php-coveralls
          php-coveralls -v

      - name: Test & publish code coverage
        if: ${{ matrix.coverage == '--coverage --coverage-xml' }}
        uses: paambaati/codeclimate-action@v2.7.5
        env:
          CC_TEST_REPORTER_ID: 739347fbfc0caa4e7f25069899203df2d4a411b3cbc9c3b1ef28257520c99d31
        with:
          coverageLocations: |
            ${{github.workspace}}/tests/_output/*.xml:clover
