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;
// PATH TO YOUR AUTOLOADER
require_once(__DIR__ . '/../../../../app/autoload.php');
// PATH TO YOUR SYMFONY APP KERNEL
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)
{
static::boot();
return static::$kernel->getContainer()->get($service_name);
}
/**
* Retrieves a Doctrine Entity Manager with database connectivity from the
* Kernel.
*/
public static function getEM()
{
static::boot();
return static::$kernel->getContainer->get('doctrine.orm.entity_manager');
}
public static function getClient(array $options = array(), array $server = array())
{
static::boot($options);
$client = static::$kernel->getContainer()->get('test.client');
$client->setServerParameters($server);
return $client;
}
/**************
* Kernel startup/shutdown related tasks.
**************/
private static function boot(array $options = array())
{
static::ensureShutdown();
static::$kernel = new AppKernel(
isset($options['environment']) ? $options['environment'] : 'test',
isset($options['debug']) ? $options['debug'] : true
);
static::$kernel->boot();
}
/**
* 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) {
static::$kernel->shutdown();
}
}
}

view raw
TestKernel.php
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:

WordPress.com Logo

You are commenting using your WordPress.com 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