In this weeks post, let’s learn how to setup and run tests with MySQL and Laravel while using Travis CI, it’s easy, educational, and for me it was a bit fun.
Let’s get started with our .travis.yml
file, we will need a couple of things, MySQL and PHPUnit
so we will include those things in our initial yaml file:
language: php # language
php:
- 7.0
- 7.1
services:
- mysql # will init MySQL
before_script:
- cp .env.travis .env # copying .env.travis
- mysql -e 'create database testing;' # will run this command
- composer self-update
- composer install --no-interaction
- php artisan key:generate
- php artisan migrate --seed
script:
- vendor/bin/phpunit # run PHPUnit
In this particular setup, you’ll want to take a close look to the before_script
, where most of the magic is happening. Now open up your text editor or IDE, in this case, I’m using Vim:
APP_ENV=testing
APP_KEY=SomeRandomString
DB_CONNECTION=testing
DB_TEST_DATABASE=testing_db
DB_TEST_USERNAME=root
DB_TEST_PASSWORD=
CACHE_DRIVER=array
SESSION_DRIVER=array
QUEUE_DRIVER=sync
The next step is to setup PHPUnit
to use the testing database connection function (this is of course assuming you are using MySQL and not something like Redis or PostGRES (I’ll be making recipes for these DB frameworks as well), for this example we utilize MySQL, here is some XML to test the connection:
<?xml version="1.0" encoding="UTF-8"?>
<Phpunit BackupGlobals="False"
BackupStaticAttributes="False"
Bootstrap="Bootstrap/Autoload.Php"
Colors="True"
ConvertErrorsToExceptions="True"
ConvertNoticesToExceptions="True"
ConvertWarningsToExceptions="True"
ProcessIsolation="False"
StopOnFailure="False"
SyntaxCheck="False">
<Testsuites>
<Testsuite Name="Application Test Suite">
<Directory>./tests/</Directory>
</Testsuite>
</Testsuites>
<Filter>
<Whitelist>
<Directory Suffix=".Php">app/</Directory>
</Whitelist>
</Filter>
<Php>
<Env Name="APP_ENV" Value="Testing"/> <!--Make sure this matches your env var-->
<Env Name="DB_CONNECTION" Value="Testing"/>
<Env Name="CACHE_DRIVER" Value="Array"/>
<Env Name="SESSION_DRIVER" Value="Array"/>
<Env Name="QUEUE_DRIVER" Value="Sync"/>
</Php>
</Phpunit>
Now add the testing config:
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
'testing' => [
'driver' => 'mysql',
'host' => env('DB_TEST_HOST', 'localhost'),
'database' => env('DB_TEST_DATABASE', 'testing_db'),
'username' => env('DB_TEST_USERNAME', 'root'),
'password' => env('DB_TEST_PASSWORD', 'secret'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
],
Perfect, now we want to push this code to GitHub, using the classic flow:
git init
git add .
git commit -m "Laravel push"
git remote add origin remote repository URL
git remote -v
git push -u origin master
Make sure this repo is synced with Travis, and you’re all set! I hope this recipe was as enjoyable as it was for you as it was for me.