Setup Travis for your SilverStripe module

Posted by on 10 August 2012 | Comments

I've wrote a blog post about using Jenkins to test your module project in Setup Jenkins for your SilverStripe module.

While Jenkins is a great tool and useful, if your project is public, you can use Travis for testing your module for free. In addition it is easier than to use Jenkins.

Currently our module is tested on Travis. You can check the status of our module at

In this blog post, I'll show you how to setup travis for your silverstripe module project.

Overview

Before go into details of settings, I'll show you the overview of how the scripts would works.

1. Travis is notified from github that new changes are pushes.
2. Travis clone codes in to ~/builds/yukiawano/sscpmodule
3. Download the latest version of SilverStripe from the repository. Details
4. Move the module into ss3 folder.
5. Create database
6. Copy configuration files to mysite directory.
7. Copy _ss_environment to ss3 directory
8. run dev/build
9. run tests

Write the script

We explained how the script would work. In this section, we'll write it into .travis.yml. I'll show an example of my module yukiawano/sscpmodule in the below.

.travis.yml

language: php
php:
- 5.4
before_script:
- cd ~/builds
- git clone git://github.com/silverstripe/silverstripe-installer.git ss3
- cd ~/builds/ss3
- git clone git://github.com/silverstripe/sapphire.git framework
- git clone git://github.com/silverstripe/silverstripe-cms.git cms
- git clone git://github.com/silverstripe-themes/silverstripe-simple.git themes/simple
- cd ~/builds
- mv yukiawano/sscpmodule ss3/sscp
- mysql -e "create database IF NOT EXISTS ss_module_test;"
- mv -f ss3/sscp/tests/travis/_config.php.travis ss3/mysite/_config.php
- mv -f ss3/sscp/tests/travis/_ss_environment.php.travis ss3/_ss_environment.php
- cd ss3/framework
- ./sake dev/build "flush=1"
script:
- ./sake dev/tests/module/sscp "flush=1"

Write configuration files

In the above script, We puts configuration files _config.php and _ss_environment.php in the tests directory of module. In the configuration files, we define database configuration and write some tricks to run test. An example of _config.php and _ss_environment.php is the below.

tests/travis/_config.php.travis

<?php

global $project;
$project = 'mysite';

global $database;
$database = 'ss_module_test';

// Use _ss_environment.php file for configuration
require_once("conf/ConfigureFromEnv.php");

tests/travis/_ss_environment.php.travis

<?php
ob_start();

define('SS_ENVIRONMENT_TYPE', 'test');

/* Database connection */
define('SS_DATABASE_SERVER', 'localhost');
define('SS_DATABASE_USERNAME', 'root');
define('SS_DATABASE_PASSWORD', '');

/* Configure a default username and password to access the CMS on all sites in this environment. */
define('SS_DEFAULT_ADMIN_USERNAME', 'username');
define('SS_DEFAULT_ADMIN_PASSWORD', 'password');

Tips: Why we add ob_start() in the head of _ss_environment.php?

Test framework of SilverStripe print some messages before running the tests, and you may get "headers already sent by..." error when you try to write cookie. While I got this error on my VPS(CentOS) and instances on Travis, I didn't get this error on my local development environment(Ubuntu). This may be environment dependent error.