function ProtectedPrivatesTest::testPrivateAdd
Same name in other branches
- 3.x modules/phpunit_example/tests/src/Unit/ProtectedPrivatesTest.php \Drupal\Tests\phpunit_example\Unit\ProtectedPrivatesTest::testPrivateAdd()
- 4.0.x modules/phpunit_example/tests/src/Unit/ProtectedPrivatesTest.php \Drupal\Tests\phpunit_example\Unit\ProtectedPrivatesTest::testPrivateAdd()
- 4.0.x modules/testing_example/tests/src/Unit/ProtectedPrivatesTest.php \Drupal\Tests\test_example\Unit\ProtectedPrivatesTest::testPrivateAdd()
Test ProtectedPrivate::privateAdd().
We want to test a private method on a class. This is problematic because, by design, we don't have access to this method. However, we do have a tool available to help us out with this problem: We can override the accessibility of a method using reflection.
@dataProvider addDataProvider
File
-
phpunit_example/
tests/ src/ Unit/ ProtectedPrivatesTest.php, line 72
Class
- ProtectedPrivatesTest
- ProtectedPrivates unit testing of restricted methods.
Namespace
Drupal\Tests\phpunit_example\UnitCode
public function testPrivateAdd($expected, $a, $b) {
// Get a reflected, accessible version of the privateAdd() method.
$private_method = $this->getAccessibleMethod('Drupal\\phpunit_example\\ProtectedPrivates', 'privateAdd');
// Create a new ProtectedPrivates object.
$pp = new ProtectedPrivates();
// Use the reflection to invoke on the object.
$sum = $private_method->invokeArgs($pp, [
$a,
$b,
]);
// Make an assertion.
$this->assertEquals($expected, $sum);
}