name: GitHub_CI

on:
  push:
    branches: [ master, develop ]
  pull_request:
    branches: [ master, develop ]

jobs:
  build:
    strategy:
      matrix:
        operating-system: [ubuntu-latest]
        php-versions: ['7.1', '7.2', '7.3', '7.4', '8.0', '8.1']
        mysql-versions: ['mysql:5.7']
    runs-on: ${{ matrix.operating-system }}
    services:
      # Setup MYSQL
      mysql-service:
        image: ${{ matrix.mysql-versions }}
        env:
          MYSQL_ROOT_PASSWORD: 'crab'
          MYSQL_DATABASE: pc_core_tests
        ports:
          - 3306:3306
        options: >-
          --health-cmd="mysqladmin ping"
          --health-interval=10s
          --health-timeout=5s
          --health-retries=3
  
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-versions }}
          extensions: mbstring, intl, pcov
          ini-values: post_max_size=256M, log_errors=1
          tools: pecl

      - name: Check PHP Version
        run: php -v

      - name: Clear existing composer
        run: >
          rm -rf wordpress 
          && rm -rf vendor 
          && rm -rf composer.lock 
      - name: Validate composer.json and composer.lock
        run: composer validate

      - name: Install dependencies
        run: >
          composer clearcache && rm -rf composer.lock 
          && composer install --prefer-dist --no-suggest
      - name: Run Tests on WP5.9
        env:
          environment_github: true
        run: composer all

      - name: Codecov
        run: bash <(curl -s https://codecov.io/bash) -t ${{ secrets.PSR16_CODECOV }}

      - name: Test on WP5.8
        if: ${{ matrix.php-versions != '8.0' && matrix.php-versions != '8.1' }}
        env:
          environment_github: true
        run: > 
          rm -rf wordpress && rm -rf vendor 
          && rm -rf composer.lock 
          && composer require php-stubs/wordpress-stubs:5.8 --dev 
          && composer require roots/wordpress:5.8 --dev 
          && composer require roots/wordpress:5.8 --dev 
          && composer clearcache 
          && rm -rf composer.lock 
          && composer install --prefer-dist --no-suggest 
          && composer all
      - name: Test on WP5.7
        if: ${{ matrix.php-versions != '8.0' && matrix.php-versions != '8.1' }}
        env:
          environment_github: true
        run: > 
          rm -rf wordpress && rm -rf vendor 
          && rm -rf composer.lock 
          && composer require php-stubs/wordpress-stubs:5.7 --dev 
          && composer require roots/wordpress:5.7 --dev 
          && composer require roots/wordpress:5.7 --dev 
          && composer clearcache 
          && rm -rf composer.lock 
          && composer install --prefer-dist --no-suggest 
          && composer all
      - name: Test on WP5.6
        if: ${{ matrix.php-versions != '8.0' && matrix.php-versions != '8.1' }}
        env:
          environment_github: true
        run: > 
          rm -rf wordpress && rm -rf vendor 
          && rm -rf composer.lock 
          && composer require php-stubs/wordpress-stubs:5.6 --dev 
          && composer require roots/wordpress:5.6 --dev 
          && composer require roots/wordpress:5.6 --dev 
          && composer clearcache 
          && rm -rf composer.lock 
          && composer install --prefer-dist --no-suggest 
          && composer all
      - name: Test on WP5.5
        if: ${{ matrix.php-versions != '8.0' && matrix.php-versions != '8.1' }}
        env:
          environment_github: true
        run: > 
          rm -rf wordpress && rm -rf vendor 
          && rm -rf composer.lock 
          && composer require php-stubs/wordpress-stubs:5.5 --dev 
          && composer require roots/wordpress:5.5 --dev 
          && composer require roots/wordpress:5.5 --dev 
          && composer clearcache 
          && rm -rf composer.lock 
          && composer install --prefer-dist --no-suggest 
          && composer all