You are here

class ProtectedPrivatesSubclass in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 phpunit_example/tests/src/Unit/Subclasses/ProtectedPrivatesSubclass.php \Drupal\Tests\phpunit_example\Unit\Subclasses\ProtectedPrivatesSubclass

A class for testing ProtectedPrivate::protectedAdd().

We could use reflection to test protected methods, just as with private ones. But in some circumstances it might make more sense to make a subclass and then run the tests against it.

This subclass allows us to get access to the protected method.

Hierarchy

Expanded class hierarchy of ProtectedPrivatesSubclass

Related topics

1 file declares its use of ProtectedPrivatesSubclass
ProtectedPrivatesTest.php in modules/phpunit_example/tests/src/Unit/ProtectedPrivatesTest.php

File

modules/phpunit_example/tests/src/Unit/Subclasses/ProtectedPrivatesSubclass.php, line 18

Namespace

Drupal\Tests\phpunit_example\Unit\Subclasses
View source
class ProtectedPrivatesSubclass extends ProtectedPrivates {

  /**
   * A stub class so we can access a protected method.
   *
   * We use a naming convention to make it clear that we are using a
   * shimmed method.
   */
  public function subclassProtectedAdd($a, $b) {
    return $this
      ->protectedAdd($a, $b);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ProtectedPrivates::privateAdd private function A simple addition method with validity checking.
ProtectedPrivates::protectedAdd protected function A simple addition method with validity checking.
ProtectedPrivatesSubclass::subclassProtectedAdd public function A stub class so we can access a protected method.