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:

Then run deps update:

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

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):

 

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:

Mockery’s way:

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.