You are here

trait InvokeMethodTrait in Search API Solr 4.x

Same name and namespace in other branches
  1. 8.3 tests/src/Traits/InvokeMethodTrait.php \Drupal\Tests\search_api_solr\Traits\InvokeMethodTrait
  2. 8 tests/src/Traits/InvokeMethodTrait.php \Drupal\Tests\search_api_solr\Traits\InvokeMethodTrait
  3. 8.2 tests/src/Traits/InvokeMethodTrait.php \Drupal\Tests\search_api_solr\Traits\InvokeMethodTrait

Provides a function to invoke protected/private methods of a class.

Hierarchy

3 files declare their use of InvokeMethodTrait
SearchApiBackendUnitTest.php in tests/src/Unit/SearchApiBackendUnitTest.php
SearchApiSolrTest.php in tests/src/Kernel/SearchApiSolrTest.php
SolrFieldNamesTest.php in tests/src/Kernel/SolrFieldNamesTest.php

File

tests/src/Traits/InvokeMethodTrait.php, line 8

Namespace

Drupal\Tests\search_api_solr\Traits
View source
trait InvokeMethodTrait {

  /**
   * Calls protected/private method of a class.
   *
   * @param object &$object
   *   Instantiated object that we will run method on.
   * @param string $methodName
   *   Method name to call.
   * @param array $parameters
   *   Array of parameters to pass into method.
   * @param array $protectedProperties
   *   Array of values that should be set on protected properties.
   *
   * @return mixed
   *   Method return.
   */
  protected function invokeMethod(&$object, $methodName, array $parameters = [], array $protectedProperties = []) {
    $reflection = new \ReflectionClass(get_class($object));
    foreach ($protectedProperties as $property => $value) {
      $property = $reflection
        ->getProperty($property);
      $property
        ->setAccessible(TRUE);
      $property
        ->setValue($object, $value);
    }
    $method = $reflection
      ->getMethod($methodName);
    $method
      ->setAccessible(TRUE);
    return $method
      ->invokeArgs($object, $parameters);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
InvokeMethodTrait::invokeMethod protected function Calls protected/private method of a class.