EntityReferenceTestBase.php in Feeds 8.3
File
tests/src/Unit/Feeds/Target/EntityReferenceTestBase.php
View source
<?php
namespace Drupal\Tests\feeds\Unit\Feeds\Target;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\feeds\EntityFinderInterface;
use Drupal\feeds\Exception\EmptyFeedException;
use Drupal\feeds\FieldTargetDefinition;
abstract class EntityReferenceTestBase extends FieldTargetTestBase {
protected $entityTypeManager;
protected $entityStorage;
protected $entityFinder;
public function setUp() {
parent::setUp();
$referencable_entity_type_id = $this
->getReferencableEntityTypeId();
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
$this->entityStorage = $this
->prophesize($this
->getEntityStorageClass());
$this->entityTypeManager
->getStorage($referencable_entity_type_id)
->willReturn($this->entityStorage);
$this->entityTypeManager
->getDefinition($referencable_entity_type_id)
->willReturn($this
->createReferencableEntityType())
->shouldBeCalled();
$this->entityFinder = $this
->prophesize(EntityFinderInterface::class);
}
protected abstract function createTargetPluginInstance(array $configuration = []);
protected function getEntityStorageClass() {
return EntityStorageInterface::class;
}
protected function getReferencableEntityTypeId() {
return 'referenceable_entity_type';
}
protected function buildContainer() {
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityTypeManager
->reveal());
$container
->set('string_translation', $this
->getStringTranslationStub());
\Drupal::setContainer($container);
}
protected function createTargetDefinitionMock() {
$referencable_entity_type_id = $this
->getReferencableEntityTypeId();
$method = $this
->getMethod($this
->getTargetClass(), 'prepareTarget')
->getClosure();
$field_definition_mock = $this
->getMockFieldDefinition([
'target_type' => $referencable_entity_type_id,
'handler_settings' => [
'target_bundles' => [],
],
]);
$field_definition_mock
->expects($this
->once())
->method('getSetting')
->willReturn($referencable_entity_type_id);
return $method($field_definition_mock);
}
protected abstract function createReferencableEntityType();
public function testPrepareTarget() {
$field_definition_mock = $this
->getMockFieldDefinition();
$field_definition_mock
->expects($this
->once())
->method('getSetting')
->will($this
->returnValue($this
->getReferencableEntityTypeId()));
$method = $this
->getMethod($this
->getTargetClass(), 'prepareTarget')
->getClosure();
$this
->assertInstanceof(FieldTargetDefinition::class, $method($field_definition_mock));
}
public function testPrepareValueEmptyFeed() {
$method = $this
->getProtectedClosure($this
->createTargetPluginInstance(), 'prepareValue');
$values = [
'target_id' => '',
];
$this
->expectException(EmptyFeedException::class);
$method(0, $values);
}
}