name: "CI"

on:
  pull_request:

jobs:
  cs:
    name: "Coding Style"
    runs-on: ubuntu-latest

    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2"
        with:
          fetch-depth: 2

      - name: "Install PHP 8.1"
        uses: "shivammathur/setup-php@v2"
        with:
          php-version: "8.1"

      - name: "List modified files"
        id: diff
        run: |
          git fetch origin
          git diff --name-only --diff-filter=AM origin/${{ github.base_ref }} > git_diff_files.txt
          echo "::set-output name=diff_php::$(cat git_diff_files.txt | grep '.\+\.php' | sed ':a;N;$!ba;s/\n/ /g')"
          echo "::set-output name=diff_js::$(cat git_diff_files.txt | grep 'Resources/modules/.\+\.js' | sed ':a;N;$!ba;s/\n/ /g')"

      - name: "Install PHPMD"
        if: ${{ env.DIFF != '' }}
        run: wget -c https://phpmd.org/static/latest/phpmd.phar -O phpmd;
        env:
          DIFF: ${{ steps.diff.outputs.diff_php}}

      - name: "Run PHPMD checks"
        if: ${{ env.DIFF != '' }}
        run: php phpmd `echo "$DIFF" | tr ' ' ','` text phpmd.xml --minimum-priority 1
        env:
          DIFF: ${{ steps.diff.outputs.diff_php }}

      - name: "Install PHPCSFixer"
        if: ${{ env.DIFF != '' }}
        run: wget https://cs.symfony.com/download/php-cs-fixer-v3.phar -O php-cs-fixer
        env:
          DIFF: ${{ steps.diff.outputs.diff_php }}

      - name: "Run PHPCSFixer checks"
        if: ${{ env.DIFF != '' }}
        run: php php-cs-fixer fix --dry-run --diff --config=.php-cs-fixer.php --path-mode=intersection `echo "$DIFF"`
        env:
          PHP_CS_FIXER_IGNORE_ENV: 1
          DIFF: ${{ steps.diff.outputs.diff_php }}

      - name: "Install ESLint"
        if: ${{ env.DIFF != '' }}
        run: npm install eslint eslint-plugin-react@7.21.5 --legacy-peer-deps
        env:
          DIFF: ${{ steps.diff.outputs.diff_js }}

      - name: "Run ESLint checks"
        if: ${{ env.DIFF != '' }}
        run: node_modules/.bin/eslint --ext js --ext jsx `echo "$DIFF"`
        env:
          DIFF: ${{ steps.diff.outputs.diff_js }}

  php-81:
    name: "PHPUnit (PHP 8.1)"
    needs: cs
    runs-on: ubuntu-latest

    services:
      mysql:
        image: mysql:8.0
        options: >-
          --health-cmd "mysqladmin ping --silent"
          -e MYSQL_ALLOW_EMPTY_PASSWORD=yes
          -e MYSQL_DATABASE=claroline_test
        ports:
          - "3306:3306"

    steps:
      - name: "Checkout"
        uses: "actions/checkout@v2"
        with:
          fetch-depth: 2

      - name: "Install PHP 8.1"
        uses: "shivammathur/setup-php@v2"
        with:
          php-version: "8.1"
          extensions: pdo_mysql

      - name: "Cache composer dependencies"
        uses: actions/cache@v1
        with:
          path: "~/.composer/cache"
          key: composer-${{ hashFiles('**/composer.json') }}
          restore-keys: composer-

      - name: "Set project parameters"
        run: php bin/configure --default

      - name: "Install PHP dependencies with Composer"
        run: composer install --no-interaction

      - name: "Cache node modules"
        uses: actions/cache@v2
        with:
          path: ~/.npm
          key: npm-${{ hashFiles('**/package.json') }}
          restore-keys: |
            npm-

      - name: "Install JS dependencies with NPM"
        run: npm install --legacy-peer-deps

      - name: "Build JS files with Webpack"
        run: npm run webpack

      - name: "Setup Claroline platform"
        run: php bin/console claroline:install --env=test -vvv

      - name: "Run PHPUnit Tests"
        run: php bin/phpunit --dont-report-useless-tests

      # Created user is not used in the job. This is just a dumb way to test the user creation process.
      # We do it after tests because we don't want tests on user parts to be affected by this.
      - name: "Create a Claroline user"
        run: php bin/console claroline:user:create --env=test -vvv -a John Doe john.doe john.doe john.doe@test.com
