name: PsAccounts Installer Quality Control PHP

on: [push]

env:
  PHP_VERSION: 7.4

jobs:
  php-linter:
    name: PHP Syntax check
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: PHP syntax checker 5.6
        uses: prestashop/github-action-php-lint/5.6@master

      - name: PHP syntax checker 7.4
        uses: prestashop/github-action-php-lint/7.4@master

      - name: PHP syntax checker 8.0
        uses: prestashop/github-action-php-lint/8.0@master

      - name: PHP syntax checker 8.1
        uses: prestashop/github-action-php-lint/8.1@master

  php-cs-fixer:
    name: PHP-CS-FIXER
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Run PHP-CS-Fixer
        uses: prestashopcorp/github-action-php-cs-fixer@master

  phpstan:
    name: PHPSTAN
    runs-on: ubuntu-latest
    strategy:
      matrix:
        presta-versions: ['latest', '8.0', '1.7', '1.7.0.3', '1.6.1.21', '1.6.1.0']
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          php-version: ${{ env.PHP_VERSION }}

      - name: Cache vendor folder
        uses: actions/cache@v3
        with:
          path: vendor
          key: php-${{ hashFiles('composer.lock') }}

      - name: Cache composer folder
        uses: actions/cache@v3
        with:
          path: ~/.composer/cache
          key: php-composer-cache

      - name: git clone ps_accounts
        run: |
          git clone https://${{ secrets.ACCESS_TOKEN }}@github.com/PrestaShopCorp/ps_accounts.git ~/ps_accounts
          cd ~/ps_accounts

      - name: composer install
        run: composer install --prefer-dist && cd ~/ps_accounts && composer install --prefer-dist

      - name: Pull PrestaShop files (Tag ${{ matrix.presta-versions }})
        run: docker run -tid --rm -v ps-volume:/var/www/html --name temp-ps prestashop/prestashop:${{ matrix.presta-versions }}

      - name: Select .neon file to run with PHPStan
        id: neon
        run: |
          PS_VERSION=$(docker exec temp-ps bash -c 'echo "$PS_VERSION"')
          [[ "${PS_VERSION:0:3}" != '1.7' ]] && echo ::set-output name=filename::phpstan-PS-1.6.neon || echo ::set-output name=filename::phpstan-PS-1.7.neon

      - name : Run PHPStan
        run: docker run --rm --volumes-from temp-ps -v $PWD:/web/module -v ~/ps_accounts:/web/ps_accounts -e _PS_ROOT_DIR_=/var/www/html --workdir=/web/module phpstan/phpstan:0.12 analyse --configuration=/web/module/tests/phpstan/${{steps.neon.outputs.filename}}

  phpunit:
    name: PHPUNIT
    runs-on: ubuntu-latest
    steps:
    - name: checkout project
      uses: actions/checkout@v3

    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: ${{ env.PHP_VERSION }}
        coverage: xdebug

    - name: Cache vendor folder
      uses: actions/cache@v3
      with:
        path: vendor
        key: php-${{ hashFiles('composer.lock') }}

    - name: composer install missing files
      run: composer install --prefer-dist

    - name: PHPUnit tests
      run: composer test
