You are here

class AuthcacheP13nTestStubVerifyer in Authenticated User Page Caching (Authcache) 7.2

Simple invocation verifier class.

Hierarchy

Expanded class hierarchy of AuthcacheP13nTestStubVerifyer

File

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

View source
class AuthcacheP13nTestStubVerifyer {
  protected $name;
  protected $observer;
  protected $checks;

  /**
   * Construct new invocation verifier instance.
   *
   * Do not use directly, invocation verifier objects are returned by
   * AuthcacheP13nTestStubObserver::method().
   */
  public function __construct($name, $observer) {
    $this->name = $name;
    $this->observer = $observer;
    $this->checks = array();
  }

  /**
   * Add an expectation.
   */
  public function expect($checkfunc, $message = NULL) {
    $this->checks[] = $checkfunc;
    return $this;
  }

  /**
   * Test whether the the expectations are fullfilled.
   */
  public function verify(&$message) {
    $invocations = $this->observer
      ->invocations($this->name);
    $checks = $this->checks;
    if (empty($checks)) {
      $checks = array(
        AuthcacheP13nTestStubVerifyer::once(),
      );
    }
    foreach ($checks as $checkfunc) {
      if (!$checkfunc($invocations, $this->name, $message)) {
        return FALSE;
      }
    }
    return TRUE;
  }

  /**
   * Invocation verifier ensuring that a method was never invoked.
   */
  public static function never() {
    return static::times(0);
  }

  /**
   * Invocation verifier testing whether a method was invoked exacly once.
   */
  public static function once() {
    return static::times(1);
  }

  /**
   * Invocation verifier testing the number of times a method was invoked.
   */
  public static function times($expected) {

    // Necessary until #1272900 lands
    // @ignore style_function_spacing
    return function ($invocations, $name, &$message) use ($expected) {
      $actual = count($invocations);
      $message = format_plural($expected, '@name: Expecting one invocation, got @actual.', '@name: Expecting @expected invocations, got @actual.', array(
        '@name' => $name,
        '@expected' => $expected,
        '@actual' => $actual,
      ));
      return $expected === $actual;
    };
  }

  /**
   * Invocation verifier matching the arguments of a given invocation.
   */
  public static function args($args = array(), $invoc = 0) {

    // Necessary until #1272900 lands
    // @ignore style_function_spacing
    return function ($invocations, $name, &$message) use ($args, $invoc) {
      $message = t('@name: On invocation number @invoc, expecting arguments @this and got @that.', array(
        '@name' => $name,
        '@invoc' => $invoc,
        '@this' => var_export($args, TRUE),
        '@that' => var_export($invocations[$invoc], TRUE),
      ));
      return $invocations[$invoc] === $args;
    };
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthcacheP13nTestStubVerifyer::$checks protected property
AuthcacheP13nTestStubVerifyer::$name protected property
AuthcacheP13nTestStubVerifyer::$observer protected property
AuthcacheP13nTestStubVerifyer::args public static function Invocation verifier matching the arguments of a given invocation.
AuthcacheP13nTestStubVerifyer::expect public function Add an expectation.
AuthcacheP13nTestStubVerifyer::never public static function Invocation verifier ensuring that a method was never invoked.
AuthcacheP13nTestStubVerifyer::once public static function Invocation verifier testing whether a method was invoked exacly once.
AuthcacheP13nTestStubVerifyer::times public static function Invocation verifier testing the number of times a method was invoked.
AuthcacheP13nTestStubVerifyer::verify public function Test whether the the expectations are fullfilled.
AuthcacheP13nTestStubVerifyer::__construct public function Construct new invocation verifier instance.