name: Run Phar tests

on:
  workflow_call:

jobs:
  test:
    name: PHP ${{ matrix.php }}
    runs-on: ubuntu-latest

    strategy:
      fail-fast: false
      matrix:
        php: ['7.4', '8.0', '8.1', '8.2', '8.3', '8.4', '8.5']

    steps:
      - name: Check out code
        uses: actions/checkout@v6.0.2
        with:
          sparse-checkout-cone-mode: false
          sparse-checkout: |
            .github
            build/composer.json
            composer.json
            test

      - name: Install PHP
        uses: shivammathur/setup-php@2.37.0
        with:
          php-version: ${{ matrix.php }}
          tools: composer:v2
          coverage: none

      - name: Download build artifacts
        uses: actions/download-artifact@v8.0.1
        with:
          name: binaries
          path: build

      - name: Make binaries executable
        run: chmod +x build/*/psysh

      - name: Smoke test
        run: test/smoketest.sh build/*/psysh

      - name: Set up problem matchers
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      - name: Composer init
        run: |
          mkdir -p .phpunit
          composer --working-dir .phpunit init --no-interaction --name=psy/test

      - name: Install PHPUnit
        uses: nick-fields/retry@v4.0.0
        with:
          timeout_minutes: 5
          max_attempts: 5
          command: composer --working-dir .phpunit require --no-interaction --no-progress "phpunit/phpunit:^9.6"

      - name: Run test suite
        run: |
          tmp_dir="$(mktemp -d)"
          cp -r test "${tmp_dir}/"
          cp build/*/psysh "${tmp_dir}/psysh"
          cp composer.json "${tmp_dir}/"
          mkdir -p "${tmp_dir}/build"
          cp build/composer.json "${tmp_dir}/build/"
          ln -sfn "${GITHUB_WORKSPACE}/.phpunit/vendor" "${tmp_dir}/vendor"
          (
            cd "${tmp_dir}" && \
            php -d memory_limit=1G vendor/bin/phpunit --bootstrap test/bootstrap-phar.php --exclude-group isolation-fail test/
          )
