class ProtectedPrivates in Examples for Developers 3.x
Same name and namespace in other branches
- 8 phpunit_example/src/ProtectedPrivates.php \Drupal\phpunit_example\ProtectedPrivates
A class with features to show how to do unit testing.
This class has private and protected methods to demonstrate how to test with reflection and mocking.
protectedAdd() and privateAdd() are shim methods to AddClass::add(). We do this so we're concentrating on the testing instead of the code being tested.
Hierarchy
- class \Drupal\phpunit_example\ProtectedPrivates
Expanded class hierarchy of ProtectedPrivates
Related topics
2 files declare their use of ProtectedPrivates
- ProtectedPrivatesSubclass.php in modules/
phpunit_example/ tests/ src/ Unit/ Subclasses/ ProtectedPrivatesSubclass.php - ProtectedPrivatesTest.php in modules/
phpunit_example/ tests/ src/ Unit/ ProtectedPrivatesTest.php
File
- modules/
phpunit_example/ src/ ProtectedPrivates.php, line 17
Namespace
Drupal\phpunit_exampleView source
class ProtectedPrivates {
/**
* A simple addition method with validity checking.
*
* @param int|float $a
* A number to add.
* @param int|float $b
* Another number to add.
*
* @return int|float
* The sum of $a and $b.
*
* @throws \InvalidArgumentException
* If either $a or $b is non-numeric, we can't add, so we throw.
*/
protected function protectedAdd($a, $b) {
$adder = new AddClass();
return $adder
->add($a, $b);
}
// phpcs:disable
/**
* A simple addition method with validity checking.
*
* @param int|float $a
* A number to add.
* @param int|float $b
* Another number to add.
*
* @return int|float
* The sum of $a and $b.
*
* @throws \InvalidArgumentException
* If either $a or $b is non-numeric, we can't add, so we throw.
*/
private function privateAdd($a, $b) {
$adder = new AddClass();
return $adder
->add($a, $b);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ProtectedPrivates:: |
private | function | A simple addition method with validity checking. | |
ProtectedPrivates:: |
protected | function | A simple addition method with validity checking. |