function BlockManagerTest::setUp

Same name in other branches
  1. 8.9.x core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php \Drupal\Tests\Core\Block\BlockManagerTest::setUp()
  2. 10 core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php \Drupal\Tests\Core\Block\BlockManagerTest::setUp()
  3. 11.x core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php \Drupal\Tests\Core\Block\BlockManagerTest::setUp()

Overrides UnitTestCase::setUp

File

core/tests/Drupal/Tests/Core/Block/BlockManagerTest.php, line 42

Class

BlockManagerTest
@coversDefaultClass \Drupal\Core\Block\BlockManager

Namespace

Drupal\Tests\Core\Block

Code

protected function setUp() : void {
    parent::setUp();
    $container = new ContainerBuilder();
    $current_user = $this->prophesize(AccountInterface::class);
    $container->set('current_user', $current_user->reveal());
    $container->set('string_translation', $this->getStringTranslationStub());
    \Drupal::setContainer($container);
    $cache_backend = $this->prophesize(CacheBackendInterface::class);
    $module_handler = $this->prophesize(ModuleHandlerInterface::class);
    $this->logger = $this->prophesize(LoggerInterface::class);
    $this->blockManager = new BlockManager(new \ArrayObject(), $cache_backend->reveal(), $module_handler->reveal(), $this->logger
        ->reveal());
    $this->blockManager
        ->setStringTranslation($this->getStringTranslationStub());
    $discovery = $this->prophesize(DiscoveryInterface::class);
    // Specify the 'broken' block, as well as 3 other blocks with admin labels
    // that are purposefully not in alphabetical order.
    $discovery->getDefinitions()
        ->willReturn([
        'broken' => [
            'admin_label' => $this->t('Broken/Missing'),
            'category' => $this->t('Block'),
            'class' => Broken::class,
            'provider' => 'core',
        ],
        'block1' => [
            'admin_label' => $this->t('Coconut'),
            'category' => $this->t('Group 2'),
        ],
        'block2' => [
            'admin_label' => $this->t('Apple'),
            'category' => $this->t('Group 1'),
        ],
        'block3' => [
            'admin_label' => $this->t('Banana'),
            'category' => $this->t('Group 2'),
        ],
    ]);
    // Force the discovery object onto the block manager.
    $property = new \ReflectionProperty(BlockManager::class, 'discovery');
    $property->setAccessible(TRUE);
    $property->setValue($this->blockManager, $discovery->reveal());
}

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