Recently I did a lot of Test Driven Development on my Symfony2 bundle. I used PHPUnit’s built-in mocks and stubs for many projects, so I took it again. But while I was working on mocking Symfony2 core objects I found those mocks very uncomfortable in use. I tried Mockery and it saved my day. Let’s see how to get it working with Symfony2 and how it kicks ass!

First of all, we need to put mockery entry in deps file:

[gist id=”1331766″]

Then run deps update:

[gist id=” 1307262″]

Last thing is to add Mockery loader in app/autoload.php:

[gist id=”1331773″]

Alternatively, you can add Mockery Listener (it’s recomended to do this to have good code coverage reports and to avoid closing manually Mockery „session” on every tear down):

[gist id=”1331779″]

 

Well, you could say: I have mocks and stubs in PHPUnit, why should I use anything else? I hope below example will be verbose enough to convince you.

PHPUnit’s  way to mock Doctrine2 Entity Manager:

[gist id=”1331789″]

Mockery’s way:

[gist id=”1331795″]

As you can see, Mockery has a very simple API, you can achieve same effect with minimal effort and also it has huge number of other options that help with mocking and testing your code.