You are here

class ConfigEntityReferenceTest in Feeds 8.3

Same name in this branch
  1. 8.3 tests/src/Unit/Feeds/Target/ConfigEntityReferenceTest.php \Drupal\Tests\feeds\Unit\Feeds\Target\ConfigEntityReferenceTest
  2. 8.3 tests/src/Kernel/Feeds/Target/ConfigEntityReferenceTest.php \Drupal\Tests\feeds\Kernel\Feeds\Target\ConfigEntityReferenceTest

@coversDefaultClass \Drupal\feeds\Feeds\Target\ConfigEntityReference @group feeds

Hierarchy

Expanded class hierarchy of ConfigEntityReferenceTest

File

tests/src/Unit/Feeds/Target/ConfigEntityReferenceTest.php, line 15

Namespace

Drupal\Tests\feeds\Unit\Feeds\Target
View source
class ConfigEntityReferenceTest extends ConfigEntityReferenceTestBase {

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this
      ->buildContainer();
  }

  /**
   * {@inheritdoc}
   */
  protected function createTargetPluginInstance(array $configuration = []) {
    $configuration += [
      'feed_type' => $this
        ->createMock(FeedTypeInterface::class),
      'target_definition' => $this
        ->createTargetDefinitionMock(),
      'reference_by' => 'id',
    ];
    return new ConfigEntityReference($configuration, 'config_entity_reference', [], $this->entityTypeManager
      ->reveal(), $this->entityFinder
      ->reveal(), $this->transliteration
      ->reveal(), $this->typedConfigManager
      ->reveal());
  }

  /**
   * {@inheritdoc}
   */
  protected function getTargetClass() {
    return ConfigEntityReference::class;
  }

  /**
   * {@inheritdoc}
   */
  protected function createReferencableEntityType() {
    $referenceable_entity_type = $this
      ->prophesize(ConfigEntityTypeInterface::class);
    $referenceable_entity_type
      ->entityClassImplements(ConfigEntityInterface::class)
      ->willReturn(TRUE)
      ->shouldBeCalled();
    $referenceable_entity_type
      ->getKey('label')
      ->willReturn('label');
    $referenceable_entity_type
      ->getConfigPrefix()
      ->willReturn('foo.foo');
    return $referenceable_entity_type;
  }

  /**
   * Tests finding an entity by ID.
   *
   * @covers ::prepareValue
   * @covers ::findEntity
   */
  public function testPrepareValue() {
    $this->entityFinder
      ->findEntities($this
      ->getReferencableEntityTypeId(), 'id', 'foo')
      ->willReturn([
      'foo',
    ])
      ->shouldBeCalled();
    $method = $this
      ->getProtectedClosure($this
      ->createTargetPluginInstance(), 'prepareValue');
    $values = [
      'target_id' => 'foo',
    ];
    $method(0, $values);
    $this
      ->assertSame($values, [
      'target_id' => 'foo',
    ]);
  }

  /**
   * Tests prepareValue() method without match.
   *
   * @covers ::prepareValue
   * @covers ::findEntity
   */
  public function testPrepareValueReferenceNotFound() {
    $this->entityFinder
      ->findEntities($this
      ->getReferencableEntityTypeId(), 'id', 'bar')
      ->willReturn([])
      ->shouldBeCalled();
    $method = $this
      ->getProtectedClosure($this
      ->createTargetPluginInstance(), 'prepareValue');
    $values = [
      'target_id' => 'bar',
    ];
    $this
      ->expectException(ReferenceNotFoundException::class);
    $this
      ->expectExceptionMessage("Referenced entity not found for field <em class=\"placeholder\">id</em> with value <em class=\"placeholder\">bar</em>.");
    $method(0, $values);
  }

  /**
   * @covers ::getSummary
   */
  public function testGetSummary() {
    $expected = [
      'Reference by: <em class="placeholder">ID</em>',
    ];
    $summary = $this
      ->createTargetPluginInstance()
      ->getSummary();
    foreach ($summary as $key => $value) {
      $summary[$key] = (string) $value;
    }
    $this
      ->assertEquals($expected, $summary);
  }

  /**
   * @covers ::getSummary
   */
  public function testGetSummaryNoReferenceBySet() {
    $target_plugin = $this
      ->createTargetPluginInstance([
      'reference_by' => NULL,
    ]);
    $expected = [
      'Please select a field to reference by.',
    ];
    $summary = $target_plugin
      ->getSummary();
    foreach ($summary as $key => $value) {
      $summary[$key] = (string) $value;
    }
    $this
      ->assertEquals($expected, $summary);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigEntityReferenceTest::createReferencableEntityType protected function Creates a referencable entity type instance. Overrides EntityReferenceTestBase::createReferencableEntityType
ConfigEntityReferenceTest::createTargetPluginInstance protected function Creates a new target plugin instance. Overrides EntityReferenceTestBase::createTargetPluginInstance
ConfigEntityReferenceTest::getTargetClass protected function Returns the target class. Overrides FieldTargetTestBase::getTargetClass
ConfigEntityReferenceTest::setUp public function Overrides ConfigEntityReferenceTestBase::setUp
ConfigEntityReferenceTest::testGetSummary public function @covers ::getSummary
ConfigEntityReferenceTest::testGetSummaryNoReferenceBySet public function @covers ::getSummary
ConfigEntityReferenceTest::testPrepareValue public function Tests finding an entity by ID.
ConfigEntityReferenceTest::testPrepareValueReferenceNotFound public function Tests prepareValue() method without match.
ConfigEntityReferenceTestBase::$transliteration protected property The transliteration manager.
ConfigEntityReferenceTestBase::$typedConfigManager protected property The manager for managing config schema type plugins.
EntityReferenceTestBase::$entityFinder protected property The Feeds entity finder service.
EntityReferenceTestBase::$entityStorage protected property The entity storage prophecy used in the test.
EntityReferenceTestBase::$entityTypeManager protected property The entity type manager prophecy used in the test.
EntityReferenceTestBase::buildContainer protected function Builds the Drupal service container.
EntityReferenceTestBase::createTargetDefinitionMock protected function Creates a Feeds target definition mock.
EntityReferenceTestBase::getEntityStorageClass protected function Returns the entity storage class name to use in this test. 1
EntityReferenceTestBase::getReferencableEntityTypeId protected function Returns the entity type machine name to use in this test. 1
EntityReferenceTestBase::testPrepareTarget public function @covers ::prepareTarget Overrides FieldTargetTestBase::testPrepareTarget 1
EntityReferenceTestBase::testPrepareValueEmptyFeed public function Tests prepareValue() without passing values. 1
FeedsMockingTrait::getMockAccount protected function Mocks an account object.
FeedsMockingTrait::getMockedAccountSwitcher protected function Returns a mocked AccountSwitcher object.
FeedsMockingTrait::getMockFeed protected function Returns a mocked feed entity.
FeedsMockingTrait::getMockFeedType protected function Returns a mocked feed type entity.
FeedsMockingTrait::getMockFieldDefinition protected function Mocks a field definition. 1
FeedsMockingTrait::getMockFileSystem protected function Mocks the file system.
FeedsReflectionTrait::callProtectedMethod protected function Calls a protected method on the given object.
FeedsReflectionTrait::getMethod protected function Gets a ReflectionMethod for a class method.
FeedsReflectionTrait::getProtectedClosure protected function Returns a dynamically created closure for the object's method.
FeedsReflectionTrait::setProtectedProperty protected function Sets a protected property.
FeedsUnitTestCase::absolutePath protected function Returns the absolute directory path of the Feeds module.
FeedsUnitTestCase::defineConstants protected function Defines stub constants.
FeedsUnitTestCase::getMockStreamWrapperManager protected function Returns a mock stream wrapper manager.
FeedsUnitTestCase::resourcesPath protected function Returns the absolute directory path of the resources folder.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.