EntityTypeRepositoryTest.php in Zircon Profile 8
File
core/tests/Drupal/Tests/Core/Entity/EntityTypeRepositoryTest.php
View source
<?php
namespace Drupal\Tests\Core\Entity;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepository;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;
class EntityTypeRepositoryTest extends UnitTestCase {
protected $entityTypeRepository;
protected $entityTypeManager;
protected function setUp() {
parent::setUp();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityTypeRepository = new EntityTypeRepository($this->entityTypeManager
->reveal());
}
protected function setUpEntityTypeDefinitions($definitions = []) {
$class = $this
->getMockClass(EntityInterface::class);
foreach ($definitions as $key => $entity_type) {
$entity_type
->getLinkTemplates()
->willReturn([]);
$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);
}
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());
}
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);
}
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->entityTypeRepository
->getEntityTypeFromClass('\\Drupal\\pear\\Entity\\Pear');
}
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->entityTypeRepository
->getEntityTypeFromClass('\\Drupal\\apple\\Entity\\Apple');
}
}