PropelORM, Symfony 2 and Unit testing PropelORM, Symfony 2 and Unit testing symfony symfony

PropelORM, Symfony 2 and Unit testing


In my opinion (and Martin Folowers's) there is a step between calling everything statically and using Dependency Injection and it may be what you are looking for.

Where I can't do full DI (Zend Framework MVC for example) I will use a Service Locator. A Service Layer will be the place that all your classes go to get there dependencies from. Think of it as a one layer deep abstraction for your classes dependencies. There are many benefits to using a Service Locator but I will focus on testability in this case.

Let's get into some code, here is are model query class

class SomeModelQuery{    public function __call($method, $params)    {        if ($method == 'find') {            return 'Real Data';        }        return $this;    }}

All it does is return itself unless the method 'find' is called. Then is will return the hard-coded string "Real Data".

Now our service locator:

class ServiceLocator{    protected static $instance;    protected $someModelQuery;    public static function resetInstance()    {        static::$instance = null;    }    public static function instance()    {        if (self::$instance === null) {            static::$instance = new static();        }        return static::$instance;    }    public function getSomeModelQuery()    {        if ($this->someModelQuery === null) {            $this->someModelQuery = new SomeModelQuery();        }        return $this->someModelQuery;    }    public function setSomeModelQuery($someModelQuery)    {        $this->someModelQuery = $someModelQuery;    }}

This does two things. Provides a global scope method instance so you can always get at it. Along with allowing it to be reset. Then providing get and set methods for the model query object. With lazy loading if it has not already been set.

Now the code that does the real work:

class Foo{    public function doSomething()    {        return ServiceLocator::instance()            ->getSomeModelQuery()->filterByFoo('bar')->find();    }}

Foo calls the service locator, it then gets an instance of the query object from it and does the call it needs to on that query object.

So now we need to write some unit tests for all of this. Here it is:

class FooTest extends PHPUnit_Framework_TestCase{    protected function setUp()    {        ServiceLocator::resetInstance();    }    public function testNoMocking()    {        $foo = new Foo();        $this->assertEquals('Real Data', $foo->doSomething());    }    public function testWithMock()    {        // Create our mock with a random value        $rand = mt_rand();        $mock = $this->getMock('SomeModelQuery');        $mock->expects($this->any())            ->method('__call')            ->will($this->onConsecutiveCalls($mock, $rand));        // Place the mock in the service locator        ServiceLocator::instance()->setSomeModelQuery($mock);        // Do we get our random value back?        $foo = new Foo();        $this->assertEquals($rand, $foo->doSomething());    }}

I've given an example where the real query code is called and where the query code is mocked.

So this gives you the ability to inject mocks with out needing to inject every dependency into the classes you want to unit test.

There are many ways to write the above code. Use it as a proof of concept and adapt it to your need.