Monday, August 13, 2012

Continuous Integration: Automated database setup with Doctrine on Travis-CI - till

Continuous Integration: Automated database setup with Doctrine on Travis-CI - till:
Testing is important — most people understand that by now. A lot of people write tests for their open source code already, but in-house testing is still hard. For example, many of us had an encounter with Jenkins: it runs well to a point where it becomes harder to maintain the Jenkins than it is to write tests.

Another obstacle is test setup and environments: When I write and run tests, there is sometimes only so much I can do to mock and avoid actual calls to my storage backend. While I prefer to run my database tests against a SQLite in memory database, there are these edge cases, where I work with multiple database or I write a direct query (and by-pass the ORM-magic).

In these cases I need to have that database server available in my test environment!

The following blog posts explains how to solve these things with Travis-CI. I will walk you through the setup on Travis-CI's business service. But most of this applies to their open source offering as well.


Step by step



I'll try to break it up into small steps.


Travis-CI



The first step is to login at http://travis-ci.com and add the repository tests should be run for. To be able to add repositories of an organization, you have to be the owner of the organization. The easiest way to get access to the service right now is donating to these guys or in case you have done that already: email them. ;-)

The second step is setting up a .travis.yml file.

Mine looks like this:

language: php
php:
- 5.3
- 5.4
before_script:
- ./composer.phar -v install --dev
- psql -c 'create database testdatabase;' -U postgres


Run-down:


  • run the tests against PHP 5.3 and 5.4

  • before_script defines your test setup (outside PHP)

  • I omitted the script stanza because the default (phpunit) works for me



Composer



I am using composer to manage my dependencies and you should too. I don't want to go into details here, but a short example of my composer.json is the following:

{
"name": "mycompany/project",
"description": "A super cool project.",
"require": {
"doctrine/orm": "2.2.2"
},
"autoload": {
"psr-0": {
"MyCompany\\Project\\Test": "tests/",
"MyCompany\\Project": "src/"
}
}
}


Side-note: We also currently commit a composer.phar into each repository for two reasons:


  1. To ensure a change in composer won't break our setup.

  2. Downtime of (or connectivity issues to) their website don't break our deployments and test runs.



Test framework setup



There is not a whole lot to setup since Travis-CI installs phpunit already. Just make sure you have a phpunit.xml in the root of your repository and you are good to go.


Database schema



The next step would be to generate your schema and check in some .sql, right? I'm not a huge fan of this, because I hate running through a lot of manual steps when I need to update something. Manual steps means that they might be forgotten or people make a mistake. So the objective is to avoid any manual labour as much as you can.

Instead of maintaining these files, I use Doctrine's SchemaTool. It takes care of this just fine because I annotated all my entities already.

To make use of this, I suggest to add the following to your test case:

<?php 
namespace MyCompany\Project\Test;

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Persistence\PersistentObject;
use Doctrine\ORM\Tools\SchemaTool;

class MyTestCase extends \PHPUnit_Framework_Testcase
{
protected $em, $tool;

public function setUp()
{
$this->setUpDatabase(); // wrap this again
$this->setUpSchema();
/* more setup here */
}

public function tearDown()
{
$classes = array(
$this->em->getClassMetadata('MyCompany\Project\Entity\SomethingImportant'),
);
$this->tool->dropSchema($classes);
unset($tool);
unset($em);
}

public function setUpDatabase()
{
$isDevMode = true;
$doctrineConfig = Setup::createAnnotationMetadataConfiguration(
array('path/to/Entity'),
$isDevMode
);

// database configuration parameters
$dbConfig = array(
'host' => '127.0.0.1',
'user' => 'postg

Truncated by Planet PHP, read more at the original (another 2380 bytes)

DIGITAL JUICE

No comments:

Post a Comment

Thank's!