You are here

abstract class OgContextCacheContextTestBase in Organic groups 8

Base class for testing cache contexts that rely on OgContext.

Use this for testing cache contexts that vary by the group that is active in the current context, as determined by OgContext::getGroup().

Hierarchy

Expanded class hierarchy of OgContextCacheContextTestBase

See also

\Drupal\og\OgContextInterface

File

tests/src/Unit/Cache/Context/OgContextCacheContextTestBase.php, line 18

Namespace

Drupal\Tests\og\Unit\Cache\Context
View source
abstract class OgContextCacheContextTestBase extends OgCacheContextTestBase {

  /**
   * The mocked OG context service.
   *
   * @var \Drupal\og\OgContextInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $ogContext;

  /**
   * A mocked group entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface|\Prophecy\Prophecy\ObjectProphecy
   */
  protected $group;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->ogContext = $this
      ->prophesize(OgContextInterface::class);
    $this->group = $this
      ->prophesize(EntityInterface::class);
  }

  /**
   * Tests the result of the cache context service with active context objects.
   *
   * This tests the most common use case: the service retrieves data from the
   * active context, and will be able to provide a relevant cache context string
   * in accordance with the provided data.
   *
   * @param mixed $context
   *   Data used to set up the expectations of the context objects. See
   *   setupExpectedContext().
   * @param string $expected_result
   *   The cache context string which is expected to be returned by the service
   *   under test.
   *
   * @covers ::getContext
   * @dataProvider contextProvider
   */
  public function testWithContext($context, $expected_result) {
    $this
      ->setupExpectedContext($context);
    $result = $this
      ->getContextResult();
    $this
      ->assertEquals($expected_result, $result);
  }

  /**
   * Tests the result of the cache context service without active context.
   *
   * @covers ::getContext
   */
  public abstract function testWithoutContext();

  /**
   * Provides test data for the test with active context objects.
   *
   * @return array
   *   An array of test data arrays, each array having two elements:
   *   1. The test data that is used to set up the active context.
   *   2. The cache context string that is expected to be returned by the cache
   *      context service being tested.
   *
   * @see ::testWithContext()
   */
  public abstract function contextProvider();

  /**
   * Set up expectations for tests that have an active context object.
   *
   * @param mixed $context
   *   The test data for the active context, as provided by contextProvider().
   *
   * @see ::contextProvider()
   */
  protected abstract function setupExpectedContext($context);

  /**
   * Sets an expectation that OgContext will return the given group.
   *
   * @param \Drupal\Core\Entity\EntityInterface|null $group
   *   The group to return, or NULL if no group is expected to be returned by
   *   OgContext.
   */
  protected function expectGroupContext(?EntityInterface $group = NULL) {
    $this->ogContext
      ->getGroup()
      ->willReturn($group);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgCacheContextTestBase::getCacheContext abstract protected function Returns the instantiated cache context service which is being tested. 3
OgCacheContextTestBase::getContextResult protected function Return the context result.
OgContextCacheContextTestBase::$group protected property A mocked group entity.
OgContextCacheContextTestBase::$ogContext protected property The mocked OG context service.
OgContextCacheContextTestBase::contextProvider abstract public function Provides test data for the test with active context objects. 2
OgContextCacheContextTestBase::expectGroupContext protected function Sets an expectation that OgContext will return the given group.
OgContextCacheContextTestBase::setUp protected function Overrides UnitTestCase::setUp 1
OgContextCacheContextTestBase::setupExpectedContext abstract protected function Set up expectations for tests that have an active context object. 2
OgContextCacheContextTestBase::testWithContext public function Tests the result of the cache context service with active context objects.
OgContextCacheContextTestBase::testWithoutContext abstract public function Tests the result of the cache context service without active context. 2
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.