class EntityViewBuilderTest
Same name in this branch
- main core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest
- 11.x core/tests/Drupal/Tests/Core/Entity/EntityViewBuilderTest.php \Drupal\Tests\Core\Entity\EntityViewBuilderTest
- 10 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest
- 9 core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityViewBuilderTest.php \Drupal\KernelTests\Core\Entity\EntityViewBuilderTest
Tests Drupal\Core\Entity\EntityViewBuilder.
Attributes
#[CoversClass(EntityViewBuilder::class)]
#[Group('Entity')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Entity\EntityViewBuilderTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of EntityViewBuilderTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityViewBuilderTest.php, line 19
Namespace
Drupal\Tests\Core\EntityView 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 | Deprecated | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | ||
| 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 | Deprecated | public | function | Adds an expected deprecation. | |
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | |||
| 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.