Easy Travis configuration for your Symfony2 tests using mysql database!
March 27, 2014Reynald Mandel2 min read
Wanna kick start a project? Wanna get straight to the code as fast as possible, without spending too much time on the overheads (creating tests environments and so on...)? Well, let's build a quick ready-to-go Travis environment with a mysql database for your tests! 3 minutes starting now!
Travis-GitHub hook configuration
Travis automatically builds virtual machines to run your tests upon request. It has been designed for developers: their virtual machines already come with a good base configuration, leaving you very few work to do to customize it to your needs.
First, log in to Travis with your GitHub account. Travis' services are free for open source projects, but you will have to pay for private repositories. To automate your tests each time you push a commit to GitHub, you will need to activate the hook between these two services: go to your Travis' repository list and activate the switch button for the project you want. You do not have anything to configure, everything is done automatically.
There you have a handy link to the GitHub repository's service hooks authorization. On this page, you can see a list of services that can hook to GitHub. Scroll down and click on "Travis". I advise you to check that the hook is effectively triggered by pressing the test button, it's fun and it's free ;)
Kick-start Travis configuration file
Now that Travis is notified every time you push a commit, it will look for your instructions in the .travis.yml file at the root of the project. Among other things, this file describes what you want to test, how, and what Travis must do before and after the tests. Here is a base ready-to-go Travis configuration file for Symfony2 projects:
php: - 5.5
- cp app/config/parameters.yml.travis app/config/parameters.yml
- composer install
- php app/console doctrine:database:create --env=test
- php app/console doctrine:schema:create --env=test
- php app/console doctrine:fixtures:load -n --env=test
- phpunit -c app
notifications: email: - email@example.com ;)
The Travis configuration file's short explanation
The first lines are self-explanatory, just note that you can specify more PHP versions than only 5.5. After that, you can see the commands we ask Travis to run before it launches his tests:
- The first one sets up your Symfony parameters for the test environment.
The only trick to know is that Travis' machines come with an already-started mysql
service, configured with a "travis" user and no password.
- Then goes the typical "composer install" that you can customize to fit your needs
with your favorite options.
- And at last the three app/console commands to create an up-to-date database
with your fixtures (for those who need them and activated the bundle).
Under the 'script' section, you can define which command(s) should be used to launch the tests. If you do not specify any, Travis default will be 'phpunit' without arguments. But in general for Symfony2 projects, your PHPUnit configuration files are in app/.
The last line is used to tell Travis which emails should receive his success/failure notifications for this repository. Pretty useful when you deal with many projects!
For reference, here is what your parameters.yml.travis file should look like:
parameters: database_user: travis database_password: ~ # Other parameters
Here it is! The 3-minute explanation is now ending and I hope that it has set your tests on track!
No more excuses for not having an up-and-running continuous integration ;)