Unit Test for Private and Protected Methods with PHPUnit Magento 2.

PHPUnit used to test your code coverage. You can easily write a Public method test case with PHPUnit.

Writing Unit Test case for private and protected methods in PHP language can cause trouble due to you can’t call private and protected methods directly But they should be tested using a different approach.

Using theĀ ReflectionMethod class invoke() method,
It’s possible to test both methods. In Magento Core code, few methods are tested with Private and Protected test case.

Example:
Core Sales Module contains a protected method,

File path: vendor/magento/module-sales/Block/Adminhtml/Order/Invoice/View.php

Check core Test file, vendor/magento/module-sales/Test/Unit/Block/Adminhtml/Order/Invoice/ViewTest.php

You can see we have created Mock for View.php file using $block.

Test Protected method, You need to call the ReflectionMethod class invoke method.

Defination:
ReflectionMethod(Original Class,Function from Original class)
Original Class: Magento\Sales\Block\Adminhtml\Order\Invoice\View.php
Function from Original class: protected _isPaymentReview

Now Using invoke method,$testMethod->invoke($block)
and check for equal assersion test.

Run Magento 2 Test using
php vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist vendor/magento/module-sales/Test/Unit/Block/Adminhtml/Order/Invoice/ViewTest.php

Result will be success test.