name: Tests
on:
  push:
    branches: [main]
  pull_request:
    branches: [main]
  schedule:
    - cron: 0 0 * * *

jobs:
  lint:
    runs-on: "ubuntu-latest"
    name: "Linting | PHP ${{ matrix.php-version }}"
    strategy:
      matrix:
        php-version:
          - "7.4"
          - "8.0"
          - "8.1"
    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2"
      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "none"
          php-version: "${{ matrix.php-version }}"
          tools: composer:v2
          extensions: dom, curl, libxml, mbstring, zip, pdo, mysql, pdo_mysql, gd
      - name: "Install dependencies"
        run: "composer update --no-progress --prefer-dist"
      - name: "PHPCS"
        run: "php vendor/bin/phpcs"
      - name: "PHPStan"
        run: "php vendor/bin/phpstan analyze"
  tests:
    continue-on-error: ${{ matrix.experimental }}
    runs-on: "ubuntu-latest"
    name: "Tests | PHP ${{ matrix.php-version }} | Drupal ${{ matrix.drupal }}"
    strategy:
      matrix:
        experimental: [false]
        php-version:
          - "7.4"
        drupal:
          - "^8.9"
          - "^9.0"
        include:
          - php-version: "8.0"
            drupal: "^9.0"
            experimental: true
          - php-version: "8.1"
            drupal: "10.0.x-dev"
            experimental: true
    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2"
      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "xdebug"
          php-version: "${{ matrix.php-version }}"
          tools: composer:v2
          extensions: dom, curl, libxml, mbstring, zip, pdo, mysql, pdo_mysql, gd
      - name: "Downgrade dev dependencies"
        run: "composer require phpunit/phpunit:6.5.14 drush/drush:~9 drupal/core-recommended:${{ matrix.drupal }} drupal/core-dev:${{ matrix.drupal }} --with-all-dependencies --dev --no-update"
        if: ${{ matrix.drupal == '^8.9' }}
      - name: "Upgrade to drupal/core:10.0.x and drush/drush:^11.0"
        run: "composer require drupal/core-recommended:${{ matrix.drupal }} drush/drush:^11.0 --with-all-dependencies --dev --no-update"
        if: ${{ matrix.drupal == '10.0.x-dev' }}
      - name: "Add phpspec/prophecy-phpunit"
        run: "composer require phpspec/prophecy-phpunit:^2 --dev --no-update"
        if: ${{ matrix.drupal != '^8.9' }}
      - name: "Install dependencies"
        run: "composer update --no-progress --prefer-dist"
      - name: "PHPUnit"
        run: "php vendor/bin/phpunit"

  build_integration:
    needs:
      - lint
      - tests
    continue-on-error: ${{ matrix.experimental }}
    runs-on: "ubuntu-latest"
    name: "Build Integration | PHP ${{ matrix.php-version }} | Drupal ${{ matrix.drupal }}"
    strategy:
      matrix:
        experimental: [false]
        php-version:
          - "7.4"
        drupal:
          - "^8.9"
          - "^9.0"
        include:
          - php-version: "8.0"
            drupal: "^9.0"
            experimental: true
          - php-version: "8.1"
            drupal: "10.0.x-dev"
            experimental: true
    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2"
      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "none"
          php-version: "${{ matrix.php-version }}"
          tools: composer:v2
          extensions: dom, curl, libxml, mbstring, zip, pdo, mysql, pdo_mysql, bcmath, gd, exif, iconv
      - name: Setup Drupal
        uses: bluehorndigital/setup-drupal@v1.0.4
        with:
          version: ${{ matrix.drupal }}
          path: ~/drupal
      - name: "set the version alias for self"
        run: |
          if [ "${{ github.event_name }}" == 'pull_request' ]; then
            echo ::set-output name=VERSION_ALIAS::dev-"${{ github.sha }}"
          else
            echo ::set-output name=VERSION_ALIAS::dev-main
          fi
        id: branch_alias
      - name: "require phpstan-drupal"
        run: |
          cd ~/drupal
          COMPOSER_MEMORY_LIMIT=-1 composer require mglaman/phpstan-drupal "${{ steps.branch_alias.outputs.VERSION_ALIAS }} as 1.1.99" phpstan/extension-installer --with-all-dependencies
          cp $GITHUB_WORKSPACE/tests/fixtures/config/drupal-phpstan.neon phpstan.neon
      - name: "Test core/install.php"
        run: |
          cd ~/drupal
          ./vendor/bin/phpstan analyze web/core/install.php --debug
      - name: "Test BrowserTestBase is autoloaded"
        run: |
          cd ~/drupal
          ./vendor/bin/phpstan analyze web/core/modules/dynamic_page_cache | grep -q "Class Drupal\Tests\BrowserTestBase not found and could not be autoloaded." && false || true
      - name: "Verify test fixtures are ignored."
        run: |
          cd ~/drupal
          ./vendor/bin/phpstan analyze web/core/modules/migrate_drupal --no-progress | grep -q "tests/fixtures" && false || true
      - name: 'Check "Cannot redeclare token_theme() due to blazy_test.module"'
        if: ${{ matrix.drupal != '10.0.x-dev' }}
        run: |
          cd ~/drupal
          COMPOSER_MEMORY_LIMIT=-1 composer require drupal/token drupal/blazy
          ./vendor/bin/phpstan analyze web/modules/contrib/blazy --no-progress || if (($? == 255)); then false; else true; fi
          COMPOSER_MEMORY_LIMIT=-1 composer remove drupal/token drupal/blazy
      - name: 'Check "Cannot redeclare video_embed_media_media_bundle_insert()"'
        if: ${{ matrix.drupal != '10.0.x-dev' }}
        run: |
          cd ~/drupal
          COMPOSER_MEMORY_LIMIT=-1 composer require drupal/video_embed_field drupal/slick
          ./vendor/bin/phpstan analyze web/modules/contrib --no-progress || if (($? == 255)); then false; else true; fi
          COMPOSER_MEMORY_LIMIT=-1 composer remove drupal/video_embed_field drupal/slick
  build_integration_no_phpunit:
    needs:
      - lint
      - tests
    continue-on-error: ${{ matrix.experimental }}
    runs-on: "ubuntu-latest"
    name: "Build Integration (No PHPUnit) | PHP ${{ matrix.php-version }} | Drupal ${{ matrix.drupal }}"
    strategy:
      matrix:
        experimental: [false]
        php-version:
          - "7.4"
        drupal:
          - "^8.9"
          - "^9.0"
        include:
          - php-version: "8.1"
            drupal: "10.0.x-dev"
            experimental: true
    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2"
      - name: "Install PHP"
        uses: "shivammathur/setup-php@v2"
        with:
          coverage: "none"
          php-version: "${{ matrix.php-version }}"
          tools: composer:v2
          extensions: dom, curl, libxml, mbstring, zip, pdo, mysql, pdo_mysql, bcmath, gd, exif, iconv
      - name: Setup Drupal
        uses: bluehorndigital/setup-drupal@v1.0.4
        with:
          version: ${{ matrix.drupal }}
          path: ~/drupal
      - name: "Remove PHPUnit"
        run: |
          cd ~/drupal
          composer --dev remove phpspec/prophecy-phpunit drupal/core-dev
      - name: "require phpstan-drupal"
        run: |
          cd ~/drupal
          COMPOSER_MEMORY_LIMIT=-1 composer require mglaman/phpstan-drupal *@dev
          cp $GITHUB_WORKSPACE/tests/fixtures/config/drupal-no-dev-phpstan.neon phpstan.neon
      - name: "Test core/install.php"
        run: |
          cd ~/drupal
          ./vendor/bin/phpstan analyze web/core/install.php --debug
      - name: "Test no crash"
        run: |
          cd ~/drupal
          ./vendor/bin/phpstan analyze web/core/modules/dynamic_page_cache --debug
