name: Development

on:
  workflow_call:
    inputs:
      php:
        required: true
        type: string
      template_engine:
        type: string
        default: 'both'
      module_codecept_test_group:
        type: string
        default: 'usercentrics'

  workflow_dispatch:
    inputs:
      php:
        description: 'PHP version'
        required: true
        default: '8.0'
        type: choice
        options:
          - '8.0'
          - '8.1'
      template_engine:
        description: 'Template engine - smarty with flow theme, twig with twig or apex theme'
        required: true
        default: 'twig-apex'
        type: choice
        options:
          - 'both'
          - 'twig-apex'
          - 'twig-twig'
          - 'smarty'
      module_codecept_test_group:
        description: 'Module codeception test group'
        required: true
        default: 'usercentrics'
        type: string

env:
  PACKAGE_NAME: 'oxid-professional-services/usercentrics'
  MODULE_ID: 'oxps_usercentrics'

jobs:
  install_shop_with_module:
    runs-on: ubuntu-latest
    steps:
      - name: Print run parameters
        run: |
          echo "Running job with:\
            PHP version: ${{ inputs.php }}\
            Template engine: ${{ inputs.template_engine }}\
            Tests group: ${{ inputs.module_codecept_test_group }}\
            Trigger event: ${{ github.event_name }}"

      - 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.0.x --single-branch source

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

      - name: Prepare container configuration
        run: |
          make setup
          make addbasicservices
          make file=services/selenium-chrome.yml addservice

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

          perl -pi\
            -e 's#display_errors =.*#display_errors = false#g;'\
            -e 'print "xdebug.max_nesting_level=1000\nxdebug.mode=coverage\n\n" if eof'\
            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
          sed -i "1s+^+SetEnvIf Authorization "\(.*\)" HTTP_AUTHORIZATION=\$1\n\n+" source/source/.htaccess
          sed -i -e 's/<dbHost>/mysql/'\
                 -e 's/<dbUser>/root/'\
                 -e 's/<dbName>/example/'\
                 -e 's/<dbPwd>/root/'\
                 -e 's/<dbPort>/3306/'\
                 -e 's/<sShopURL>/http:\/\/localhost.local\//'\
                 -e 's/<sShopDir>/\/var\/www\/source\//'\
                 -e 's/<sCompileDir>/\/var\/www\/source\/tmp\//'\
              source/source/config.inc.php

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

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

      - name: Configure module package to be installed
        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 all configured 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 ${{ env.MODULE_ID }}

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

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

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

      - name: Run tests
        run: |
          docker-compose exec -T \
            -e XDEBUG_MODE=coverage \
            php vendor/bin/phpunit \
              -c vendor/${{ env.PACKAGE_NAME }}/tests/phpunit.xml \
              --bootstrap=source/bootstrap.php \
              --coverage-clover=/var/www/coverage.xml \
              --coverage-text \
              --log-junit=/var/www/junit-log.xml \
            vendor/${{ env.PACKAGE_NAME }}/tests

      - name: Stop containers
        if: always()
        run: |
          make down
          sleep 2

      - name: Upload log artifact
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: TestsLog-${{ inputs.php }}-${{ inputs.template_engine }}
          path: |
            source/coverage.xml
            source/junit-log.xml

  codeception_smarty:
    needs: [ install_shop_with_module ]
    if: ${{ inputs.template_engine == 'both' || inputs.template_engine == 'smarty' }}
    runs-on: ubuntu-latest
    steps:
      - name: Load current installation from cache
        uses: actions/cache@v3
        with:
          path: |
            ./*
          key: installation-${{ join(inputs, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}
          restore-keys: |
            installation-${{ join(inputs, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}

      - name: Reconfigure codeception for smarty themes
        run: |
          perl -pi \
          -e 's#views/twig#views/flow/translations#g;' \
          source/test-module/tests/Codeception/acceptance.suite.yml
          perl -pi \
          -e 's#views/apex#views/flow/translations#g;' \
          source/test-module/tests/Codeception/acceptance.suite.yml

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

      - name: Install smarty components
        run: |
          docker-compose exec -T php composer require oxid-esales/flow-theme:dev-b-7.0.x --no-interaction --no-update
          docker-compose exec -T php composer require oxid-esales/smarty-component:dev-b-7.0.x --no-interaction --no-update
          docker-compose exec -T php composer require oxid-esales/smarty-admin-theme:dev-b-7.0.x --no-interaction --no-update
          docker-compose exec -T php composer update --no-interaction

      - name: Run tests
        run: |
          docker-compose exec -T \
            -e SELENIUM_SERVER_HOST=selenium \
            -e THEME_ID=flow \
            -e MODULE_IDS=${{ env.MODULE_ID }} \
            php php vendor/bin/codecept run acceptance \
              -c /var/www/vendor/${{ env.PACKAGE_NAME }}/tests/codeception.yml \
              -g ${{ inputs.module_codecept_test_group }}

      - name: Stop containers
        if: always()
        run: |
          make down
          sleep 2

      - name: Upload test artifacts
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: sModuleCodeceptionTestsLog-${{ inputs.php }}-${{ inputs.template_engine }}
          path: |
            ./source/test-module/tests/Codeception/_output/*

  codeception_twig:
    needs: [ install_shop_with_module ]
    if: ${{ inputs.template_engine == 'both' || inputs.template_engine == 'twig-twig' || inputs.template_engine == 'twig-apex' }}
    runs-on: ubuntu-latest
    steps:
      - name: Load current installation from cache
        uses: actions/cache@v3
        with:
          path: |
            ./*
          key: installation-${{ join(inputs, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}
          restore-keys: |
            installation-${{ join(inputs, '-') }}-${{ github.run_number }}-${{ github.run_attempt }}

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

      - name: Set Github dependencies
        run: |
          docker-compose run -T --rm php composer config github-protocols https
          docker-compose run -T --rm php composer config -g github-oauth.github.com ${{ secrets.GH_CI_JENKINS_TOKEN }}

      - name: Install twig template engine with twig and apex theme
        run: |
          docker-compose exec -T php composer require oxid-esales/twig-component:dev-b-7.0.x --no-interaction --no-update
          docker-compose exec -T php composer require oxid-esales/twig-theme:dev-b-7.0.x --no-interaction --no-update
          docker-compose exec -T php composer require oxid-esales/twig-admin-theme:dev-b-7.0.x --no-interaction --no-update
          docker-compose exec -T \
          php composer config repositories.oxid-esales/apex-theme \
            --json '{"type":"git", "url":"https://github.com/OXID-eSales/apex-theme.git"}'
          docker-compose exec -T php composer require oxid-esales/apex-theme:dev-main --no-update
          docker-compose exec -T php composer update --no-interaction

      - name: Reconfigure codeception for twig theme
        if: ${{ inputs.template_engine == 'both' || inputs.template_engine == 'twig-twig' }}
        run: |
          perl -pi \
          -e 's#views/apex#views/twig#g;' \
          source/test-module/tests/Codeception/acceptance.suite.yml

      - name: Run tests with twig theme
        if: ${{ inputs.template_engine == 'both' || inputs.template_engine == 'twig-twig' }}
        run: |
          docker-compose exec -T \
            -e SELENIUM_SERVER_HOST=selenium \
            -e THEME_ID=twig \
            -e MODULE_IDS=${{ env.MODULE_ID }} \
            php php vendor/bin/codecept run acceptance \
              -c /var/www/vendor/${{ env.PACKAGE_NAME }}/tests/codeception.yml \
              -g ${{ inputs.module_codecept_test_group }}

      - name: Install codeception-page-objects for apex theme
        if: ${{ inputs.template_engine == 'both' || inputs.template_engine == 'twig-apex' }}
        run: |
          docker-compose exec -T php composer require oxid-esales/codeception-page-objects:dev-b-7.0.x-APEX --no-update
          docker-compose exec -T php composer update --no-interaction

      - name: Reconfigure codeception for apex theme
        if: ${{ inputs.template_engine == 'both' || inputs.template_engine == 'twig-apex' }}
        run: |
          perl -pi \
          -e 's#views/twig#views/apex#g;' \
          source/test-module/tests/Codeception/acceptance.suite.yml

      - name: Run tests with apex theme
        if: ${{ inputs.template_engine == 'both' || inputs.template_engine == 'twig-apex' }}
        run: |
          docker-compose exec -T \
            -e SELENIUM_SERVER_HOST=selenium \
            -e THEME_ID=apex \
            -e MODULE_IDS=${{ env.MODULE_ID }} \
            php php vendor/bin/codecept run acceptance \
              -c /var/www/vendor/${{ env.PACKAGE_NAME }}/tests/codeception.yml \
              -g ${{ inputs.module_codecept_test_group }}

      - name: Stop containers
        if: always()
        run: |
          make down
          sleep 2

      - name: Upload test artifacts
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: ModuleCodeceptionTestsLog-${{ inputs.php }}-${{ inputs.template_engine }}
          path: |
            ./source/test-module/tests/Codeception/_output/*
