Creating Symfony2 Unit Tests outside of PHPUnit

The latest framework I’ve begun to experiment with is Symfony2 for PHP. At Mover we’ve integrated a few of Symfony’s modules for various bits and pieces of our internal tooling and since I’ve been rather impressed with what we’ve used so far I figured I’d give the entire framework itself a shot. As such, I’ve set out to create my first web project utilizing what seems to be a pretty well documented, explained, and exampled system.

However, I came across my first real hiccup rather shortly as a result of wanting to make my Symfony project testable outside of the defacto testing standard that is PHPUnit. As of right now I am powering my tests with Matura, a really neat test framework that a colleague of mine wrote in which I am also evaluating and hoping to test for him in the process of this experiment. Anyway, if you are using Symfony’s pre-provided abstract class “WebTestCase” you can simply extend it and Voila, you’ve got access to Symfony’s internal workings and you’re writing PHPUnit tests.

Long story short, I dove into that code, ripped out the basic pieces I’ve needed so far and if you scroll down, you’ll see the end result. A little bootstrapper class which is essentially just the Symfony bits with the PHPUnit logic ripped out.

<?php namespace PROJECT\Tests;
require_once(__DIR__ . '/../../../../app/autoload.php');
require_once(__DIR__ . '/../../../../app/AppKernel.php');
use AppKernel;
* Any test related loading should happen here. Since we are using an
* autoloader and decent framework, this file should hopefully remaing fairly
* concise.
class TestKernel
protected static $kernel;
* Because we'd like to avoid using actualy database connections as much
* as possible, use this call to get a mock Entity Manager which can then
* be passed into Mediator's constructor.
public static function getService($service_name)
return static::$kernel->getContainer()->get($service_name);
* Retrieves a Doctrine Entity Manager with database connectivity from the
* Kernel.
public static function getEM()
return static::$kernel->getContainer->get('doctrine.orm.entity_manager');
public static function getClient(array $options = array(), array $server = array())
$client = static::$kernel->getContainer()->get('test.client');
return $client;
* Kernel startup/shutdown related tasks.
private static function boot(array $options = array())
static::$kernel = new AppKernel(
isset($options['environment']) ? $options['environment'] : 'test',
isset($options['debug']) ? $options['debug'] : true
* Should be called at the "Tear-Down" stage within any tests that utilize
* a Client or Kernel.
public static function ensureShutdown()
if (static::$kernel !== null) {

view raw
hosted with ❤ by GitHub

Hopefully you find that useful, if not an alright starting point for creating your own non-PHPUnit Symfony unit tests.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s