class AddComponentDeriverTest

@coversDefaultClass \Drupal\layout_builder\Plugin\ConfigAction\Deriver\AddComponentDeriver
@group layout_builder

Hierarchy

Expanded class hierarchy of AddComponentDeriverTest

File

core/modules/layout_builder/tests/src/Unit/Plugin/ConfigAction/Deriver/AddComponentDeriverTest.php, line 19

Namespace

Drupal\Tests\layout_builder\Unit\Plugin\ConfigAction\Deriver
View source
class AddComponentDeriverTest extends UnitTestCase {
  use ProphecyTrait;
  
  /**
   * Tests derivative generation for entities implementing SectionListInterface.
   *
   * @covers ::getDerivativeDefinitions
   */
  public function testGetDerivativeDefinitions() : void {
    $entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
    $deriver = new AddComponentDeriver($entityTypeManager->reveal());
    $entity_types = [];
    // Create a mock entity type that implements both required interfaces.
    $valid_entity_type = $this->prophesize(EntityTypeInterface::class);
    $valid_entity_type->entityClassImplements(ConfigEntityInterface::class)
      ->willReturn(TRUE);
    $valid_entity_type->entityClassImplements(SectionListInterface::class)
      ->willReturn(TRUE);
    $valid_entity_type->id()
      ->willReturn('valid_type');
    $entity_types['valid_type'] = $valid_entity_type->reveal();
    // Create a mock entity type that only implements ConfigEntityInterface.
    $config_only_type = $this->prophesize(EntityTypeInterface::class);
    $config_only_type->entityClassImplements(ConfigEntityInterface::class)
      ->willReturn(TRUE);
    $config_only_type->entityClassImplements(SectionListInterface::class)
      ->willReturn(FALSE);
    $entity_types['config_only'] = $config_only_type->reveal();
    // Create a mock entity type that only implements SectionListInterface.
    $section_only_type = $this->prophesize(EntityTypeInterface::class);
    $section_only_type->entityClassImplements(ConfigEntityInterface::class)
      ->willReturn(FALSE);
    $section_only_type->entityClassImplements(SectionListInterface::class)
      ->willReturn(TRUE);
    $entity_types['section_only'] = $section_only_type->reveal();
    $entityTypeManager->getDefinitions()
      ->willReturn($entity_types);
    $base_plugin_definition = [];
    $derivatives = $deriver->getDerivativeDefinitions($base_plugin_definition);
    $this->assertCount(1, $derivatives, 'Only one derivative should be generated.');
    $this->assertArrayHasKey('addComponentToLayout', $derivatives, 'The derivative should be keyed by addComponentToLayout.');
    $this->assertEquals([
      'valid_type',
    ], $derivatives['addComponentToLayout']['entity_types'], 'Only the valid entity type should be included in the derivative definition.');
  }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
AddComponentDeriverTest::testGetDerivativeDefinitions public function Tests derivative generation for entities implementing SectionListInterface.
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
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.
UnitTestCase::$root protected property The app root.
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::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::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.
UnitTestCase::setUp protected function 360
UnitTestCase::setupMockIterator protected function Set up a traversable class mock to return specific items when iterated.

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