class ProtectedPrivates
Same name in other branches
- 3.x modules/phpunit_example/src/ProtectedPrivates.php \Drupal\phpunit_example\ProtectedPrivates
- 8.x-1.x 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);
}
// phpcs:enable
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
ProtectedPrivates::privateAdd | private | function | A simple addition method with validity checking. |
ProtectedPrivates::protectedAdd | protected | function | A simple addition method with validity checking. |