Stub ParamConverter in phpunit

Grégoire Willmann
Mar 30, 2018 · 3 min read

This week I spent exactly 5 hours and 38 minutes trying to stub what Sensio’s ParamConverter would return in my tests…

So I thought it would be a good idea to share what I learned on this journey and how I managed to do it.

If you have a better way of doing this: please come forward 🙏 because my solution feels kind of hacky.

For more information on ParamConverter, checkout the official documentation:

Basically what ParamConverter does is automatically fetch an object for you without having to deal with EntityManagers.

The code we want to test

Suppose you have a UserController.php with a show action:

* @Route('/user/{id}', name='show_user')
public function showAction(User $user)
return $this->render(
'user/show.html.twig', [ 'user' => $user ]

when you call the URL :, Symfony will automatically fetch the user with the id 1 from the database.

The tests

Suppose now you want to test the fact that the page actually renders successfully (http 200 status).

BUT In order to keep our tests fast we don’t want them to actually fetch anything from the database. So we create a fake user and feed it to our stubbed ParamConverter.

We create a UserControllerTest.php

class UserControllerTest extends WebTestCase 
function setUp()
$this->client = static::createClient();
function testItRendersShowPage()
// Insert something to tell phpunit not fetch the real user
// from the database

$crawler = $this->client->request('GET', '/user/1');
200, $this->client->getResponse()->getStatusCode()

Here comes the hard part. When a request is made, a listener (ParamConverterListener) checks whether a param has to be converted. If there is one, a ParamConverterManager returns a list of available ParamConverters which is looped over to find one that supports this type of parameter. If one is found (DoctrineParamConverter in our case), it fires a ManagerRegistry that will call the appropriate ObjectManager to fetch the entity from the database thanks to its EntityRepository. wow….

Here is a diagram to visualize things a bit:

The worst part is we need to stub ALL of these classes 😣

Let’s do it:

First we create our fake user:

$user = new User();

then we mock its EntityRepository, and stub its find method to return our fake user:

$entityRepository = $this->getMockBuilder(EntityRepository::class)

then we mock the ObjectManager, and stub its getRepository method to return our mocked EntityRepository:

$objectManager = $this->getMockBuilder(ObjectManager::class)

then we mock the ManagerRegistry, and stub its getManagerForClass method to return our mocked ObjectManager:

$managerRegistry = $this->getMockBuilder(ManagerRegistry::class)

then we mock the DoctrineParamConverter class, and stub its __construct and supports methods to make it use our mocked ManagerRegistry:

$doctrineParamConverter = 
->setMethods(array('__construct', 'supports'))

then we mock the ParamConverterManager class, and stub its add and all methods to return an array of available ManagerRegistry with our mocked DoctrineParamConverter class:

$paramManager = $this->getMockBuilder(ParamConverterManager::class)
->setMethods(array('add', 'all'))
$paramManager->add($doctrineParamConverter, 10);

finally we mock the ParamConverterListener and stub its __construct method to use our mocked ParamConverterManager:

$paramListener = 

Last but not least, we tell phpunit to use our mocked ParamConverterListener service:

->set('sensio_framework_extra.converter.listener', $paramListener);

That’s it for today!

Here is the full UserControllerTest:

Lastly don’t forget to refactor this code in order to make it more reusable.

Happy testing!

Grégoire Willmann

Written by

Head of infrastructure at Kinoba | Ruby On Rails expert 👾 | Developing innovative web platforms for our tribe (

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade