class ComponentKernelTestBase

Defines a base class for component kernel tests.

Hierarchy

Expanded class hierarchy of ComponentKernelTestBase

File

core/tests/Drupal/Tests/Core/Theme/Component/ComponentKernelTestBase.php, line 17

Namespace

Drupal\Tests\Core\Theme\Component
View source
abstract class ComponentKernelTestBase extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'system',
    'user',
    'serialization',
  ];
  
  /**
   * Themes to install.
   *
   * @var string[]
   */
  protected static $themes = [];
  
  /**
   * The component negotiator.
   *
   * @return \Drupal\Core\Theme\ComponentNegotiator
   */
  protected ComponentNegotiator $negotiator;
  
  /**
   * The component plugin manager.
   *
   * @var \Drupal\Core\Theme\ComponentPluginManager
   */
  protected ComponentPluginManager $manager;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    if (empty(static::$themes)) {
      throw new \Exception('You need to set the protected static $themes property on your test class, with the first item being the default theme.');
    }
    $this->container
      ->get('theme_installer')
      ->install(static::$themes);
    $this->installConfig('system');
    $system_theme_config = $this->container
      ->get('config.factory')
      ->getEditable('system.theme');
    $theme_name = reset(static::$themes);
    $system_theme_config->set('default', $theme_name)
      ->save();
    $theme_manager = \Drupal::service('theme.manager');
    $active_theme = \Drupal::service('theme.initialization')->initTheme($theme_name);
    $theme_manager->setActiveTheme($active_theme);
    $this->negotiator = new ComponentNegotiator($theme_manager, \Drupal::service('extension.list.module'));
    $this->manager = \Drupal::service('plugin.manager.sdc');
  }
  
  /**
   * Renders a component for testing sake.
   *
   * @param array $component
   *   Component render array.
   * @param \Drupal\Core\Render\BubbleableMetadata|null $metadata
   *   Bubble metadata.
   *
   * @return \Symfony\Component\DomCrawler\Crawler
   *   Crawler for introspecting the rendered component.
   */
  protected function renderComponentRenderArray(array $component, ?BubbleableMetadata $metadata = NULL) : Crawler {
    $component = [
      '#type' => 'container',
      '#attributes' => [
        'id' => 'sdc-wrapper',
      ],
      'component' => $component,
    ];
    $metadata = $metadata ?: new BubbleableMetadata();
    $context = new RenderContext();
    $renderer = \Drupal::service('renderer');
    $output = $renderer->executeInRenderContext($context, fn() => $renderer->render($component));
    if (!$context->isEmpty()) {
      $metadata->addCacheableDependency($context->pop());
    }
    return new Crawler((string) $output);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ComponentKernelTestBase::$manager protected property The component plugin manager.
ComponentKernelTestBase::$modules protected static property 6
ComponentKernelTestBase::$negotiator protected property The component negotiator.
ComponentKernelTestBase::$themes protected static property Themes to install. 6
ComponentKernelTestBase::renderComponentRenderArray protected function Renders a component for testing sake.
ComponentKernelTestBase::setUp protected function
ExtensionListTestTrait::getModulePath protected function Gets the path for the specified module.
ExtensionListTestTrait::getThemePath protected function Gets the path for the specified theme.
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.
StorageCopyTrait::replaceStorageContents protected static function Copy the configuration from one storage to another and remove stale items.

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