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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\og\Unit\Cache\Context\OgCacheContextTestBase
- class \Drupal\Tests\og\Unit\Cache\Context\OgContextCacheContextTestBase
- class \Drupal\Tests\og\Unit\Cache\Context\OgCacheContextTestBase
Expanded class hierarchy of OgContextCacheContextTestBase
See also
File
- tests/
src/ Unit/ Cache/ Context/ OgContextCacheContextTestBase.php, line 18
Namespace
Drupal\Tests\og\Unit\Cache\ContextView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
OgCacheContextTestBase:: |
abstract protected | function | Returns the instantiated cache context service which is being tested. | 3 |
OgCacheContextTestBase:: |
protected | function | Return the context result. | |
OgContextCacheContextTestBase:: |
protected | property | A mocked group entity. | |
OgContextCacheContextTestBase:: |
protected | property | The mocked OG context service. | |
OgContextCacheContextTestBase:: |
abstract public | function | Provides test data for the test with active context objects. | 2 |
OgContextCacheContextTestBase:: |
protected | function | Sets an expectation that OgContext will return the given group. | |
OgContextCacheContextTestBase:: |
protected | function |
Overrides UnitTestCase:: |
1 |
OgContextCacheContextTestBase:: |
abstract protected | function | Set up expectations for tests that have an active context object. | 2 |
OgContextCacheContextTestBase:: |
public | function | Tests the result of the cache context service with active context objects. | |
OgContextCacheContextTestBase:: |
abstract public | function | Tests the result of the cache context service without active context. | 2 |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |