class EntityViewBuilderTest

Same name in this branch
  1. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest
Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest

@coversDefaultClass \Drupal\Core\Entity\EntityViewBuilder
@group Entity

Hierarchy

Expanded class hierarchy of EntityViewBuilderTest

File

core/tests/Drupal/Tests/Core/Entity/EntityViewBuilderTest.php, line 18

Namespace

Drupal\Tests\Core\Entity
View source
class EntityViewBuilderTest extends UnitTestCase {
  const ENTITY_TYPE_ID = 'test_entity_type';
  
  /**
   * The entity view builder under test.
   *
   * @var \Drupal\Core\Entity\EntityViewBuilder
   */
  protected EntityViewBuilder $viewBuilder;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->viewBuilder = new class  extends EntityViewBuilder {
      public function __construct() {
        $this->entityTypeId = EntityViewBuilderTest::ENTITY_TYPE_ID;
      }

};
  }
  
  /**
   * Tests build components using a mocked Iterator.
   */
  public function testBuildComponents() : void {
    $field_name = $this->randomMachineName();
    $bundle = $this->randomMachineName();
    $entity_id = mt_rand(20, 30);
    $field_item_list = $this->createStub(FieldItemListInterface::class);
    $item = new \stdClass();
    $this->setupMockIterator($field_item_list, [
      $item,
    ]);
    $entity = $this->createConfiguredStub(FieldableEntityInterface::class, [
      'bundle' => $bundle,
      'hasField' => TRUE,
      'get' => $field_item_list,
    ]);
    $formatter_result = [
      $entity_id => [
        '#' . $this->randomMachineName() => $this->randomString(),
      ],
    ];
    $display = $this->createConfiguredStub(EntityViewDisplayInterface::class, [
      'getComponents' => [
        $field_name => [],
      ],
      'buildMultiple' => $formatter_result,
    ]);
    $entities = [
      $entity_id => $entity,
    ];
    $displays = [
      $bundle => $display,
    ];
    $build = [
      $entity_id => [],
    ];
    $view_mode = $this->randomMachineName();
    // Assert the hook is invoked.
    $module_handler = $this->createMock(ModuleHandlerInterface::class);
    $module_handler->expects($this->once())
      ->method('invokeAll')
      ->with('entity_prepare_view', [
      self::ENTITY_TYPE_ID,
      $entities,
      $displays,
      $view_mode,
    ]);
    $this->viewBuilder
      ->setModuleHandler($module_handler);
    $this->viewBuilder
      ->buildComponents($build, $entities, $displays, $view_mode);
    $this->assertSame([], $item->_attributes);
    $this->assertSame($formatter_result, $build);
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityViewBuilderTest::$viewBuilder protected property The entity view builder under test.
EntityViewBuilderTest::ENTITY_TYPE_ID constant
EntityViewBuilderTest::setUp protected function Overrides UnitTestCase::setUp
EntityViewBuilderTest::testBuildComponents public function Tests build components using a mocked Iterator.
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::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.