name: Shop tests with a module

on:
  workflow_dispatch:
  workflow_call:

env:
  PACKAGE_NAME: 'eyeable/eye-able-oxid'
  MODULE_IDS: 'eyeable_assist'

jobs:
  install_shop_with_module:
    strategy:
      matrix:
        php: [ '8.2' ]
    runs-on: ubuntu-latest
    steps:
      - name: Clone testing environment
        run: git clone https://github.com/OXID-eSales/docker-eshop-sdk.git .

      - name: Clone the shop
        run: git clone --depth 1 https://github.com/OXID-eSales/oxideshop_ce.git --branch b-7.1.x --single-branch source

      - name: Cache current installation
        uses: actions/cache@v3
        with:
          path: |
            ./*
          key: shopInstallation-${{ github.sha }}-${{ join(inputs.*, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}

      - name: Prepare container configuration
        run: |
          make setup
          make addbasicservices

          perl -pi\
            -e "s#PHP_VERSION=.*#PHP_VERSION=${{ matrix.php }}#g;"\
            .env

          perl -pi\
            -e 's#display_errors =.*#display_errors = false#g;'\
            -e 's#error_reporting = .*#error_reporting = E_ALL ^ E_WARNING ^ E_DEPRECATED#g;'\
            -e 'print "xdebug.max_nesting_level=1000\nxdebug.mode=coverage\n\n"'\
            containers/php/custom.ini

          perl -pi\
            -e 's#/var/www/#/var/www/source/#g;'\
            containers/httpd/project.conf

      - name: Prepare shop configuration
        run: |
          cp source/source/config.inc.php.dist source/source/config.inc.php
          perl -pi\
            -e 's#<dbHost>#mysql#g;'\
            -e 's#<dbUser>#root#g;'\
            -e 's#<dbName>#example#g;'\
            -e 's#<dbPwd>#root#g;'\
            -e 's#<dbPort>#3306#g;'\
            -e 's#<sShopURL>#http://localhost.local/#g;'\
            -e 's#<sShopDir>#/var/www/source/#g;'\
            -e 's#<sCompileDir>#/var/www/source/tmp/#g;'\
            source/source/config.inc.php

      - name: Checkout current module
        uses: actions/checkout@v4
        with:
          path: source/test-module

      - name: Start containers
        run: |
          make up
          sleep 2

      - name: Install module
        run: |
          docker-compose exec -T \
            php composer config repositories.${{ env.PACKAGE_NAME }} \
            --json '{"type":"path", "url":"./test-module", "options": {"symlink": true}}'
          docker-compose exec -T \
            php composer require ${{ env.PACKAGE_NAME }}:* --no-interaction --no-update

      - name: Install Twig templates
        run: |
          docker-compose exec -T php composer require oxid-esales/twig-admin-theme:dev-b-7.1.x --no-update
          docker-compose exec -T php composer require oxid-esales/apex-theme:dev-b-7.1.x --no-update

      - name: Install dependencies and reset shop
        run: |
          docker-compose exec -T php composer update --no-interaction
          docker-compose exec -T php bin/oe-console oe:database:reset \
            --db-host=mysql --db-port=3306 --db-name=example --db-user=root --db-password=root --force
          docker-compose exec -T php bin/oe-console oe:module:activate eyeable_assist

      - name: Stop containers
        run: |
          docker-compose down
          sleep 2

      - name: Show docker log
        if: always()
        run: |
          docker-compose logs

      - name: Upload configuration artifacts
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: Configs-${{ inputs.php }}-${{ github.sha }}-${{ github.run_number }}-${{ github.run_attempt }}
          path: |
            docker-compose.yml
            source/composer.json
            source/composer.lock
            source/source/config.inc.php

  shop_codeception_tests:
    strategy:
      matrix:
        php: [ '8.1' ]
    needs: [ install_shop_with_module ]
    runs-on: ubuntu-latest
    if: (github.event_name == 'workflow_dispatch')
    steps:
      - name: Load current installation from cache
        uses: actions/cache@v3
        with:
          path: |
            ./*
          key: shopInstallation-${{ github.sha }}-${{ join(inputs.*, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}
          restore-keys: |
            shopInstallation-${{ github.sha }}-${{ join(inputs.*, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}
            shopInstallation-${{ github.sha }}-${{ join(inputs.*, '-') }}-${{ github.run_number }}

      - name: Start containers
        run: |
          make file=services/selenium-chrome.yml addservice
          make up
          sleep 2

      - name: Run shop tests
        run: |    
          docker-compose exec -T \
            -e MODULE_IDS=${{ env.MODULE_IDS }} \
            -e THEME_ID=apex \
            -e SELENIUM_SERVER_HOST=selenium \
            -e BROWSER_NAME=chrome \
            php vendor/bin/codecept run Acceptance \
              --bootstrap=/var/www/source/bootstrap.php \
              -c tests/codeception.yml \
              --ext DotReporter \
              --skip-group flow_theme  --skip-group admin

      - name: Upload log artifact
        uses: actions/upload-artifact@v3
        with:
          name: ShopCodeceptionTestsLog-${{ inputs.php }}-${{ inputs.mysql }}-${{ inputs.template_engine }}
          path: |
            docker-compose.yml
            source/composer.json
            source/composer.lock
            source/source/config.inc.php
            source/source/log/oxideshop.log
            data/php/logs/error_log.txt
            source/tests/Codeception/_output

  shopadmin_codeception_tests:
    strategy:
      matrix:
        php: [ '8.1' ]
    needs: [ install_shop_with_module ]
    runs-on: ubuntu-latest
    steps:
      - name: Load current installation from cache
        uses: actions/cache@v3
        with:
          path: |
            ./*
          key: shopInstallation-${{ github.sha }}-${{ join(inputs.*, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}
          restore-keys: |
            shopInstallation-${{ github.sha }}-${{ join(inputs.*, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}
            shopInstallation-${{ github.sha }}-${{ join(inputs.*, '-') }}-${{ github.run_number }}

      - name: Start containers
        run: |
          make file=services/selenium-chrome.yml addservice
          make up
          sleep 2

      - name: Run tests
        run: |
          docker-compose exec -T \
            -e MODULE_IDS=${{ env.MODULE_IDS }} \
            -e THEME_ID=apex \
            -e SELENIUM_SERVER_HOST=selenium \
            -e BROWSER_NAME=chrome \
            php vendor/bin/codecept run Acceptance \
              --bootstrap=/var/www/source/bootstrap.php \
              -c tests/codeception.yml \
              --ext DotReporter \
              -g admin

      - name: Upload log artifact
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: ShopAdminCodeceptionTestsLog-${{ inputs.php }}-${{ inputs.mysql }}-${{ inputs.template_engine }}
          path: |
            docker-compose.yml
            source/composer.json
            source/composer.lock
            source/source/config.inc.php
            source/source/log/oxideshop.log
            data/php/logs/error_log.txt
            source/tests/Codeception/_output