abstract class EntityReferenceTestBase in Feeds 8.3
Base class for entity reference target tests.
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\feeds\Unit\FeedsUnitTestCase uses FeedsMockingTrait, FeedsReflectionTrait
- class \Drupal\Tests\feeds\Unit\Feeds\Target\FieldTargetTestBase
- class \Drupal\Tests\feeds\Unit\Feeds\Target\EntityReferenceTestBase
- class \Drupal\Tests\feeds\Unit\Feeds\Target\FieldTargetTestBase
- class \Drupal\Tests\feeds\Unit\FeedsUnitTestCase uses FeedsMockingTrait, FeedsReflectionTrait
Expanded class hierarchy of EntityReferenceTestBase
File
- tests/
src/ Unit/ Feeds/ Target/ EntityReferenceTestBase.php, line 15
Namespace
Drupal\Tests\feeds\Unit\Feeds\TargetView source
abstract class EntityReferenceTestBase extends FieldTargetTestBase {
/**
* The entity type manager prophecy used in the test.
*
* @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The entity storage prophecy used in the test.
*
* @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityStorageInterface
*/
protected $entityStorage;
/**
* The Feeds entity finder service.
*
* @var \Drupal\feeds\EntityFinderInterface
*/
protected $entityFinder;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$referencable_entity_type_id = $this
->getReferencableEntityTypeId();
// Entity type manager.
$this->entityTypeManager = $this
->prophesize(EntityTypeManagerInterface::class);
// Entity storage (needed for entity queries).
$this->entityStorage = $this
->prophesize($this
->getEntityStorageClass());
$this->entityTypeManager
->getStorage($referencable_entity_type_id)
->willReturn($this->entityStorage);
// Made-up entity type that we are referencing to.
$this->entityTypeManager
->getDefinition($referencable_entity_type_id)
->willReturn($this
->createReferencableEntityType())
->shouldBeCalled();
// Entity finder.
$this->entityFinder = $this
->prophesize(EntityFinderInterface::class);
}
/**
* Creates a new target plugin instance.
*
* @param array $configuration
* (optional) The configuration for the target plugin.
*
* @return \Drupal\feeds\Plugin\Type\Target\TargetInterface
* A FeedsTarget plugin instance.
*/
protected abstract function createTargetPluginInstance(array $configuration = []);
/**
* Returns the entity storage class name to use in this test.
*
* @return string
* The full name of the entity storage class.
*/
protected function getEntityStorageClass() {
return EntityStorageInterface::class;
}
/**
* Returns the entity type machine name to use in this test.
*
* @return string
* The entity type ID.
*/
protected function getReferencableEntityTypeId() {
return 'referenceable_entity_type';
}
/**
* Builds the Drupal service container.
*/
protected function buildContainer() {
// EntityReference::prepareTarget() accesses the entity type manager from
// the global container.
// @see \Drupal\feeds\Feeds\Target\EntityReference::prepareTarget()
$container = new ContainerBuilder();
$container
->set('entity_type.manager', $this->entityTypeManager
->reveal());
$container
->set('string_translation', $this
->getStringTranslationStub());
\Drupal::setContainer($container);
}
/**
* Creates a Feeds target definition mock.
*
* @return \Drupal\feeds\TargetDefinitionInterface
* A mocked target definition.
*/
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);
}
/**
* Creates a referencable entity type instance.
*
* @return \Drupal\Core\Entity\EntityTypeInterface
* The entity type to use in tests.
*/
protected abstract function createReferencableEntityType();
/**
* @covers ::prepareTarget
*/
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));
}
/**
* Tests prepareValue() without passing values.
*
* @covers ::prepareValue
*/
public function testPrepareValueEmptyFeed() {
$method = $this
->getProtectedClosure($this
->createTargetPluginInstance(), 'prepareValue');
$values = [
'target_id' => '',
];
$this
->expectException(EmptyFeedException::class);
$method(0, $values);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityReferenceTestBase:: |
protected | property | The Feeds entity finder service. | |
EntityReferenceTestBase:: |
protected | property | The entity storage prophecy used in the test. | |
EntityReferenceTestBase:: |
protected | property | The entity type manager prophecy used in the test. | |
EntityReferenceTestBase:: |
protected | function | Builds the Drupal service container. | |
EntityReferenceTestBase:: |
abstract protected | function | Creates a referencable entity type instance. | 3 |
EntityReferenceTestBase:: |
protected | function | Creates a Feeds target definition mock. | |
EntityReferenceTestBase:: |
abstract protected | function | Creates a new target plugin instance. | 3 |
EntityReferenceTestBase:: |
protected | function | Returns the entity storage class name to use in this test. | 1 |
EntityReferenceTestBase:: |
protected | function | Returns the entity type machine name to use in this test. | 1 |
EntityReferenceTestBase:: |
public | function |
Overrides FeedsUnitTestCase:: |
2 |
EntityReferenceTestBase:: |
public | function |
@covers ::prepareTarget Overrides FieldTargetTestBase:: |
1 |
EntityReferenceTestBase:: |
public | function | Tests prepareValue() without passing values. | 1 |
FeedsMockingTrait:: |
protected | function | Mocks an account object. | |
FeedsMockingTrait:: |
protected | function | Returns a mocked AccountSwitcher object. | |
FeedsMockingTrait:: |
protected | function | Returns a mocked feed entity. | |
FeedsMockingTrait:: |
protected | function | Returns a mocked feed type entity. | |
FeedsMockingTrait:: |
protected | function | Mocks a field definition. | 1 |
FeedsMockingTrait:: |
protected | function | Mocks the file system. | |
FeedsReflectionTrait:: |
protected | function | Calls a protected method on the given object. | |
FeedsReflectionTrait:: |
protected | function | Gets a ReflectionMethod for a class method. | |
FeedsReflectionTrait:: |
protected | function | Returns a dynamically created closure for the object's method. | |
FeedsReflectionTrait:: |
protected | function | Sets a protected property. | |
FeedsUnitTestCase:: |
protected | function | Returns the absolute directory path of the Feeds module. | |
FeedsUnitTestCase:: |
protected | function | Defines stub constants. | |
FeedsUnitTestCase:: |
protected | function | Returns a mock stream wrapper manager. | |
FeedsUnitTestCase:: |
protected | function | Returns the absolute directory path of the resources folder. | |
FieldTargetTestBase:: |
abstract protected | function | Returns the target class. | 20 |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 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. |