How to set up a Symfony application test pipeline on Gitlab CI

GitLab CI (taken from https://docs.gitlab.com/ee/ci/introduction/)

Libraries to install

wget https://github.com/phpstan/phpstan/releases/download/0.11.19/phpstan.phar -O bin/phpstan.phar
wget https://getcomposer.org/download/1.9.0/composer.phar -O bin/composer.phar
composer require — dev phpunit/phpunit ^8

Scripts

php bin/console cache:clear --env=test
php bin/console doctrine:database:drop --force --env=test
php bin/console doctrine:database:create --env=test
php bin/console doctrine:migrations:migrate --env=test --no-interaction
php vendor/phpunit/phpunit/phpunit --testdox
#!/bin/bash

# We need to install dependencies only for Docker
[[ ! -e /.dockerenv ]] && exit 0

set -xe

apt-get update -yqq
apt-get install git zlib1g-dev unzip -yqq

docker-php-ext-install pdo pdo_mysql zip # add other extension needed

Framework config

# .env.test
DATABASE_URL=mysql://myapptest:myapptest@127.0.0.1:3307/myapptest
# app/config/config_test.yml
doctrine:
dbal:
driver: pdo_mysql
host: "%env(TEST_DATABASE_HOST)%"
port: "%env(TEST_DATABASE_PORT)%"
dbname: myapptest
user: myapptest
password: myapptest
# app/config/parameters.yml.dist (parameters.yml)
parameters:
env(TEST_DATABASE_PORT): 3307
env(TEST_DATABASE_HOST): 127.0.0.1

Local execution

# docker-compose version with test db named 'db_test'
docker-compose up -d db_test && sh scripts/php_test_run.sh
phpstan analyse src 

Gitlab CI config

default:
image: php:7.2-fpm
services:
- name: mysql:5.7
alias: mysql

cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- vendor/

variables:
MYSQL_ROOT_PASSWORD: pass_test
MYSQL_DATABASE: myapptest
MYSQL_USER: myapptest
MYSQL_PASSWORD: myapptest
# symfony 4
DATABASE_URL: 'mysql://myapptest:myapptest@mysql:3306/myapptest'
# symfony 3.2+
TEST_DATABASE_PORT: 3306
TEST_DATABASE_HOST: mysql
before_script:
- sh scripts/ci-php-install.sh
- php bin/composer.phar global require hirak/prestissimo
- php bin/composer.phar install -o

static_analysis:
stage: static_analysis
script:
- php bin/phpstan.phar analyse src --level=1

phpunit:
stage: test
script:
- sh scripts/php_test_run.sh

stages:
- static_analysis
- test
Gitlab branch page
Pipeline view
Job execution log

--

--

--

Software Engineer @ London [https://www.linkedin.com/in/elvisciotti]

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ActiveRecord vs. Ecto Part Two

My MobApp Studio Report

AWS WorkSpace : The taste of cloud

My first blog

The free Gmail SMTP server

Bigger .dockerignore, Smaller Docker Images

Welcome to the JAMstack Era (Part 2)

Task 11.3

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Elvis Ciotti

Elvis Ciotti

Software Engineer @ London [https://www.linkedin.com/in/elvisciotti]

More from Medium

Solid Principles

PHP Snippet: Unit Testing private methods

`private_access($user)->encryptPassword(‘abcde’);`

Using Laravel events in unit tests