name: PHPUnit Test (Pest)
on: [ push, pull_request ]
jobs:
    tests:
        strategy:
            fail-fast: true
            matrix:
                operating-system:
                    - ubuntu-24.04
                php-version:
                    - '8.5'
        name: php ${{ matrix.php-version }} on ${{ matrix.operating-system }}
        runs-on: ${{ matrix.operating-system }}
        services:
            mysql:
                image: mysql:8.0
                env:
                    MYSQL_ALLOW_EMPTY_PASSWORD: yes
                    MYSQL_DATABASE: unit3d
                ports:
                    - 3306:3306
                options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
            redis:
                image: redis:7.2.1
                ports:
                    - 6379:6379
                options: >-
                    --health-cmd "redis-cli ping"
                    --health-interval 10s
                    --health-timeout 5s
                    --health-retries 5
        steps:
            # 1. Checkout
            -   name: Checkout
                uses: actions/checkout@v6
                with:
                    fetch-depth: 0

            # 2. Setup
            -   uses: ./.github/actions/setup
                with:
                    php-version: ${{ matrix.php-version }}
                    operating-system: ${{ matrix.operating-system }}

            # 3. PHPUnit Tests
            -   name: Restore PHPUnit cache
                uses: actions/cache/restore@v4
                with:
                    path: .phpunit.cache
                    key: unit3d-${{ matrix.operating-system }}-php${{ matrix.php-version }}-phpunit-${{ github.run_id }}
                    restore-keys: |
                        unit3d-${{ matrix.operating-system }}-php${{ matrix.php-version }}-phpunit-

            -   name: Run PHPUnit tests
                run: COLUMNS=200 ./vendor/bin/pest --compact --parallel --processes=4
                env:
                    DB_CONNECTION: mysql
                    DB_PORT: ${{ job.services.mysql.ports['3306'] }}
                    DB_USERNAME: root
                    DB_DATABASE: unit3d
                    DB_PASSWORD: null
                    CACHE_STORE: array

            -   name: Save PHPUnit cache
                uses: actions/cache/save@v4
                if: always()
                with:
                    path: .phpunit.cache
                    key: unit3d-${{ matrix.operating-system }}-php${{ matrix.php-version }}-phpunit-${{ github.run_id }}
