class EntityTypeRepositoryTest
Same name and namespace in other branches
- 10 core/tests/Drupal/Tests/Core/Entity/EntityTypeRepositoryTest.php \Drupal\Tests\Core\Entity\EntityTypeRepositoryTest
- 9 core/tests/Drupal/Tests/Core/Entity/EntityTypeRepositoryTest.php \Drupal\Tests\Core\Entity\EntityTypeRepositoryTest
- 8.9.x core/tests/Drupal/Tests/Core/Entity/EntityTypeRepositoryTest.php \Drupal\Tests\Core\Entity\EntityTypeRepositoryTest
- main core/tests/Drupal/Tests/Core/Entity/EntityTypeRepositoryTest.php \Drupal\Tests\Core\Entity\EntityTypeRepositoryTest
Tests Drupal\Core\Entity\EntityTypeRepository.
Attributes
#[CoversClass(EntityTypeRepository::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\EntityTypeRepositoryTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of EntityTypeRepositoryTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityTypeRepositoryTest.php, line 24
Namespace
Drupal\Tests\Core\EntityView source
class EntityTypeRepositoryTest extends UnitTestCase {
/**
* The entity type repository under test.
*
* @var \Drupal\Core\Entity\EntityTypeRepository
*/
protected $entityTypeRepository;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $entityTypeManager;
/**
* The entity type bundle info service.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $entityTypeBundleInfo;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
$this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class);
$this->entityTypeRepository = new EntityTypeRepository($this->entityTypeManager
->reveal(), $this->entityTypeBundleInfo
->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([]);
// Give the entity type a legitimate class to return.
$entity_type->getClass()
->willReturn(EntityInterface::class);
$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);
$this->entityTypeBundleInfo
->getAllBundleInfo()
->willReturn([]);
}
/**
* Tests the getEntityTypeLabels() method.
*/
public function testGetEntityTypeLabels() : void {
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getLabel()
->willReturn('Apple');
$apple->getBundleOf()
->willReturn(NULL);
$banana = $this->prophesize(EntityTypeInterface::class);
$banana->getLabel()
->willReturn('Banana');
$banana->getBundleOf()
->willReturn(NULL);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$expected = [
'apple' => 'Apple',
'banana' => 'Banana',
];
$this->assertSame($expected, $this->entityTypeRepository
->getEntityTypeLabels());
}
/**
* Tests get entity type from class.
*/
public function testGetEntityTypeFromClass() : void {
$apple = $this->prophesize(EntityTypeInterface::class);
$banana = $this->prophesize(EntityTypeInterface::class);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$apple->getOriginalClass()
->willReturn('\\Drupal\\apple\\Entity\\Apple');
$apple->getDecoratedClasses()
->willReturn([
'\\Drupal\\apple\\Entity\\Apple',
]);
$banana->getOriginalClass()
->willReturn('\\Drupal\\banana\\Entity\\Banana');
$banana->getDecoratedClasses()
->willReturn([
'\\Drupal\\banana\\Entity\\Banana',
]);
$banana->getClass()
->willReturn('\\Drupal\\mango\\Entity\\Mango');
$banana->id()
->willReturn('banana')
->shouldBeCalledTimes(2);
$entity_type_id = $this->entityTypeRepository
->getEntityTypeFromClass('\\Drupal\\banana\\Entity\\Banana');
$this->assertSame('banana', $entity_type_id);
$entity_type_id = $this->entityTypeRepository
->getEntityTypeFromClass('\\Drupal\\mango\\Entity\\Mango');
$this->assertSame('banana', $entity_type_id);
}
/**
* Tests get entity type from class no match.
*/
public function testGetEntityTypeFromClassNoMatch() : void {
$apple = $this->prophesize(EntityTypeInterface::class);
$banana = $this->prophesize(EntityTypeInterface::class);
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$apple->getOriginalClass()
->willReturn('\\Drupal\\apple\\Entity\\Apple');
$apple->getDecoratedClasses()
->willReturn([
'\\Drupal\\apple\\Entity\\Apple',
]);
$banana->getOriginalClass()
->willReturn('\\Drupal\\banana\\Entity\\Banana');
$banana->getDecoratedClasses()
->willReturn([
'\\Drupal\\banana\\Entity\\Banana',
]);
$this->expectException(NoCorrespondingEntityClassException::class);
$this->expectExceptionMessage('The \\Drupal\\pear\\Entity\\Pear class does not correspond to an entity type.');
$this->entityTypeRepository
->getEntityTypeFromClass('\\Drupal\\pear\\Entity\\Pear');
}
/**
* Tests get entity type from class ambiguous.
*/
public function testGetEntityTypeFromClassAmbiguous() : void {
$jazz = $this->prophesize(EntityTypeInterface::class);
$jazz->getOriginalClass()
->willReturn('\\Drupal\\apple\\Entity\\Apple');
$jazz->getDecoratedClasses()
->willReturn([
'\\Drupal\\apple\\Entity\\Apple',
]);
$jazz->id()
->willReturn('jazz');
$gala = $this->prophesize(EntityTypeInterface::class);
$gala->getOriginalClass()
->willReturn('\\Drupal\\apple\\Entity\\Apple');
$gala->getDecoratedClasses()
->willReturn([
'\\Drupal\\apple\\Entity\\Apple',
]);
$gala->id()
->willReturn('gala');
$this->setUpEntityTypeDefinitions([
'jazz' => $jazz,
'gala' => $gala,
]);
$this->expectException(AmbiguousEntityClassException::class);
$this->expectExceptionMessage('Multiple entity types found for \\Drupal\\apple\\Entity\\Apple.');
$this->entityTypeRepository
->getEntityTypeFromClass('\\Drupal\\apple\\Entity\\Apple');
}
/**
* Tests get entity type from class ambiguous bundle class.
*/
public function testGetEntityTypeFromClassAmbiguousBundleClass() : void {
$blackcurrant = $this->prophesize(EntityTypeInterface::class);
$blackcurrant->getOriginalClass()
->willReturn(Apple::class);
$blackcurrant->getDecoratedClasses()
->willReturn([
Apple::class,
]);
$blackcurrant->getClass()
->willReturn(Blackcurrant::class);
$blackcurrant->id()
->willReturn('blackcurrant');
$gala = $this->prophesize(EntityTypeInterface::class);
$gala->getOriginalClass()
->willReturn(Apple::class);
$gala->getDecoratedClasses()
->willReturn([
Apple::class,
]);
$gala->getClass()
->willReturn(RoyalGala::class);
$gala->id()
->willReturn('gala');
$this->setUpEntityTypeDefinitions([
'blackcurrant' => $blackcurrant,
'gala' => $gala,
]);
$this->entityTypeBundleInfo
->getAllBundleInfo()
->willReturn([
'gala' => [
'royal_gala' => [
'label' => 'Royal Gala',
'class' => RoyalGala::class,
],
],
]);
$this->assertSame('gala', $this->entityTypeRepository
->getEntityTypeFromClass(RoyalGala::class));
}
/**
* Tests the ::getEntityTypeFromClass() method.
*/
public function testGetEntityTypeFromClassDecoratedClass() : void {
$apple = $this->prophesize(EntityTypeInterface::class);
$apple->getOriginalClass()
->willReturn(Apple::class);
$apple->getDecoratedClasses()
->willReturn([
Apple::class,
RoyalGala::class,
]);
$apple->getClass()
->willReturn(Apple::class);
$apple->id()
->willReturn('apple');
$this->setUpEntityTypeDefinitions([
'apple' => $apple,
]);
$this->assertSame('apple', $this->entityTypeRepository
->getEntityTypeFromClass(RoyalGala::class));
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title |
|---|---|---|---|---|---|
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | ||
| EntityTypeRepositoryTest::$entityTypeBundleInfo | protected | property | The entity type bundle info service. | ||
| EntityTypeRepositoryTest::$entityTypeManager | protected | property | The entity type manager. | ||
| EntityTypeRepositoryTest::$entityTypeRepository | protected | property | The entity type repository under test. | ||
| EntityTypeRepositoryTest::setUp | protected | function | Overrides UnitTestCase::setUp | ||
| EntityTypeRepositoryTest::setUpEntityTypeDefinitions | protected | function | Sets up the entity type manager to be tested. | ||
| EntityTypeRepositoryTest::testGetEntityTypeFromClass | public | function | Tests get entity type from class. | ||
| EntityTypeRepositoryTest::testGetEntityTypeFromClassAmbiguous | public | function | Tests get entity type from class ambiguous. | ||
| EntityTypeRepositoryTest::testGetEntityTypeFromClassAmbiguousBundleClass | public | function | Tests get entity type from class ambiguous bundle class. | ||
| EntityTypeRepositoryTest::testGetEntityTypeFromClassDecoratedClass | public | function | Tests the ::getEntityTypeFromClass() method. | ||
| EntityTypeRepositoryTest::testGetEntityTypeFromClassNoMatch | public | function | Tests get entity type from class no match. | ||
| EntityTypeRepositoryTest::testGetEntityTypeLabels | public | function | Tests the getEntityTypeLabels() method. | ||
| 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.