class EntityTypeBundleInfoTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php \Drupal\Tests\Core\Entity\EntityTypeBundleInfoTest
- 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php \Drupal\Tests\Core\Entity\EntityTypeBundleInfoTest
- 10 core/tests/Drupal/Tests/Core/Entity/EntityTypeBundleInfoTest.php \Drupal\Tests\Core\Entity\EntityTypeBundleInfoTest
@coversDefaultClass \Drupal\Core\Entity\EntityTypeBundleInfo @group Entity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Entity\EntityTypeBundleInfoTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of EntityTypeBundleInfoTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityTypeBundleInfoTest.php, line 26
Namespace
Drupal\Tests\Core\EntityView source
class EntityTypeBundleInfoTest extends UnitTestCase {
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $moduleHandler;
/**
* The cache backend to use.
*
* @var \Drupal\Core\Cache\CacheBackendInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $cacheBackend;
/**
* The cache tags invalidator.
*
* @var \Drupal\Core\Cache\CacheTagsInvalidatorInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $cacheTagsInvalidator;
/**
* The typed data manager.
*
* @var \Drupal\Core\TypedData\TypedDataManagerInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $typedDataManager;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $entityTypeManager;
/**
* The language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* The entity type bundle info under test.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfo
*/
protected $entityTypeBundleInfo;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class);
$this->moduleHandler
->alter('entity_type', Argument::type('array'))
->willReturn(NULL);
$this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
$this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
$this->cacheTagsInvalidator = $this->prophesize(CacheTagsInvalidatorInterface::class);
$language = new Language([
'id' => 'en',
]);
$this->languageManager = $this->prophesize(LanguageManagerInterface::class);
$this->languageManager
->getCurrentLanguage()
->willReturn($language);
$this->languageManager
->getLanguages()
->willReturn([
'en' => (object) [
'id' => 'en',
],
]);
$this->typedDataManager = $this->prophesize(TypedDataManagerInterface::class);
$this->cacheBackend = $this->prophesize(CacheBackendInterface::class);
$container = $this->prophesize(ContainerInterface::class);
$container->get('cache_tags.invalidator')
->willReturn($this->cacheTagsInvalidator
->reveal());
// $container->get('typed_data_manager')->willReturn($this->typedDataManager->reveal());
\Drupal::setContainer($container->reveal());
$this->entityTypeBundleInfo = new EntityTypeBundleInfo($this->entityTypeManager
->reveal(), $this->languageManager
->reveal(), $this->moduleHandler
->reveal(), $this->typedDataManager
->reveal(), $this->cacheBackend
->reveal());
}
/**
* Sets up the entity type manager to be tested.
*
* @param \Drupal\Core\Entity\EntityTypeInterface[]|\Prophecy\Prophecy\ProphecyInterface[] $definitions
* (optional) An array of entity type definitions.
*/
protected function setUpEntityTypeDefinitions($definitions = []) : void {
foreach ($definitions as $key => $entity_type) {
// \Drupal\Core\Entity\EntityTypeInterface::getLinkTemplates() is called
// by \Drupal\Core\Entity\EntityTypeManager::processDefinition() so it
// must always be mocked.
$entity_type->getLinkTemplates()
->willReturn([]);
$definitions[$key] = $entity_type->reveal();
}
$this->entityTypeManager
->getDefinition(Argument::cetera())
->will(function ($args) use ($definitions) {
$entity_type_id = $args[0];
$exception_on_invalid = $args[1];
if (isset($definitions[$entity_type_id])) {
return $definitions[$entity_type_id];
}
elseif (!$exception_on_invalid) {
return NULL;
}
else {
throw new PluginNotFoundException($entity_type_id);
}
});
$this->entityTypeManager
->getDefinitions()
->willReturn($definitions);
}
/**
* Tests the clearCachedBundles() method.
*
* @covers ::clearCachedBundles
*/
public function testClearCachedBundles() : void {
$this->setUpEntityTypeDefinitions();
$this->typedDataManager
->clearCachedDefinitions()
->shouldBeCalled();
$this->cacheTagsInvalidator
->invalidateTags([
'entity_bundles',
])
->shouldBeCalled();
$this->entityTypeBundleInfo
->clearCachedBundles();
}
/**
* Tests the getBundleInfo() method.
*
* @covers ::getBundleInfo
*
* @dataProvider providerTestGetBundleInfo
*/
public function testGetBundleInfo($entity_type_id, $expected) : void {
$this->moduleHandler
->invokeAll('entity_bundle_info')
->willReturn([]);
$this->moduleHandler
->alter('entity_bundle_info', Argument::type('array'))
->willReturn(NULL);
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getLabel()
->willReturn('Apple');
$apple->getBundleEntityType()
->willReturn(NULL);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->getLabel()
->willReturn('Banana');
$banana->getBundleEntityType()
->willReturn(NULL);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$bundle_info = $this->entityTypeBundleInfo
->getBundleInfo($entity_type_id);
$this->assertSame($expected, $bundle_info);
}
/**
* Provides test data for testGetBundleInfo().
*
* @return array
* Test data.
*/
public static function providerTestGetBundleInfo() {
return [
[
'apple',
[
'apple' => [
'label' => 'Apple',
],
],
],
[
'banana',
[
'banana' => [
'label' => 'Banana',
],
],
],
[
'pear',
[],
],
];
}
/**
* Tests the getAllBundleInfo() method.
*
* @covers ::getAllBundleInfo
*/
public function testGetAllBundleInfo() : void {
$this->moduleHandler
->invokeAll('entity_bundle_info')
->willReturn([]);
$this->moduleHandler
->alter('entity_bundle_info', Argument::type('array'))
->willReturn(NULL);
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getLabel()
->willReturn('Apple');
$apple->getBundleEntityType()
->willReturn(NULL);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->getLabel()
->willReturn('Banana');
$banana->getBundleEntityType()
->willReturn(NULL);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$cacheBackend = $this->cacheBackend;
$this->cacheBackend
->get('entity_bundle_info:en')
->willReturn(FALSE);
$this->cacheBackend
->set('entity_bundle_info:en', Argument::any(), Cache::PERMANENT, [
'entity_types',
'entity_bundles',
])
->will(function () use ($cacheBackend) {
$cacheBackend->get('entity_bundle_info:en')
->willReturn((object) [
'data' => 'cached data',
])
->shouldBeCalled();
})
->shouldBeCalled();
$this->cacheTagsInvalidator
->invalidateTags([
'entity_bundles',
])
->shouldBeCalled();
$this->typedDataManager
->clearCachedDefinitions()
->shouldBeCalled();
$expected = [
'apple' => [
'apple' => [
'label' => 'Apple',
],
],
'banana' => [
'banana' => [
'label' => 'Banana',
],
],
];
$bundle_info = $this->entityTypeBundleInfo
->getAllBundleInfo();
$this->assertSame($expected, $bundle_info);
$bundle_info = $this->entityTypeBundleInfo
->getAllBundleInfo();
$this->assertSame($expected, $bundle_info);
$this->entityTypeBundleInfo
->clearCachedBundles();
$bundle_info = $this->entityTypeBundleInfo
->getAllBundleInfo();
$this->assertSame('cached data', $bundle_info);
}
/**
* @covers ::getAllBundleInfo
*/
public function testGetAllBundleInfoWithEntityBundleInfo() : void {
// Ensure that EntityTypeBundleInfo::getAllBundleInfo() does not add
// additional bundles if hook_entity_bundle_info() defines some and the
// entity_type does not define a bundle entity type.
$this->moduleHandler
->invokeAll('entity_bundle_info')
->willReturn([
'banana' => [
'fig' => [
'label' => 'Fig banana',
],
],
]);
$this->moduleHandler
->alter('entity_bundle_info', Argument::type('array'))
->willReturn(NULL);
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getLabel()
->willReturn('Apple');
$apple->getBundleEntityType()
->willReturn(NULL);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->getLabel()
->willReturn('Banana');
$banana->getBundleEntityType()
->willReturn(NULL);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$expected = [
'banana' => [
'fig' => [
'label' => 'Fig banana',
],
],
'apple' => [
'apple' => [
'label' => 'Apple',
],
],
];
$bundle_info = $this->entityTypeBundleInfo
->getAllBundleInfo();
$this->assertSame($expected, $bundle_info);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
EntityTypeBundleInfoTest::$cacheBackend | protected | property | The cache backend to use. | |
EntityTypeBundleInfoTest::$cacheTagsInvalidator | protected | property | The cache tags invalidator. | |
EntityTypeBundleInfoTest::$entityTypeBundleInfo | protected | property | The entity type bundle info under test. | |
EntityTypeBundleInfoTest::$entityTypeManager | protected | property | The entity type manager. | |
EntityTypeBundleInfoTest::$languageManager | protected | property | The language manager. | |
EntityTypeBundleInfoTest::$moduleHandler | protected | property | The module handler. | |
EntityTypeBundleInfoTest::$typedDataManager | protected | property | The typed data manager. | |
EntityTypeBundleInfoTest::providerTestGetBundleInfo | public static | function | Provides test data for testGetBundleInfo(). | |
EntityTypeBundleInfoTest::setUp | protected | function | Overrides UnitTestCase::setUp | |
EntityTypeBundleInfoTest::setUpEntityTypeDefinitions | protected | function | Sets up the entity type manager to be tested. | |
EntityTypeBundleInfoTest::testClearCachedBundles | public | function | Tests the clearCachedBundles() method. | |
EntityTypeBundleInfoTest::testGetAllBundleInfo | public | function | Tests the getAllBundleInfo() method. | |
EntityTypeBundleInfoTest::testGetAllBundleInfoWithEntityBundleInfo | public | function | @covers ::getAllBundleInfo | |
EntityTypeBundleInfoTest::testGetBundleInfo | public | function | Tests the getBundleInfo() method. | |
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
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::getConfigStorageStub | public | function | Returns a stub config storage that returns the supplied configuration. | |
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::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.