In this post, I will explain how to test private method with PHP Unit.
Using reflection, we can test these methods.

Reflection

Reflection is described as below in wikipedia article, Reflection (computer programming).

In computer science, reflection is the ability of a computer program to examine, introspect, and modify its own structure and behavior at runtime.

Call a private method from outside

PHP has Reflection class to do reflection.

Using this class, you can load the object you want to test.
Then, you can modify and make private method accesessible from outside.

To test getName method of User class, you can use this code.

<?php
$user   = new User();
$method = new ReflectionMethod($user, 'getName');
$method->setAccessible(true);
$method->invoke($user));

Generalization

I generalized these codes.

<?php
abstract class TestAbstract extends TestCase
{
    /**
     * Get ReflectionMethod
     *
     * @param mixed $testClassInstance
     * @param string $testMethodName
     * @return \ReflectionMethod
     */
    public function getReflectionMethod($testClassInstance, $testMethodName) {
        $reflectionClass = new \ReflectionClass($testClassInstance);
        $reflectionMethod = $reflectionClass->getMethod($testMethodName);
        $reflectionMethod->setAccessible(true);
        return $reflectionMethod;
    }

    /**
     * Test private method
     *
     * @param mixed $testClassInstance
     * @param string $testMethodName
     * @param array $testArgs
     * @param callable $assertAction
     * @return \ReflectionMethod
     */
    public function testPrivateMethod(
        $testClassInstance,
        $testMethodName,
        $testArgs,
        $assertAction
    ) {
        $reflectionMethod = $this->getReflectionMethod(
            $testClassInstance,
            $testMethodName
        );
        $retVal = $reflectionMethod->invokeArgs($testClassInstance, $testArgs);
        $assertAction($retVal);
    }
}

You can use it like this.

<?php
// Test sum method of Math class.
$math = new Math();
$this->testPrivateMethod(
    $math,
    'sum',
    [1000, 100],
    function ($retVal) {
        $this->assertEquals($retVal, 1100);
    }
);