name: PHPUnit

on:
  push:
    branches:
      - main
      - improve_tests
  pull_request:

env:
  WP_TESTS_DIR: /workspace/wordpress-test-lib
  WP_CORE_DIR: /workspace/wordpress

jobs:
  phpunit:
    runs-on: ubuntu-latest
    services:
      mysql:
        image: mariadb
        env:
          MYSQL_ROOT_PASSWORD: root
    strategy:
      matrix:
        php-version: ['8.1', '8.2', '8.3']
    name: PHPUnit – PHP ${{ matrix.php-version }}
    env:
      extensions: mysql
      key: cache-v1
    steps:
      - name: Checkout
        uses: https://code.forgejo.org/actions/checkout@v4

      - name: Cache WordPress Setup
        id: cache-wordpress
        uses: https://code.forgejo.org/actions/cache@v4
        with:
          path: |
            ${{ env.WP_CORE_DIR }}
            ${{ env.WP_TESTS_DIR }}
          key: cache-wordpress-9

      - name: Cache Composer
        id: cache-composer-phpunit
        uses: https://code.forgejo.org/actions/cache@v4
        with:
          path: |
            ./vendor/
          key: cache-composer-phpunit-1

      - name: Setup PHP
        uses: https://github.com/shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php-version }}
          coverage: none
          tools: composer
        env:
          runner: self-hosted

      - name: Install PHPUnit and PHPUnit-Polyfills
        if: steps.cache-composer-phpunit.outputs.cache-hit != 'true'
        run: composer require --dev yoast/phpunit-polyfills:"^3.0"

      - name: Install and cache mysqladmin needed to initialize the test database
        uses: https://github.com/awalsh128/cache-apt-pkgs-action@latest
        with:
          packages: mysql-client
          version: 1.0

      - name: Setup Test Environment
        if: steps.cache-wordpress.outputs.cache-hit != 'true'
        run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false false false false

      - name: Initialize WordPress test database
        if: steps.cache-wordpress.outputs.cache-hit != 'false'
        run: bash bin/install-wp-tests.sh wordpress_test root root 127.0.0.1 6.6 false true true true

      - name: Run Integration tests for The Events Calendar
        run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=the_events_calendar
        env:
          PHP_VERSION: ${{ matrix.php-version }}

      - name: Run Integration tests for VS Event List
        run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=vs_event_list
        env:
          PHP_VERSION: ${{ matrix.php-version }}

      - name: Run Integration tests for GatherPress
        run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=gatherpress
        env:
          PHP_VERSION: ${{ matrix.php-version }}

      - name: Run Integration tests for Events Manager
        run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=events_manager
        env:
          PHP_VERSION: ${{ matrix.php-version }}

      - name: Run Integration tests for WP Event Manager
        run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=wp_event_manager
        env:
          PHP_VERSION: ${{ matrix.php-version }}

      - name: Run Integration tests for Eventin (WP Event Solution)
        run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=eventin
        env:
          PHP_VERSION: ${{ matrix.php-version }}

      - name: Run Integration tests for Modern Events Calendar Lite
        run: cd /workspace/Event-Federation/wordpress-activitypub-event-bridge/ && ./vendor/bin/phpunit --filter=modern_events_calendar_lite
        env:
          PHP_VERSION: ${{ matrix.php-version }}