3 namespace Drupal\Tests\Core\Plugin\Context;
5 use Drupal\Core\Plugin\Context\Context;
6 use Drupal\Core\Plugin\Context\ContextDefinition;
7 use Drupal\Core\Plugin\Context\LazyContextRepository;
8 use Drupal\Tests\UnitTestCase;
9 use Symfony\Component\DependencyInjection\ContainerBuilder;
12 * @coversDefaultClass \Drupal\Core\Plugin\Context\LazyContextRepository
15 class LazyContextRepositoryTest extends UnitTestCase {
20 * @var \Symfony\Component\DependencyInjection\ContainerBuilder
27 protected function setUp() {
30 $this->container = new ContainerBuilder();
34 * @covers ::getRuntimeContexts
36 public function testGetRuntimeContextsSingle() {
37 $contexts = $this->setupContextAndProvider('test_provider', ['test_context']);
39 $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
40 $run_time_contexts = $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context']);
41 $this->assertEquals(['@test_provider:test_context' => $contexts[0]], $run_time_contexts);
45 * @covers ::getRuntimeContexts
47 public function testGetRuntimeMultipleContextsPerService() {
48 $contexts = $this->setupContextAndProvider('test_provider', ['test_context0', 'test_context1']);
50 $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
51 $run_time_contexts = $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider:test_context1']);
52 $this->assertEquals(['@test_provider:test_context0' => $contexts[0], '@test_provider:test_context1' => $contexts[1]], $run_time_contexts);
56 * @covers ::getRuntimeContexts
58 public function testGetRuntimeMultipleContextProviders() {
59 $contexts0 = $this->setupContextAndProvider('test_provider', ['test_context0', 'test_context1'], ['test_context0']);
60 $contexts1 = $this->setupContextAndProvider('test_provider2', ['test1_context0', 'test1_context1'], ['test1_context0']);
62 $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
63 $run_time_contexts = $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider2:test1_context0']);
64 $this->assertEquals(['@test_provider:test_context0' => $contexts0[0], '@test_provider2:test1_context0' => $contexts1[1]], $run_time_contexts);
68 * @covers ::getRuntimeContexts
70 public function testInvalidContextId() {
71 $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
72 $this->setExpectedException(\InvalidArgumentException::class, 'You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
73 $lazy_context_repository->getRuntimeContexts(['test_context', '@test_provider:test_context1']);
77 * @covers ::getRuntimeContexts
79 public function testGetRuntimeStaticCache() {
80 $context0 = new Context(new ContextDefinition('example'));
81 $context1 = new Context(new ContextDefinition('example'));
83 $context_provider = $this->prophesize('\Drupal\Core\Plugin\Context\ContextProviderInterface');
84 $context_provider->getRuntimeContexts(['test_context0', 'test_context1'])
85 ->shouldBeCalledTimes(1)
86 ->willReturn(['test_context0' => $context0, 'test_context1' => $context1]);
87 $context_provider = $context_provider->reveal();
88 $this->container->set('test_provider', $context_provider);
90 $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
91 $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider:test_context1']);
92 $lazy_context_repository->getRuntimeContexts(['@test_provider:test_context0', '@test_provider:test_context1']);
96 * @covers ::getAvailableContexts
98 public function testGetAvailableContexts() {
99 $contexts0 = $this->setupContextAndProvider('test_provider0', ['test0_context0', 'test0_context1']);
100 $contexts1 = $this->setupContextAndProvider('test_provider1', ['test1_context0', 'test1_context1']);
102 $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider0', 'test_provider1']);
103 $contexts = $lazy_context_repository->getAvailableContexts();
105 $this->assertEquals([
106 '@test_provider0:test0_context0' => $contexts0[0],
107 '@test_provider0:test0_context1' => $contexts0[1],
108 '@test_provider1:test1_context0' => $contexts1[0],
109 '@test_provider1:test1_context1' => $contexts1[1],
115 * Sets up contexts and context providers.
117 * @param string $service_id
118 * The service ID of the service provider.
119 * @param string[] $unqualified_context_ids
120 * An array of context slot names.
121 * @param string[] $expected_unqualified_context_ids
122 * The expected unqualified context IDs passed to getRuntimeContexts.
125 * An array of set up contexts.
127 protected function setupContextAndProvider($service_id, array $unqualified_context_ids, array $expected_unqualified_context_ids = []) {
129 for ($i = 0; $i < count($unqualified_context_ids); $i++) {
130 $contexts[] = new Context(new ContextDefinition('example'));
133 $expected_unqualified_context_ids = $expected_unqualified_context_ids ?: $unqualified_context_ids;
135 $context_provider = $this->prophesize('\Drupal\Core\Plugin\Context\ContextProviderInterface');
136 $context_provider->getRuntimeContexts($expected_unqualified_context_ids)
137 ->willReturn(array_combine($unqualified_context_ids, $contexts));
138 $context_provider->getAvailableContexts()
139 ->willReturn(array_combine($unqualified_context_ids, $contexts));
140 $context_provider = $context_provider->reveal();
141 $this->container->set($service_id, $context_provider);