Oct 2, 2020 by Thibault Debatty | 4375 views
https://cylab.be/blog/95/test-your-laravel-project-with-gitlab
So you have a Laravel project, and as a good programmer you are using GitLab to manage your code, and you started implementing some phpunit tests. But how to run these tests in GitLab?
First, you'll need a custom .env file that will be used for the tests. Let's call it env.gitlab. Here is a starting point that you can tweak to your needs.
APP_NAME="My awesome app"
APP_ENV=test
APP_KEY=base64:/nTMW3a12345678901234567890Ecv/ALCcT+TbaVU4=
APP_DEBUG=true
APP_URL=http://172.0.0.1:8000
LOG_CHANNEL=stack
DB_CONNECTION=sqlite
DB_DATABASE=db.sqlite
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_MAILER=log
EMAIL_VERIFICATION_EXPIRATION_TIME=60
APP_TIMEZONE="Europe/Brussels"
The main attention points here are:
Now we can create the file .gitlab-ci.yml (don't forget the leading dot) that will instruct GitLab how to test our app. Once again, here is a starting point:
# this docker image contains PHP 7.4, composer and some other goodies
# you can find the full recipe (Dockerfile) here
# https://gitlab.cylab.be/cylab/docker-images/-/blob/master/php/php7.4
image: cylab/php74
test:
before_script:
# use our custom .env file
- cp env.gitlab .env
# install the application
- COMPOSER_CACHE_DIR=composer-cache composer install
- php artisan key:generate
- touch storage/app/db.sqlite
- php artisan migrate
# Optionally, we can install nodejs to build our js and css files
- curl -sL https://deb.nodesource.com/setup_10.x | bash -
- apt install nodejs -y
- npm install
- npm run prod
# run the tests
script:
- vendor/bin/phpunit
# what we want to store locally to speed up the next run of this job
cache:
paths:
- composer-cache/
- node_modules/
In this file:
composer-cache
) and put these files in cache, so they can be reused at the next execution of the job. This allows to reduce the amount of downloaded libraries. Other files like bootstrap are not put in cache because these would simply pollute the next execution of the job and might corrupt the results of the tests.Finally, we can commit and push this all to GitLab:
git add env.gitlab
git add .gitlab-ci.yml
git commit -m "test with gitlab"
git push
Once this basic tests are configured, don't hesitate to add:
This blog post is licensed under CC BY-SA 4.0