name: Test plugin
on: [push, pull_request]

jobs:
  test:
    name: PHP ${{ matrix.php }} / WP ${{ matrix.wp }} Test
    runs-on: ubuntu-latest
    strategy:
      matrix:
        include:
          - php: 7.4
            wp: 6.7
            composer_file: composer-php7.json
          - php: 8.2
            wp: 6.7
            composer_file: composer.json
    steps:
      - name: Checkout
        uses: actions/checkout@master
        
      - name: PHP ${{ matrix.php }} Test
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: mbstring, intl, mysqli
          ini-values: post_max_size=256M, short_open_tag=On
          coverage: xdebug
          tools: composer:v2
          
      - name: Setup Node.js
        uses: actions/setup-node@v3
        with:
          node-version: '22'
          
      - name: Ensure Yarn is installed
        run: |
          if ! command -v yarn &> /dev/null; then
            echo "Yarn not found, installing..."
            npm install -g yarn
          fi
          yarn --version
          
      - name: Install plugin dependencies for PHP 7.4
        if: ${{ matrix.php == '7.4' }}
        run: |
          if [ -f "${{ matrix.composer_file }}" ]; then
            cp ${{ matrix.composer_file }} composer.json
          fi
          composer update --optimize-autoloader --prefer-dist
          
      - name: Install plugin dependencies for PHP 8.2
        if: ${{ matrix.php == '8.2' }}
        run: |
          composer install --optimize-autoloader --prefer-dist
          
      - name: Start WP-ENV
        uses: godaddy-wordpress/setup-wp-env@v1
        with:
          core: 'WordPress/WordPress#${{ matrix.wp }}'
          phpVersion: '${{ matrix.php }}'
        
      - name: Cache Composer packages
        uses: actions/cache@v3
        with:
          path: vendor
          key: ${{ runner.os }}-php-${{ matrix.php }}-${{ hashFiles('**/composer.lock') }}
          restore-keys: |
            ${{ runner.os }}-php-${{ matrix.php }}-
      
      - name: Run tests with PHPUnit 
        run: |
          yarn test
