How to test module in different Magento and PHP versions using CI?
-
14-04-2021 - |
Pregunta
I have an open-source module in Github and I'd like to automatically test it via CI using the Magento Coding Standard and different Magento installations in different PHP versions. For example:
Magento Enterprise 2.3 + PHP 7.2
Magento Community 2.4 + PHP 7.3
How could I implement it?
Solución
First of all, you have to implement the Magento Coding Standard, then you can create a Travis config file to set the PHP version that you want to test your module and the Magento version and edition, for example:
PHP 7.2
- Magento OpenSource (2.3.2, 2.3.3-p1)
- Magento Commerce (2.3.2, 2.3.3-p1)
PHP 7.3
- Magento OpenSource (2.3.3-p1, 2.3.4-p2)
- Magento Commerce (2.3.3-p1, 2.3.4-p2)
Magento Coding Standard
To install the Magento's coding standards in your module first add the Composer package:
php bin/magento setup:di:compile
Then add these lines in your composer.json
, I'm considering that you module is inside an src
folder.
...
"scripts": {
"post-install-cmd": [
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"
],
"post-update-cmd": [
"([ $COMPOSER_DEV_MODE -eq 0 ] || vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/)"
],
"php": "vendor/bin/phpcs --standard=./vendor/phpcompatibility/php-compatibility/PHPCompatibility --extensions=php,phtml -d memory_limit=-1 src",
"lint": "vendor/bin/phpcs --standard=Magento2 src",
"fix": "vendor/bin/phpcbf --standard=Magento2 src"
}
...
Automated tests
In order to set up the automated tests I recommend using Travis CI with Github, to do so, you just need to create these files below. And add your Magento user and password in the Travis panel as MAGENTO_USERNAME and MAGENTO_PASSWORD.
.travis.yml
sudo: required
dist: trusty
addons:
apt:
packages:
- mysql-server-5.6
- mysql-client-core-5.6
- mysql-client-5.6
- postfix
language: php
jobs:
include:
- php: 7.2
env:
- MAGENTO_VERSION=2.3.2 MAGENTO_EDITION=community
- if: type != pull_request
php: 7.2
env:
- MAGENTO_VERSION=2.3.2 MAGENTO_EDITION=enterprise
- php: 7.2
env:
- MAGENTO_VERSION=2.3.3-p1 MAGENTO_EDITION=community
- if: type != pull_request
php: 7.2
env:
- MAGENTO_VERSION=2.3.3-p1 MAGENTO_EDITION=enterprise
- php: 7.2
env:
- MAGENTO_VERSION=2.3.4-p2 MAGENTO_EDITION=community
- if: type != pull_request
php: 7.2
env:
- MAGENTO_VERSION=2.3.4-p2 MAGENTO_EDITION=enterprise
- php: 7.3
env:
- MAGENTO_VERSION=2.3.3-p1 MAGENTO_EDITION=community
- if: type != pull_request
php: 7.3
env:
- MAGENTO_VERSION=2.3.3-p1 MAGENTO_EDITION=enterprise
- php: 7.3
env:
- MAGENTO_VERSION=2.3.4-p2 MAGENTO_EDITION=community
- if: type != pull_request
php: 7.3
env:
- MAGENTO_VERSION=2.3.4-p2 MAGENTO_EDITION=enterprise
before_install:
- if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then composer config -g http-basic.repo.magento.com $MAGENTO_USERNAME $MAGENTO_PASSWORD ; fi
- echo "{\"http-basic\":{\"repo.magento.com\":{\"username\":\"${MAGENTO_USERNAME}\",\"password\":\"${MAGENTO_PASSWORD}\"}}}" > auth.json
- composer global require hirak/prestissimo
- chmod +x ./bin/*
- ./bin/prepare_php.sh
- ./bin/before_install.sh
install:
- composer install --prefer-dist
cache:
directories:
- $HOME/.composer/cache
script:
- ./bin/test_magento.sh
- vendor/bin/phpcs --config-set installed_paths ../../magento/magento-coding-standard/
- vendor/bin/phpcs --standard=Magento2 --severity=9 src/
bin/before_install.sh
#!/usr/bin/env bash
mkdir -p app/etc var
# If no stability configured, do nothing and keep installing the given Magento2 version.
if [[ -z "${MAGENTO_STABILITY}" ]]; then
VERSION=$MAGENTO_VERSION
else
# Else, switch explicit version to closest one with ~
VERSION="~${MAGENTO_VERSION}"
fi
echo "==> Require magento/magento2-base package (Version $VERSION) ..."
composer require magento/magento2-base $VERSION --no-update -q
bin/prepare_php.sh
#!/usr/bin/env bash
set -e
trap '>&2 echo Error: Command \`$BASH_COMMAND\` on line $LINENO failed with exit code $?' ERR
echo '==> Disabling xdebug, adjusting memory limit to -1.'
phpenv config-rm xdebug.ini
echo 'memory_limit = -1' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
phpenv rehash;
bin/test_magento.sh
#!/usr/bin/env bash
set -e
trap '>&2 echo Error: Command \`$BASH_COMMAND\` on line $LINENO failed with exit code $?' ERR
# Prepare a full Magento2 project with proper version.
cd ..
# If no stability configured, do nothing and keep installing the given Magento2 version.
if [[ -z "${MAGENTO_STABILITY}" ]]; then
STABILITY="--stability=stable"
VERSION="$MAGENTO_VERSION"
else
# Else, switch explicit version to closest one, and set composer to prefer given stability.
# Eg. if we want to test 2.3.0 which is in beta, composer will not be able to install it with magento/project-community-edition=2.3.0
# Proper syntax for installing would be : magento/project-community-edition=2.3.* --stability=beta
# That's the purpose of the 2 following lines.
STABILITY="--stability=$MAGENTO_STABILITY"
VERSION="${MAGENTO_VERSION%.*}.*"
fi
echo "==> Copying Magento2 repository credentials here."
cp "$TRAVIS_BUILD_DIR/auth.json" .
echo "==> Installing Magento 2 $MAGENTO_EDITION (Version $VERSION) ..."
echo "composer create-project --repository-url=https://repo.magento.com magento/project-$MAGENTO_EDITION-edition=$VERSION $STABILITY magento"
composer create-project --repository-url=https://repo.magento.com magento/project-$MAGENTO_EDITION-edition=$VERSION $STABILITY magento --quiet
cd "magento"
# Require the extension to make it usable (autoloading)
echo "==> Requiring extension from the $TRAVIS_BRANCH-dev branch"
composer require --dev "rafaelcg/magento2-quicklink:$TRAVIS_BRANCH-dev" --quiet
echo "==> Installing Magento 2"
mysql -uroot -e 'CREATE DATABASE magento2;'
php bin/magento setup:install -q --admin-user="admin" --admin-password="admin123" --admin-email="admin@example.com" --admin-firstname="Admin" --admin-lastname="User" --db-name="magento2"
echo "==> Process upgrade and try to compile..."
php bin/magento setup:upgrade -q
php bin/magento cache:flush
php bin/magento setup:static-content:deploy
php bin/magento setup:di:compile