class LazyContextRepositoryTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php \Drupal\Tests\Core\Plugin\Context\LazyContextRepositoryTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php \Drupal\Tests\Core\Plugin\Context\LazyContextRepositoryTest
  3. 11.x core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php \Drupal\Tests\Core\Plugin\Context\LazyContextRepositoryTest

@coversDefaultClass \Drupal\Core\Plugin\Context\LazyContextRepository @group context

Hierarchy

Expanded class hierarchy of LazyContextRepositoryTest

File

core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php, line 17

Namespace

Drupal\Tests\Core\Plugin\Context
View source
class LazyContextRepositoryTest extends UnitTestCase {
    
    /**
     * The container.
     *
     * @var \Symfony\Component\DependencyInjection\ContainerBuilder
     */
    protected $container;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->container = new ContainerBuilder();
    }
    
    /**
     * @covers ::getRuntimeContexts
     */
    public function testGetRuntimeContextsSingle() : void {
        $contexts = $this->setupContextAndProvider('test_provider', [
            'test_context',
        ]);
        $lazy_context_repository = new LazyContextRepository($this->container, [
            'test_provider',
        ]);
        $run_time_contexts = $lazy_context_repository->getRuntimeContexts([
            '@test_provider:test_context',
        ]);
        $this->assertEquals([
            '@test_provider:test_context' => $contexts[0],
        ], $run_time_contexts);
    }
    
    /**
     * @covers ::getRuntimeContexts
     */
    public function testGetRuntimeMultipleContextsPerService() : void {
        $contexts = $this->setupContextAndProvider('test_provider', [
            'test_context0',
            'test_context1',
        ]);
        $lazy_context_repository = new LazyContextRepository($this->container, [
            'test_provider',
        ]);
        $run_time_contexts = $lazy_context_repository->getRuntimeContexts([
            '@test_provider:test_context0',
            '@test_provider:test_context1',
        ]);
        $this->assertEquals([
            '@test_provider:test_context0' => $contexts[0],
            '@test_provider:test_context1' => $contexts[1],
        ], $run_time_contexts);
    }
    
    /**
     * @covers ::getRuntimeContexts
     */
    public function testGetRuntimeMultipleContextProviders() : void {
        $contexts0 = $this->setupContextAndProvider('test_provider', [
            'test_context0',
            'test_context1',
        ], [
            'test_context0',
        ]);
        $contexts1 = $this->setupContextAndProvider('test_provider2', [
            'test1_context0',
            'test1_context1',
        ], [
            'test1_context0',
        ]);
        $lazy_context_repository = new LazyContextRepository($this->container, [
            'test_provider',
        ]);
        $run_time_contexts = $lazy_context_repository->getRuntimeContexts([
            '@test_provider:test_context0',
            '@test_provider2:test1_context0',
        ]);
        $this->assertEquals([
            '@test_provider:test_context0' => $contexts0[0],
            '@test_provider2:test1_context0' => $contexts1[1],
        ], $run_time_contexts);
    }
    
    /**
     * @covers ::getRuntimeContexts
     */
    public function testInvalidContextId() : void {
        $lazy_context_repository = new LazyContextRepository($this->container, [
            'test_provider',
        ]);
        $this->expectException(\AssertionError::class);
        $this->expectExceptionMessage('You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
        $lazy_context_repository->getRuntimeContexts([
            'test_context',
            '@test_provider:test_context1',
        ]);
    }
    
    /**
     * @covers ::getRuntimeContexts
     */
    public function testGetRuntimeStaticCache() : void {
        $context0 = new Context(new ContextDefinition('example'));
        $context1 = new Context(new ContextDefinition('example'));
        $context_provider = $this->prophesize('\\Drupal\\Core\\Plugin\\Context\\ContextProviderInterface');
        $context_provider->getRuntimeContexts([
            'test_context0',
            'test_context1',
        ])
            ->shouldBeCalledTimes(1)
            ->willReturn([
            'test_context0' => $context0,
            'test_context1' => $context1,
        ]);
        $context_provider = $context_provider->reveal();
        $this->container
            ->set('test_provider', $context_provider);
        $lazy_context_repository = new LazyContextRepository($this->container, [
            'test_provider',
        ]);
        $lazy_context_repository->getRuntimeContexts([
            '@test_provider:test_context0',
            '@test_provider:test_context1',
        ]);
        $lazy_context_repository->getRuntimeContexts([
            '@test_provider:test_context0',
            '@test_provider:test_context1',
        ]);
    }
    
    /**
     * @covers ::getAvailableContexts
     */
    public function testGetAvailableContexts() : void {
        $contexts0 = $this->setupContextAndProvider('test_provider0', [
            'test0_context0',
            'test0_context1',
        ]);
        $contexts1 = $this->setupContextAndProvider('test_provider1', [
            'test1_context0',
            'test1_context1',
        ]);
        $lazy_context_repository = new LazyContextRepository($this->container, [
            'test_provider0',
            'test_provider1',
        ]);
        $contexts = $lazy_context_repository->getAvailableContexts();
        $this->assertEquals([
            '@test_provider0:test0_context0' => $contexts0[0],
            '@test_provider0:test0_context1' => $contexts0[1],
            '@test_provider1:test1_context0' => $contexts1[0],
            '@test_provider1:test1_context1' => $contexts1[1],
        ], $contexts);
    }
    
    /**
     * Sets up contexts and context providers.
     *
     * @param string $service_id
     *   The service ID of the service provider.
     * @param string[] $unqualified_context_ids
     *   An array of context slot names.
     * @param string[] $expected_unqualified_context_ids
     *   The expected unqualified context IDs passed to getRuntimeContexts.
     *
     * @return array
     *   An array of set up contexts.
     */
    protected function setupContextAndProvider($service_id, array $unqualified_context_ids, array $expected_unqualified_context_ids = []) {
        $contexts = [];
        for ($i = 0; $i < count($unqualified_context_ids); $i++) {
            $contexts[] = new Context(new ContextDefinition('example'));
        }
        $expected_unqualified_context_ids = $expected_unqualified_context_ids ?: $unqualified_context_ids;
        $context_provider = $this->prophesize('\\Drupal\\Core\\Plugin\\Context\\ContextProviderInterface');
        $context_provider->getRuntimeContexts($expected_unqualified_context_ids)
            ->willReturn(array_combine($unqualified_context_ids, $contexts));
        $context_provider->getAvailableContexts()
            ->willReturn(array_combine($unqualified_context_ids, $contexts));
        $context_provider = $context_provider->reveal();
        $this->container
            ->set($service_id, $context_provider);
        return $contexts;
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
LazyContextRepositoryTest::$container protected property The container.
LazyContextRepositoryTest::setUp protected function Overrides UnitTestCase::setUp
LazyContextRepositoryTest::setupContextAndProvider protected function Sets up contexts and context providers.
LazyContextRepositoryTest::testGetAvailableContexts public function @covers ::getAvailableContexts
LazyContextRepositoryTest::testGetRuntimeContextsSingle public function @covers ::getRuntimeContexts
LazyContextRepositoryTest::testGetRuntimeMultipleContextProviders public function @covers ::getRuntimeContexts
LazyContextRepositoryTest::testGetRuntimeMultipleContextsPerService public function @covers ::getRuntimeContexts
LazyContextRepositoryTest::testGetRuntimeStaticCache public function @covers ::getRuntimeContexts
LazyContextRepositoryTest::testInvalidContextId public function @covers ::getRuntimeContexts
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
RandomGeneratorTrait::randomStringValidate Deprecated public function Callback for random string validation.
UnitTestCase::$root protected property The app root. 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::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUpBeforeClass public static function
UnitTestCase::__get public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.