name: PHP Tests Reusable Workflow

# https://docs.github.com/en/actions/using-workflows/reusing-workflows#creating-a-reusable-workflow
on:
  # Reusable workflow
  workflow_call:
    # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#inputs
    inputs:
      versions:
        description: 'Version of the interpreter'
        required: true
        default: "['8.2']"
        type: string

permissions: # Setting permissions for the token
  contents: read # needed to fetch code


# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#defaults
# Set the default shell for a run
defaults:
  run:
    shell: bash

# A list of the jobs that run in the workflow file.
jobs:
  test: # The identifier of the job

    name: Test on php ${{ matrix.php }} # The name of the job

    runs-on: ubuntu-latest

    strategy:
      # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix
      matrix:
         # 2 jobs will run, one for each include entry
         # because we don't specify any matrix variables
         # All configurations under include will run
         # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-adding-configurations
         php: ${{ fromJson(github.event.inputs.versions) }}
      fail-fast: false

    env:
      COMBO_HOME: lib/plugins/combo

    steps:

      - name: Phpunit ${{ matrix.php }}
        run: |
          echo "PHPUNIT_VERSION=${{ fromJson(env.PHPUNIT_BY_PHP)[matrix.php] }}" >> $GITHUB_ENV
        env:
          PHPUNIT_BY_PHP: '{"7.4":"8.5.33","8.2":"8.5.33"}'

      # https://github.com/marketplace/actions/setup-php-action#matrix-setup
      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ matrix.php }}
          extensions: mbstring, intl, PDO, pdo_sqlite, pdo_mysql, pdo_pgsql, bz2
          ini-values: pcre.jit=0
          tools: phpunit:${{ env.PHPUNIT_VERSION }}

      # Php Problem Matchers
      # https://github.com/marketplace/actions/setup-php-action#problem-matchers
      # Problem matchers are json configurations which identify errors and warnings in your logs
      # and surface them prominently in the GitHub Actions UI by highlighting them and creating code annotations.
      - name: Setup problem matchers for PHP
        run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"
      - name: Setup problem matchers for PHPUnit
        run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

      # Cloning Dokuwiki
      - name: Checkout Dokuwiki
        uses: actions/checkout@v3
        with:
          repository: dokuwiki/dokuwiki

      # Cloning this repository to the runner
      # https://github.com/actions/checkout
      - name: Checkout Combo
        uses: actions/checkout@v3
        with:
          path: ${{ env.COMBO_HOME }}


      # Runs command-line programs using the operating system's shell.
      # https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
      - name: Post installation (Download Requirements)
        run: |
          chmod +x ${COMBO_HOME}/.github/bootstrap.sh
          source ${COMBO_HOME}/.github/bootstrap.sh
        # https://docs.github.com/en/actions/security-guides/encrypted-secrets#using-encrypted-secrets-in-a-workflow
        env:
          TOKEN: ${{ secrets.TOKEN }}
        working-directory: .
        shell: bash

      # Node
      # https://github.com/actions/setup-node/issues/160#issuecomment-642739512
      # We don't specify the version to get the one that is on the image
      # otherwise you get a time-out as it tries to download it and failed
      - uses: actions/setup-node@v3
#        with:
#          node-version: latest

      # Yarn (should happen after combo as we install in combo)
      - name: Yarn install
        run: |
          npm install --global yarn
          cd ${COMBO_HOME} && yarn install

      # Get the list of locales
      - name: Installed Locale
        run: locale -a

      # Composer is used since Kaos version (06/02/2024)
      - name: Setup PHPUnit
        run: |
          cd _test
          composer install --no-interaction --no-progress --no-suggest --prefer-dist

      # No better formatter
      # This one does not work: https://github.com/mheap/phpunit-matcher-action
      # with verbose, you see the configuration file used at the beginning
      - name: Test
        run: |
          phpunit --version
          phpunit --stderr --configuration _test/phpunit.xml --verbose --debug --bootstrap ${COMBO_HOME}/_test/bootstrap.php ${COMBO_HOME}/_test
