You are here

class AuthcacheP13nTestDefaultRequestHandler in Authenticated User Page Caching (Authcache) 7.2

Test cases for default request handler.

Hierarchy

Expanded class hierarchy of AuthcacheP13nTestDefaultRequestHandler

File

modules/authcache_p13n/tests/authcache_p13n.request-handler.test, line 14
Define unit tests for request handler.

View source
class AuthcacheP13nTestDefaultRequestHandler extends DrupalUnitTestCase {
  protected $stubObserver;
  protected $coreService;
  protected $requestValidator;
  protected $contentBuilder;
  protected $contentEncoder;

  /**
   * {@inheritdoc}
   */
  public static function getInfo() {
    return array(
      'name' => "Default Request Handler",
      'description' => 'Unit tests for default request handler.',
      'group' => 'Authcache Personalization',
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    require_once __DIR__ . '/../includes/AuthcacheP13nDefaultRequestHandler.inc';
    $this->stubObserver = new AuthcacheP13nTestStubObserver();
    $this->coreService = new AuthcacheP13nTestCoreServiceStub($this->stubObserver);
    $this->requestValidator = new AuthcacheP13nTestRequestValidatorStub($this->stubObserver);
    $this->contentBuilder = new AuthcacheP13nTestContentBuilderStub($this->stubObserver);
    $this->contentEncoder = new AuthcacheP13nTestContentEncoderStub($this->stubObserver);
    parent::setUp();
  }

  /**
   * Test default configuration.
   */
  public function testDefaultRequestHandler() {
    $filters = array();
    $context_providers = array();
    $params = array(
      'some' => 'args',
    );

    // Setup expectations.
    $validate = $this->stubObserver
      ->method($this->requestValidator, 'validate', $params + array(
      'valid' => TRUE,
    ))
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      $params,
    )));
    $build = $this->stubObserver
      ->method($this->contentBuilder, 'build', 'output built successfully')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      $params + array(
        'valid' => TRUE,
      ),
      array(),
    )));
    $content_type = $this->stubObserver
      ->method($this->contentEncoder, 'contentType', 'application/test')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args());
    $encode = $this->stubObserver
      ->method($this->contentEncoder, 'encode', 'encoded successfully')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'output built successfully',
    )));
    $add_header = $this->stubObserver
      ->method($this->coreService, 'drupalAddHttpHeader')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'Content-Type',
      'application/test',
    )));

    // Run handler, test full sequence without filters / context providers /
    $handler = new AuthcacheP13nDefaultRequestHandler($this->coreService, $this->requestValidator, $this->contentBuilder, $this->contentEncoder, $filters, $context_providers);
    ob_start();
    $handler
      ->handle($params);
    $result = ob_get_clean();
    $this
      ->assertEqual('encoded successfully', $result);

    // Verify stubs.
    $this
      ->assert($add_header
      ->verify($message), $message);
    $this
      ->assert($validate
      ->verify($message), $message);
    $this
      ->assert($build
      ->verify($message), $message);
    $this
      ->assert($content_type
      ->verify($message), $message);
    $this
      ->assert($encode
      ->verify($message), $message);
  }

  /**
   * Test handler with request filter.
   */
  public function testDefaultRequestHandlerWithRequestFilter() {
    $filters = array(
      'request' => array(
        new AuthcacheP13nTestFilterStub($this->stubObserver),
        new AuthcacheP13nTestFilterStub($this->stubObserver),
      ),
    );
    $context_providers = array();
    $params = array(
      'some' => 'args',
    );

    // Setup expectations.
    $first_filter = $this->stubObserver
      ->method($filters['request'][0], 'filter', array(
      'some' => 'filtered args',
    ))
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'request',
      $params,
    )));
    $second_filter = $this->stubObserver
      ->method($filters['request'][1], 'filter', array(
      'some' => 'args after second filter',
    ))
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'request',
      array(
        'some' => 'filtered args',
      ),
    )));
    $validate = $this->stubObserver
      ->method($this->requestValidator, 'validate')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      array(
        'some' => 'args after second filter',
      ),
    )));

    // Run handler.
    $handler = new AuthcacheP13nDefaultRequestHandler($this->coreService, $this->requestValidator, $this->contentBuilder, $this->contentEncoder, $filters, $context_providers);
    ob_start();
    $handler
      ->handle($params);
    ob_end_clean();

    // Verify stubs.
    $this
      ->assert($first_filter
      ->verify($message), $message);
    $this
      ->assert($second_filter
      ->verify($message), $message);
    $this
      ->assert($validate
      ->verify($message), $message);
  }

  /**
   * Test handler with response filter.
   */
  public function testDefaultRequestHandlerWithResponseFilter() {
    $filters = array(
      'response' => array(
        new AuthcacheP13nTestFilterStub($this->stubObserver),
        new AuthcacheP13nTestFilterStub($this->stubObserver),
      ),
    );
    $context_providers = array();
    $params = array(
      'some' => 'args',
    );

    // Setup expectations.
    $encode = $this->stubObserver
      ->method($this->contentEncoder, 'encode', 'encoded output')
      ->expect(AuthcacheP13nTestStubVerifyer::once());
    $first_filter = $this->stubObserver
      ->method($filters['response'][0], 'filter', 'output after first filter')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'response',
      'encoded output',
    )));
    $second_filter = $this->stubObserver
      ->method($filters['response'][1], 'filter', 'output after second filter')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      'response',
      'output after first filter',
    )));

    // Run handler.
    $handler = new AuthcacheP13nDefaultRequestHandler($this->coreService, $this->requestValidator, $this->contentBuilder, $this->contentEncoder, $filters, $context_providers);
    ob_start();
    $handler
      ->handle($params);
    $result = ob_get_clean();
    $this
      ->assertEqual('output after second filter', $result);

    // Verify stubs.
    $this
      ->assert($encode
      ->verify($message), $message);
    $this
      ->assert($first_filter
      ->verify($message), $message);
    $this
      ->assert($second_filter
      ->verify($message), $message);
  }

  /**
   * Test handler with context provider.
   */
  public function testDefaultRequestHandlerWithContextProvider() {
    $filters = array();
    $context_providers = array(
      'my context' => new AuthcacheP13nTestContextProviderStub($this->stubObserver),
      'other context' => new AuthcacheP13nTestContextProviderStub($this->stubObserver),
    );
    $params = array(
      'some' => 'args',
    );
    $validate = $this->stubObserver
      ->method($this->requestValidator, 'validate', $params + array(
      'valid' => TRUE,
    ))
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      $params,
    )));

    // Setup expectations.
    $cp_1 = $this->stubObserver
      ->method($context_providers['my context'], 'get', 'some value')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      $params + array(
        'valid' => TRUE,
      ),
    )));
    $cp_2 = $this->stubObserver
      ->method($context_providers['other context'], 'get', array(
      'other stuff',
    ))
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      $params + array(
        'valid' => TRUE,
      ),
    )));
    $build = $this->stubObserver
      ->method($this->contentBuilder, 'build')
      ->expect(AuthcacheP13nTestStubVerifyer::once())
      ->expect(AuthcacheP13nTestStubVerifyer::args(array(
      $params + array(
        'valid' => TRUE,
      ),
      array(
        'my context' => 'some value',
        'other context' => array(
          'other stuff',
        ),
      ),
    )));

    // Run handler.
    $handler = new AuthcacheP13nDefaultRequestHandler($this->coreService, $this->requestValidator, $this->contentBuilder, $this->contentEncoder, $filters, $context_providers);
    ob_start();
    $handler
      ->handle($params);
    ob_end_clean();

    // Verify stubs.
    $this
      ->assert($validate
      ->verify($message), $message);
    $this
      ->assert($cp_1
      ->verify($message), $message);
    $this
      ->assert($cp_2
      ->verify($message), $message);
    $this
      ->assert($build
      ->verify($message), $message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthcacheP13nTestDefaultRequestHandler::$contentBuilder protected property
AuthcacheP13nTestDefaultRequestHandler::$contentEncoder protected property
AuthcacheP13nTestDefaultRequestHandler::$coreService protected property
AuthcacheP13nTestDefaultRequestHandler::$requestValidator protected property
AuthcacheP13nTestDefaultRequestHandler::$stubObserver protected property
AuthcacheP13nTestDefaultRequestHandler::getInfo public static function
AuthcacheP13nTestDefaultRequestHandler::setUp public function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp
AuthcacheP13nTestDefaultRequestHandler::testDefaultRequestHandler public function Test default configuration.
AuthcacheP13nTestDefaultRequestHandler::testDefaultRequestHandlerWithContextProvider public function Test handler with context provider.
AuthcacheP13nTestDefaultRequestHandler::testDefaultRequestHandlerWithRequestFilter public function Test handler with request filter.
AuthcacheP13nTestDefaultRequestHandler::testDefaultRequestHandlerWithResponseFilter public function Test handler with response filter.
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$setup protected property Flag to indicate whether the test has been set up.
DrupalTestCase::$setupDatabasePrefix protected property
DrupalTestCase::$setupEnvironment protected property
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::$useSetupInstallationCache public property Whether to cache the installation part of the setUp() method.
DrupalTestCase::$useSetupModulesCache public property Whether to cache the modules installation part of the setUp() method.
DrupalTestCase::$verboseDirectoryUrl protected property URL to the verbose output file directory.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion. 1
DrupalTestCase::errorHandler public function Handle errors during test runs. 1
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::getDatabaseConnection public static function Returns the database connection to the site running Simpletest.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs a verbose message in a text file.
DrupalUnitTestCase::tearDown protected function 1
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct