og-8.x-1.x-dev/tests/src/Unit/Cache/Context/OgContextCacheContextTestBase.php
tests/src/Unit/Cache/Context/OgContextCacheContextTestBase.php
<?php
declare(strict_types=1);
namespace Drupal\Tests\og\Unit\Cache\Context;
use Prophecy\Prophecy\ObjectProphecy;
use Drupal\Core\Entity\EntityInterface;
use Drupal\og\OgContextInterface;
use Prophecy\PhpUnit\ProphecyTrait;
/**
* 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().
*
* @see \Drupal\og\OgContextInterface
*/
abstract class OgContextCacheContextTestBase extends OgCacheContextTestBase {
use ProphecyTrait;
/**
* The mocked OG context service.
*/
protected OgContextInterface|ObjectProphecy $ogContext;
/**
* A mocked group entity.
*/
protected EntityInterface|ObjectProphecy $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 \Drupal\og\Cache\Context\OgMembershipStateCacheContext::getContext
* @covers \Drupal\og\Cache\Context\OgGroupContextCacheContext::getContext
* @dataProvider contextProvider
*/
public function testWithContext(mixed $context, string $expected_result): void {
$this->setupExpectedContext($context);
$result = $this->getContextResult();
$this->assertEquals($expected_result, $result);
}
/**
* Tests the result of the cache context service without active context.
*
* @doesNotPerformAssertions
*/
abstract public function testWithoutContext(): void;
/**
* 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()
*/
abstract public static function contextProvider(): array;
/**
* 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()
*/
abstract protected function setupExpectedContext(mixed $context): void;
/**
* 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): void {
$this->ogContext->getGroup()->willReturn($group);
}
}
