PHP Snippet: Unit Testing private methods

When testing private or protected methods, the most common approach is to create an invokeMethod helper that, using the Reflection API, will invoke the non public method with the desired.

Most of the times, it looks like this:

$this->invokeMethod($formatter, 'formatUser', [$user]);

The issue with this implementation is that it does not look very intuitive.

Instead, I would suggest to create a wrapper class:

`private_access($user)->encryptPassword(‘abcde’);`

Here is the code, if you’re interested:

If you found this useful, you should check out my twitter for more! @juampi_92

--

--