From 2897437ee3eaf0a07e47a6b54e943cc996951650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Zuffinetti?= Date: Thu, 2 Nov 2023 11:00:15 +0100 Subject: [PATCH 1/6] Add support for unit tests, coverage, and CI --- .github/workflows/ci.yaml | 71 ++++++++++++++++++++++++++++++++++++ Dockerfile | 26 +++++++++++++ Makefile | 5 +++ alma/composer.json | 2 +- alma/phpunit.ci.xml | 9 +++++ docker-compose.yml | 11 +++++- docker/php-customization.ini | 4 +- scripts/entrypoint.sh | 5 +++ 8 files changed, 129 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/ci.yaml create mode 100644 Dockerfile create mode 100755 scripts/entrypoint.sh diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml new file mode 100644 index 000000000..4e3d7a258 --- /dev/null +++ b/.github/workflows/ci.yaml @@ -0,0 +1,71 @@ +--- +name: E2E Test +on: + pull_request: + branches: ["main", "develop"] + types: ["opened", "synchronize", "reopened", "ready_for_review"] + workflow_dispatch: + +jobs: + launch: + strategy: + matrix: + version: + - 1.7.8.7 + #- 1.7.7.5 + runs-on: ubuntu-latest + if: github.event.pull_request.draft == false + steps: + - name: Get only last part of branch name in $SLUG_VERSION env var + run: | + VERSION=${{ matrix.version }} + echo "SLUG_VERSION=${VERSION//./-}" >> $GITHUB_ENV + + - name: Generate token + id: generate_token + uses: tibdex/github-app-token@v1.8.0 + continue-on-error: true + with: + app_id: ${{ secrets.ALMA_UPDATE_CHECKS_APP_ID }} + private_key: ${{ secrets.ALMA_UPDATE_CHECKS_APP_PEM }} + + - uses: LouisBrunner/checks-action@v1.6.2 + id: e2e_status + with: + token: ${{ steps.generate_token.outputs.token }} + name: E2E Test / result (${{ matrix.version }}) + status: "in_progress" + + - name: Generate Github token for integration-infrastructure repo + id: generate_github_token_integration_infrastructure + uses: tibdex/github-app-token@v1 + with: + app_id: ${{ secrets.ALMA_WF_TRIGGER_APP_ID }} + private_key: ${{ secrets.ALMA_WF_TRIGGER_APP_PEM }} + installation_id: ${{ secrets.ALMA_WF_TRIGGER_INSTALLATION_ID }} + repository: alma/integration-infrastructure + + - name: Invoke e2e workflow with inputs + uses: benc-uk/workflow-dispatch@v1.2.2 + with: + workflow: Deploy CMS + token: ${{ steps.generate_github_token_integration_infrastructure.outputs.token }} + repo: alma/integration-infrastructure + ref: main + inputs: > + { + "name": "e2e-${{ github.run_id }}", + "alma_plugin_branch": "${{ github.head_ref || github.ref_name }}", + "alma_plugin_test_branch" : "main", + "cms": "prestashop-${{ matrix.version }}", + "e2e": "true", + "e2e_check_id": "${{ steps.e2e_status.outputs.check_id }}", + "e2e_check_origin" : "${{ github.repository }}" + } + + - uses: LouisBrunner/checks-action@v1.6.2 + if: failure() + with: + token: ${{ steps.generate_token.outputs.token }} + check_id: ${{ steps.e2e_status.outputs.check_id }} + conclusion: failure diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..341e95502 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +ARG COMPOSER_VERSION=2 +ARG PLATFORM_VERSION=8.1.0 + +FROM composer:${COMPOSER_VERSION} AS composer +FROM prestashop/prestashop:${PLATFORM_VERSION} + +ENV PS_ENABLE_SSL=1 + +# Change root password +RUN echo 'root:alma' | chpasswd + +RUN pecl install xdebug-3.1.5 \ + && docker-php-ext-enable xdebug + +WORKDIR /var/www/html/modules/alma/ + +RUN head -n -1 /tmp/docker_run.sh > /tmp/docker_install.sh +COPY ./scripts/entrypoint.sh /entrypoint.sh + +# Composer install +COPY --from=composer /usr/bin/composer /usr/bin/composer +COPY alma/composer.json . + +RUN composer install + +ENTRYPOINT ["bash", "/entrypoint.sh"] diff --git a/Makefile b/Makefile index 3a2380f21..ce7d6edb4 100644 --- a/Makefile +++ b/Makefile @@ -41,3 +41,8 @@ crowdin-download: .PHONY: crowdin-upload crowdin-upload: crowdin upload sources + +.PHONY: test +test: + docker compose build prestashop + docker compose run --rm prestashop ./vendor/bin/phpunit -c phpunit.ci.xml --coverage-text diff --git a/alma/composer.json b/alma/composer.json index 58d92e6b8..653bc5f64 100644 --- a/alma/composer.json +++ b/alma/composer.json @@ -3,7 +3,7 @@ "description": "Payment method module to integrate Alma in PrestaShop", "type": "library", "require-dev": { - "phpunit/phpunit": "^5", + "phpunit/phpunit": "^7", "mockery/mockery": "^1.3", "phpcompatibility/php-compatibility": "10.x-dev", "prestashop/php-dev-tools": "^3.16" diff --git a/alma/phpunit.ci.xml b/alma/phpunit.ci.xml index 2eaae35e6..9874b4d52 100644 --- a/alma/phpunit.ci.xml +++ b/alma/phpunit.ci.xml @@ -16,4 +16,13 @@ + + + ../alma + + ../alma/vendor + + + + diff --git a/docker-compose.yml b/docker-compose.yml index 0d4385f21..27b4657ef 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,13 @@ services: - "./tmp/db_data:/var/lib/mysql" prestashop: - image: prestashop/prestashop:1.6.1.23 + build: + context: . + args: + # We are not using version 8.x for now, as this will install with PHP 8.x. + # This will then require a PHPUnit version that will be too high, requiring + # updating prestashop/autoindex. + PLATFORM_VERSION: 1.7.8.7 environment: - DB_SERVER=mysql - DB_NAME=prestashop @@ -32,8 +38,11 @@ services: - PS_FOLDER_INSTALL=alminstall - ADMIN_MAIL=admin@test.prestashop.com - ADMIN_PASSWD=test2test + depends_on: + - mysql ports: - "8080:80" volumes: - ./alma:/var/www/html/modules/alma + - /var/www/html/modules/alma/vendor # do not mount vendor inside container - ./docker/php-customization.ini:/usr/local/etc/php/conf.d/php-customization.ini diff --git a/docker/php-customization.ini b/docker/php-customization.ini index fb41e938c..e7572020c 100644 --- a/docker/php-customization.ini +++ b/docker/php-customization.ini @@ -2,10 +2,10 @@ max_input_vars=5000 display_errors=1 -;[xdebug] +[xdebug] +xdebug.mode=develop,coverage ;; comment out this line to disable xdebug ;zend_extension=xdebug.so -;xdebug.mode=develop ;xdebug.remote_enable=1 ;xdebug.remote_autostart=1 ;xdebug.log=/var/log/xdebug.log diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh new file mode 100755 index 000000000..b19bfff73 --- /dev/null +++ b/scripts/entrypoint.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +bash /tmp/docker_install.sh + +exec "$@" From dc4e95f3fc870261c48ec2fb50fc077bc9c224a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Zuffinetti?= Date: Thu, 2 Nov 2023 11:05:11 +0100 Subject: [PATCH 2/6] Add trigger on push to test --- .github/workflows/ci.yaml | 75 ++++++--------------------------------- 1 file changed, 11 insertions(+), 64 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 4e3d7a258..7707e2bda 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,71 +1,18 @@ ---- -name: E2E Test +name: Continuous Integration + on: + push: pull_request: - branches: ["main", "develop"] - types: ["opened", "synchronize", "reopened", "ready_for_review"] + branches: + - main + - develop workflow_dispatch: jobs: - launch: - strategy: - matrix: - version: - - 1.7.8.7 - #- 1.7.7.5 - runs-on: ubuntu-latest - if: github.event.pull_request.draft == false - steps: - - name: Get only last part of branch name in $SLUG_VERSION env var - run: | - VERSION=${{ matrix.version }} - echo "SLUG_VERSION=${VERSION//./-}" >> $GITHUB_ENV - - - name: Generate token - id: generate_token - uses: tibdex/github-app-token@v1.8.0 - continue-on-error: true - with: - app_id: ${{ secrets.ALMA_UPDATE_CHECKS_APP_ID }} - private_key: ${{ secrets.ALMA_UPDATE_CHECKS_APP_PEM }} + tests: - - uses: LouisBrunner/checks-action@v1.6.2 - id: e2e_status - with: - token: ${{ steps.generate_token.outputs.token }} - name: E2E Test / result (${{ matrix.version }}) - status: "in_progress" + runs-on: ubuntu-22.04 - - name: Generate Github token for integration-infrastructure repo - id: generate_github_token_integration_infrastructure - uses: tibdex/github-app-token@v1 - with: - app_id: ${{ secrets.ALMA_WF_TRIGGER_APP_ID }} - private_key: ${{ secrets.ALMA_WF_TRIGGER_APP_PEM }} - installation_id: ${{ secrets.ALMA_WF_TRIGGER_INSTALLATION_ID }} - repository: alma/integration-infrastructure - - - name: Invoke e2e workflow with inputs - uses: benc-uk/workflow-dispatch@v1.2.2 - with: - workflow: Deploy CMS - token: ${{ steps.generate_github_token_integration_infrastructure.outputs.token }} - repo: alma/integration-infrastructure - ref: main - inputs: > - { - "name": "e2e-${{ github.run_id }}", - "alma_plugin_branch": "${{ github.head_ref || github.ref_name }}", - "alma_plugin_test_branch" : "main", - "cms": "prestashop-${{ matrix.version }}", - "e2e": "true", - "e2e_check_id": "${{ steps.e2e_status.outputs.check_id }}", - "e2e_check_origin" : "${{ github.repository }}" - } - - - uses: LouisBrunner/checks-action@v1.6.2 - if: failure() - with: - token: ${{ steps.generate_token.outputs.token }} - check_id: ${{ steps.e2e_status.outputs.check_id }} - conclusion: failure + steps: + - name: Run unit tests + run: make test From 1c459d931b046a6d43f6c933675e545819c44088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Zuffinetti?= Date: Thu, 2 Nov 2023 11:06:36 +0100 Subject: [PATCH 3/6] Add trigger on push to test --- .github/workflows/ci.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7707e2bda..1b8e32c9a 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -14,5 +14,7 @@ jobs: runs-on: ubuntu-22.04 steps: + - uses: actions/checkout@v4 + - name: Run unit tests run: make test From e74583dcfdabae8d935830d84e79660d22b0c04b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Zuffinetti?= Date: Thu, 2 Nov 2023 11:36:37 +0100 Subject: [PATCH 4/6] Bash flags --- docker-compose.yml | 13 ++++++++++++- scripts/entrypoint.sh | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 27b4657ef..70c9aa9d9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,16 @@ services: - 3307:3306 volumes: - "./tmp/db_data:/var/lib/mysql" + healthcheck: + test: + [ + "CMD", + "mysqladmin", + "ping", + "-p$$MYSQL_ROOT_PASSWORD" + ] + timeout: 10s + retries: 10 prestashop: build: @@ -39,7 +49,8 @@ services: - ADMIN_MAIL=admin@test.prestashop.com - ADMIN_PASSWD=test2test depends_on: - - mysql + mysql: + condition: service_healthy ports: - "8080:80" volumes: diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh index b19bfff73..d6df6011e 100755 --- a/scripts/entrypoint.sh +++ b/scripts/entrypoint.sh @@ -1,5 +1,12 @@ #!/bin/bash +######################################## +# Exit as soon as any line in the bash script fails. +set -o errexit + +# pipefail: the return value of a pipeline is the status of the last command to exit with a non-zero status, or zero if no command exited with a non-zero status +set -o pipefail + bash /tmp/docker_install.sh exec "$@" From 9460868a363c2a1af711d2c056da1d4ef2eb396f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Zuffinetti?= Date: Sun, 26 Nov 2023 13:13:48 +0100 Subject: [PATCH 5/6] Upgrade to mysql8.2 --- docker-compose.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docker-compose.yml b/docker-compose.yml index 70c9aa9d9..121ccccc9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '3' services: mysql: - image: mysql:5.5 + image: mysql:8.2 environment: - MYSQL_ROOT_PASSWORD=admin - MYSQL_DATABASE=prestashop @@ -53,6 +53,7 @@ services: condition: service_healthy ports: - "8080:80" + # platform: linux/x86_64 volumes: - ./alma:/var/www/html/modules/alma - /var/www/html/modules/alma/vendor # do not mount vendor inside container From 229742a115ca9c3537c389694ee00931d6b99927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Zuffinetti?= Date: Sun, 26 Nov 2023 13:25:18 +0100 Subject: [PATCH 6/6] Restore phpunit version --- alma/composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alma/composer.json b/alma/composer.json index 653bc5f64..58d92e6b8 100644 --- a/alma/composer.json +++ b/alma/composer.json @@ -3,7 +3,7 @@ "description": "Payment method module to integrate Alma in PrestaShop", "type": "library", "require-dev": { - "phpunit/phpunit": "^7", + "phpunit/phpunit": "^5", "mockery/mockery": "^1.3", "phpcompatibility/php-compatibility": "10.x-dev", "prestashop/php-dev-tools": "^3.16"