Mocking at architectural boundaries: persistence and time

Posted on by Matthias Noback

More and more I've come to realize that I've been mocking less and less.

The thing is, creating test doubles is a very dangerous activity. For example, what I often see is something like this:

$entityManager = $this->createMock(EntityManager::class);


Or, what appears to be better, since we'd be mocking an interface instead of a concrete class:

$entityManager = $this->createMock(ObjectManagerInterface::class);
// ...

To be very honest, there isn't a big different between these two examples. If this code is in, for example, a unit test for a repository class, we're not testing many of the aspects of the code that should have been tested instead.

For example, by creating a test double for the EntityManager, we're assuming that it will work well with any objects we'll pass to it. If you've ever debugged an issue with an EntityManager, you know that this is a bad assumption. Anything may go wrong: a mistake in the mapping, missing configuration for cascading persist/delete behavior, an issue with the database credentials, availability of the database server, network connectivity, a missing or invalid database schema, etc.

In short, a unit test like this doesn't add any value, except that it verifies correct execution of the code you wrote (something a linter or static analysis tool may be able to do as well). There's nothing in this test that ensures a correct working once the code has been deployed and is actually being used.

The general rule to apply here is "Don't mock what you don't own" (see the excellent book "Growing Object-Oriented Software, Guided by Tests", or an article on the topic by Eric Smith, "That's Not Yours"). Whenever I've brought up this rule in discussions with developers, I've always met with resistance. "What else is there to mock?" "Isn't mocking meant to replace the slow, fragile stuff with something that is fast and stable?"

Mock across architecturally significant boundaries

Of course we want to use mocks for that. And we need to, since our test suite will become very slow and fragile if we don't do it. But we need to do it in the right place: at the boundaries of our application.

My reasoning for "when to mock" is always:

  1. If you encounter the need for some information or some action that isn't available to you in the memory of the the currently running program, define an interface that represents your query (in case you need to know something) or command (in case you want to do something). Put the interface in the "core" of your application (in the domain or application layer).
  2. Use this interface anywhere you want to send this query or command.
  3. Write at least one implementation for the interface, and make sure all the clients of the interface get this implementation injected as constructor arguments.

Mocking "persistence"

To fix the EntityManager example above we need to take a step back and articulate our reason for using the EntityManager in the first place. Apparently, we were in need of persisting an object. This is not something the running application could do naturally (the moment it stops, it forgets about any object it has in memory). So we had to reach across the application's boundaries, to an external service called a "database".

Because we always considering reusing things that are already available in our project, we just decided to go with the previously installed EntityManager to fulfill our needs. If however we would've followed the steps described above, we would've ended up in a different place:

  1. I need to persist (not just any object, but) my Article entity, so I define an interface that represents the action I intend for it to do:

    interface ArticleRepository
        public function persist(Article $article): void
  2. I use this interface everywhere in my code.

  3. I provide a default implementation for it, one that uses my beloved EntityManager:

    final class ORMArticleRepository implements ArticleRepository
        public function persist(Article $article): void
            // here we *will* use the EntityManager 

Note that there's nothing about EntityManagers in the interface we've defined. Also, there's no generic object-type parameter, but a very specific one (Article). Finally, there's nothing "crazy" about the interface, like first having to call persist() and then flush(). It's now one thing: we want the ArticleRepository to take care of persisting an Article entity.

Mocking "time"

Another example: I need to know what the current date/time is. This information isn't available inside the running program; I need to reach outside of it. Normally, I'd simply summon the language's native date/time representation of "now", i.e. new DateTime('now'). Instead, following the same steps as described above:

  1. I define an interface that represents my query:

    interface Clock
        public function currentTime(): DateTime;
  2. I use this interface in my code:

    final class ArticleRepository
        public function __construct(Clock $clock)
            // ...
        public function findNotYetPublishedArticles(): array
            $now = $this->clock->currentTime();
            // look for articles that will be published 
            // at some point in the future...
  3. I write a standard implementation for it, one that uses the most common way of determining the current date/time. This involves the system clock and the server's time zone:

    final class SystemClock implements Clock
        public function currentTime(): DateTime
            return new DateTime('now');

In unit tests for the core of your application, you can safely mock Clock to give you some deterministic return value. The unit test will be a pure unit test, since it won't involve a system call to get the current date/time (as opposed to when we'd just call new DateTime('now') on the spot).

Consider passing time as context

In terms of dependency injection versus context passing, you should also consider determining the current time once in the infrastructure layer (e.g. in the controller), and passing it as a value to any other objects that need this information.

Since we'll use a test double for Clock everywhere, the code in SystemClock won't be exercised during a test run. So we need another kind of test, a so-called integration test, which can prove that the SystemClock functions correctly, given an actual system clock is available to retrieve the current time.

Your unit tests will be fast, and your integration test will show you any mistakes in your assumptions about the third-party code or hardware that you're implicitly relying on.

Replacing language features

Another style of "mocking" time involves overriding built-in time-related functions. A pretty smart solution is provided by Symfony's PHPUnit Bridge library. It overwrites calls like time() and sleep(). Though this library is useful for testing low-level infrastructure/framework code, I don't recommend to use this library in scenarios that are like the one above, for two reasons:

  • Only part of the quality improvement comes from faster running test suites. The biggest improvement comes from defining your own abstractions. With it comes a more clear sense of what it is you need: do you need the current date, the current time, then with what precision, is a timezone involved, or even relevant, etc.
  • Replacing system calls involves assumptions about what the behavior in real life will be. These assumptions go untested and will result in hard-to-figure out bugs.

Mocking with architectural consequences

Another big benefit of this approach is that it forces you to think through what your significant architectural boundaries are; and enforce them with polymorphic interfaces. This allows you to manage the dependencies across those boundaries so that you can independently deploy (and develop) the components on either side of the boundary.

Uncle Bob, "When to Mock"

By following the above steps, you've basically been applying the Dependency Inversion Principle. And you'll be better off with that. The results are:

  • Loose coupling; you have the option to change the implementation, without touching all the clients of the interface.
  • Intention-revealing interfaces: you'll have better interfaces that define your questions in a better way than the framework/library/language feature's interface does.

If you keep making these interfaces for things that deal with "the world outside your application", you'll end up with two things:

  • An accidentally emerging infrastructure "layer", which contains all the code that connects to external services, data providers and devices.
  • An accidentally emerging hexagonal architecture architecture that is good for testing, since it allows using application features without all those slow and brittle external services, data providers and devices.

To me it's fascinating how proper techniques for creating test doubles could make a difference at the level of an application's architecture.


We've discussed two common areas of an application that need mocking - persistence and time. We've also seen how correctly mocking things has a healthy effect on your application's architecture. In another article we'll discuss two other things that need mocking - the filesystem, the network and... randomness.

PHP testing mocking hexagonal architecture