class EntityTypeRepositoryTest in Drupal 10
Same name and namespace in other branches
- 8 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
@coversDefaultClass \Drupal\Core\Entity\EntityTypeRepository @group Entity
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, PhpUnitWarnings
- class \Drupal\Tests\Core\Entity\EntityTypeRepositoryTest
Expanded class hierarchy of EntityTypeRepositoryTest
File
- core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityTypeRepositoryTest.php, line 19
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;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityTypeRepository = new EntityTypeRepository($this->entityTypeManager
->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 = []) {
$class = $this
->getMockClass(EntityInterface::class);
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($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);
}
/**
* Tests the getEntityTypeLabels() method.
*
* @covers ::getEntityTypeLabels
*/
public function testGetEntityTypeLabels() {
$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());
}
/**
* @covers ::getEntityTypeFromClass
*/
public function testGetEntityTypeFromClass() {
$apple = $this
->prophesize(EntityTypeInterface::class);
$banana = $this
->prophesize(EntityTypeInterface::class);
$this
->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$apple
->getOriginalClass()
->willReturn('\\Drupal\\apple\\Entity\\Apple');
$banana
->getOriginalClass()
->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);
}
/**
* @covers ::getEntityTypeFromClass
*/
public function testGetEntityTypeFromClassNoMatch() {
$apple = $this
->prophesize(EntityTypeInterface::class);
$banana = $this
->prophesize(EntityTypeInterface::class);
$this
->setUpEntityTypeDefinitions([
'apple' => $apple,
'banana' => $banana,
]);
$apple
->getOriginalClass()
->willReturn('\\Drupal\\apple\\Entity\\Apple');
$banana
->getOriginalClass()
->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');
}
/**
* @covers ::getEntityTypeFromClass
*/
public function testGetEntityTypeFromClassAmbiguous() {
$boskoop = $this
->prophesize(EntityTypeInterface::class);
$boskoop
->getOriginalClass()
->willReturn('\\Drupal\\apple\\Entity\\Apple');
$boskoop
->id()
->willReturn('boskop');
$gala = $this
->prophesize(EntityTypeInterface::class);
$gala
->getOriginalClass()
->willReturn('\\Drupal\\apple\\Entity\\Apple');
$gala
->id()
->willReturn('gala');
$this
->setUpEntityTypeDefinitions([
'boskoop' => $boskoop,
'gala' => $gala,
]);
$this
->expectException(AmbiguousEntityClassException::class);
$this
->expectExceptionMessage('Multiple entity types found for \\Drupal\\apple\\Entity\\Apple.');
$this->entityTypeRepository
->getEntityTypeFromClass('\\Drupal\\apple\\Entity\\Apple');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityTypeRepositoryTest:: |
protected | property | The entity type manager. | |
EntityTypeRepositoryTest:: |
protected | property | The entity type repository under test. | |
EntityTypeRepositoryTest:: |
protected | function |
Overrides UnitTestCase:: |
|
EntityTypeRepositoryTest:: |
protected | function | Sets up the entity type manager to be tested. | |
EntityTypeRepositoryTest:: |
public | function | @covers ::getEntityTypeFromClass | |
EntityTypeRepositoryTest:: |
public | function | @covers ::getEntityTypeFromClass | |
EntityTypeRepositoryTest:: |
public | function | @covers ::getEntityTypeFromClass | |
EntityTypeRepositoryTest:: |
public | function | Tests the getEntityTypeLabels() method. | |
PhpUnitWarnings:: |
private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |
PhpUnitWarnings:: |
public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
public static | function |