Mocking the Entity Manager


You're a lousy Entity Manager and you're doing a bad job. You're a very naughty Entity Manager.

No, not that kind of Mocking. I know, I know, such wit.

Ok, fun time over, back to business.

In this video we're going to be looking at how we can create a Mock of our Entity Manager, allowing us to effectively fake our requests to, and responses from our Database.

Why might we want to do this? The biggest reason is speed. Without having to talk to our database - a slow process, in computer time frames - we will have faster tests. Faster tests mean we are more likely to regularly run our test suite, and after all, if we aren't running our test suite regularly, why are we investing time in creating and maintaining it?

The code used in the example for this video is a hybrid of the example from the How to Test Code that Interacts with the Database cookbook article on Symfony.com.

The first time you create some PHP Unit tests (whether in Codeception, PHPUnit itself, or otherwise) that have mocks that return mocks that return yet more mocks can be quite confusing. It's even more confusing when someone new to the project, or more specifically, new to PHP Unit testing in general has to look at and understand that mock structure before they can benefit from what the test is telling them.

Personally, I find having someone walk through an example, maybe explaining it slightly differently to the written documentation, and going over it step by step is incredibly helpful.

Hopefully you do to.

Code For This Course

Get the code for this course.

Code For This Video

Get the code for this video.

Episodes