You are here

class AuthcacheP13nTestStubObserver in Authenticated User Page Caching (Authcache) 7.2

Simple invocation recorder class.

Hierarchy

Expanded class hierarchy of AuthcacheP13nTestStubObserver

File

modules/authcache_p13n/tests/authcache_p13n.stub.inc, line 10
Stub classes for testing.

View source
class AuthcacheP13nTestStubObserver {
  protected $invocations;
  protected $returns;
  protected $objects;
  protected $classes;

  /**
   * Construct new stub observer instance.
   */
  public function __construct() {
    $this->classes = array();
    $this->objects = array();
    $this->invocations = array();
    $this->returns = array();
  }

  /**
   * Setup invocation verifier and return-value for a method.
   *
   * @return AuthcacheP13nTestStubVerifyer
   *   New invocation verifier instance.
   */
  public function method($object, $method, $return = NULL) {
    $name = $this
      ->boundMethodName($object, $method);
    $this->returns[$name] = $return;
    $this->invocations[$name] = array();
    return new AuthcacheP13nTestStubVerifyer($name, $this);
  }

  /**
   * Record one method invocation.
   */
  public function record($object, $method, $arguments) {
    $name = $this
      ->boundMethodName($object, $method);

    // Record invocation.
    $this->invocations[$name][] = $arguments;
    $result = isset($this->returns[$name]) ? $this->returns[$name] : NULL;
    if (is_object($result) && $result instanceof Exception) {
      throw $result;
    }

    // Return result.
    return $result;
  }

  /**
   * Return recorded invocations.
   */
  public function invocations($name) {
    return $this->invocations[$name];
  }

  /**
   * Compute the name for a bound method.
   */
  public function boundMethodName($object, $method) {
    $id = spl_object_hash($object);
    if (isset($this->objects[$id])) {
      $instance_name = $this->objects[$id]['name'];
    }
    else {
      $class_name = get_class($object);
      if (!isset($this->classes[$class_name])) {
        $this->classes[$class_name] = 0;
      }
      $instance_counter = $this->classes[$class_name]++;
      $instance_name = $class_name . '_' . $instance_counter;
      $this->objects[$id] = array(
        'instance' => $object,
        'name' => $instance_name,
      );
    }
    return $instance_name . '::' . $method;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthcacheP13nTestStubObserver::$classes protected property
AuthcacheP13nTestStubObserver::$invocations protected property
AuthcacheP13nTestStubObserver::$objects protected property
AuthcacheP13nTestStubObserver::$returns protected property
AuthcacheP13nTestStubObserver::boundMethodName public function Compute the name for a bound method.
AuthcacheP13nTestStubObserver::invocations public function Return recorded invocations.
AuthcacheP13nTestStubObserver::method public function Setup invocation verifier and return-value for a method.
AuthcacheP13nTestStubObserver::record public function Record one method invocation.
AuthcacheP13nTestStubObserver::__construct public function Construct new stub observer instance.