loading...

How to use reflection to test private and protected methods

daniel_werner profile image Daniel Werner Originally published at 42coders.com on ・2 min read

Developing application with TDD methodology we tend to achieve the highest possible code coverage, including private and protected methods as well. But writing unit test for a non public method is not trivial. Changing the visibility for the sake of unit tests is not a good idea. In these cases we can use php’s reflection to make those methods accessible, let’s see how.

Methods

Create new Reflection class from our class:

$class = new \ReflectionClass('MyNamespace\MyClass');

Get the private/protected method:

$myProtectedMethod = $class->getMethod('myProtectedMethod');

Make it accessible:

$myProtectedMethod->setAccessible(true);

Create and instance from our class:

$myInstance = new MyClass();

Call the method with arguments on the instance created above:

$result = $myProtectedMethod->invokeArgs($myInstance, [$argument1, $argument2]);

Properties

In similar way we can we can change the visibility of the non public properties of a class. This could be useful when we want to unit test edge cases or error handling, and we want to change the internal state of the instance to simulate the erroneous behaviour.

Create new Reflection class from our class:

$class = new \ReflectionClass('MyNamespace\MyClass');

Get the private/protected property:

$myProtectedProperty = $class->getProperty(myProtectedProperty);

Make it accessible:

$myProtectedProperty->setAccessible(true);

Create and instance from our class:

$myInstance = new MyClass();

Set the value of the property on the created instance:

$myProtectedProperty->setValue($myInstance, 'value');

You can find more information about the reflection in the php documentation: https://www.php.net/manual/en/book.reflection.php

The post How to use reflection to test private and protected methods appeared first on 42 Coders.

Posted on Jul 17 '19 by:

daniel_werner profile

Daniel Werner

@daniel_werner

Senior Software Developer. Clean Code and TDD enthusiast. Team lead and mentor.

Discussion

markdown guide
 

You generally shouldn't test private members directly.

You should be testing that your implementation meets it's interface by writing tests that use your code as normal.

This is what TDD means when it says that "testable" code is well designed code. If you find yourself needing reflection in order to write tests, your code is not testable (and thus possibly needs better design)

If using your code requires inkoking t private members, they shouldn't be private. If you don't need to invoke private members to use your module, then neither should your tests.

For example, via inversion of control / dependency injection, you can guide the unit under test into whatever state you want by passing "fake" implementations of its dependencies which, for example, trigger error conditions. This also leads to production code that has more separated responsibilities that is easier to refactor (or delete!) since
your code does fewer things in each place.

 

This is a highly opinion-based topic, if you should or shouldn't test private/protected methods, just see the discussion here: stackoverflow.com/questions/105007...

I agree with the concepts you mentioned, but in some cases, especially when working with legacy code, creating unit tests for those methods could be beneficial. Also the tests can ensure that you don't make breaking changes in private methods.

 

The stackoverflow link you posted shows that the winner of the discussion (marked as accepted and higher votes) is the answer that tells you NOT to test private methods.

If a private method is changed, the tests for the public methods should pick it up, via a difference in the expected output of the public methods that use the private method.