name: Testing with OXID eShop-CE

on:
  push:
  pull_request:
  workflow_dispatch:
  schedule:
    - cron: '0 6 * * 1'

jobs:
  run_tests:
    strategy:
      matrix:
        php: [ '8.1' ]
        mysql: [ '8.0' ]
      fail-fast: false
    runs-on: ubuntu-latest
    env:
      ESHOP_DOCKER_SDK_REPOSITORY: https://github.com/OXID-eSales/docker-eshop-sdk.git
      SHOP_CE_REPOSITORY: https://github.com/OXID-eSales/oxideshop_ce.git
      SHOP_CE_DEFAULT_BRANCH: b-7.1.x
      SHOP_INSTALL_DIR: eshop_ce
      DOCKER_SETUP_DIR: source
    steps:
      - name: Add environment variables
        run: |
          SHOP_ROOT_DIR="/var/www/$SHOP_INSTALL_DIR"
          RUN_PHP="docker-compose run -T -w $SHOP_ROOT_DIR php"
          TWIG_ROOT="/var/www/$SHOP_INSTALL_DIR/vendor/oxid-esales/twig-component"
          echo "SHOP_ROOT_DIR=$SHOP_ROOT_DIR" >> $GITHUB_ENV
          echo "RUN_PHP=$RUN_PHP" >> $GITHUB_ENV
          echo "TWIG_ROOT=$TWIG_ROOT" >> $GITHUB_ENV

      - name: Clone Docker Eshop SDK
        run: |
          git clone --depth 1 $ESHOP_DOCKER_SDK_REPOSITORY --branch master --single-branch .

      - name: Define eShop-CE branch for cloning
        run: |
          if [[ $(git ls-remote --heads $SHOP_CE_REPOSITORY ${{ github.ref_name }}) ]]; then
            SHOP_CE_BRANCH=${{ github.ref_name }};
          else
            SHOP_CE_BRANCH=$SHOP_CE_DEFAULT_BRANCH;
          fi
          echo "SHOP_CE_BRANCH=$SHOP_CE_BRANCH" >> $GITHUB_ENV

      - name: Clone the shop
        run: |
          git clone --depth 1 $SHOP_CE_REPOSITORY --branch $SHOP_CE_BRANCH --single-branch $DOCKER_SETUP_DIR/$SHOP_INSTALL_DIR

      - name: Prepare container configuration
        run: |
          make setup
          make addbasicservices
          perl -pi\
              -e "s#PHP_VERSION=.*#PHP_VERSION='${{ matrix.php }}'#g;"\
              -e "s#MYSQL_VERSION=.*#MYSQL_VERSION='${{ matrix.mysql }}'#g;"\
              .env
          perl -pi\
              -e 's#display_errors =.*#display_errors = false#g;'\
              -e 'print "xdebug.max_nesting_level=1000\n\n";'\
              -e 'print "error_reporting = E_ALL & ~E_DEPRECATED & ~E_NOTICE & ~E_WARNING\n"'\
              containers/php/custom.ini
          perl -pi\
              -e 's#/var/www/#$SHOP_ROOT_DIR/source/#g;'\
              containers/httpd/project.conf

      - name: Start containers
        run: |
          make up
          $RUN_PHP \
            composer config preferred-install.oxid-esales/* source

      - name: Require twig component by tag
        if: github.ref_type == 'tag'
        run: |
          $RUN_PHP \
            composer require oxid-esales/twig-component:${{ github.ref_name }} --no-update

      - name: Require twig component (${{ github.ref_name }})
        if: github.ref_type != 'tag'
        run: |
          $RUN_PHP \
            composer require oxid-esales/twig-component:dev-${{ github.ref_name }} --no-update

      - name: Run Composer install
        run: |
          $RUN_PHP \
            composer require squizlabs/php_codesniffer
          
          $RUN_PHP \
            composer install --no-interaction

      - name: Prepare for PHP_CS check
        run: |
          cd source/eshop_ce/vendor/oxid-esales/twig-component
          git diff --name-only origin/${{ github.ref_name }} HEAD~1 | grep '\.php$' >changed-files.txt || true 
          if [[ -f "changed-files.txt" && -s "changed-files.txt" ]]; then
             perl -pi -e "s#^#$TWIG_ROOT/#" changed-files.txt
             if [[ -f  "changed-files.txt" && -s "changed-files.txt" ]]; then
               cd ../../../../../ 
               $RUN_PHP \
                vendor/bin/phpcs \
                --standard=psr12 \
                --ignore=tests/*/Fixtures/* \
                --exclude=Generic.Files.LineLength \
                --file-list=$TWIG_ROOT/changed-files.txt 2>&1 \
                | tee php_codesniff_log.txt  || true
               rm source/eshop_ce/vendor/oxid-esales/twig-component/changed-files.txt
               if grep -q -Ei 'fail|\\.\\=\\=|Warning|Notice|Deprecated|Fatal|Error' php_codesniff_log.txt; 
                  then exit 1; 
               fi
             fi
          fi

      - name: Run Install Shop
        run: |
          $RUN_PHP \
            cp source/config.inc.php.dist source/config.inc.php
          $RUN_PHP \
          bin/oe-console oe:setup:shop \
          --db-host mysql \
          --db-port 3306 \
          --db-name example \
          --db-user root \
          --db-password root \
          --shop-url localhost.localhost \
          --shop-directory $SHOP_ROOT_DIR/source/ \
          --compile-directory $SHOP_ROOT_DIR/source/tmp/

      - name: Run component tests
        # Put PHPUnit logs to the eShop log folder
        run: |
          $RUN_PHP \
            php vendor/bin/phpunit \
          --configuration phpunit.xml \
          --bootstrap source/bootstrap.php \
          vendor/oxid-esales/twig-component/tests \
          --testdox-text source/log/phpunit_log.txt

      - name: Upload logged artifacts
        uses: actions/upload-artifact@v3
        with:
          name: eshop_source_log_folder
          path: ${{ env.DOCKER_SETUP_DIR }}/${{ env.SHOP_INSTALL_DIR }}/source/log/
          retention-days: 7
          if-no-files-found: error

      - name: Stop containers
        run: |
          make down
