You are here

class DependencyFieldMapGeneratorTest in Entity Reference Integrity 8

Test the field map generator.

@group entity_reference_integrity

Hierarchy

Expanded class hierarchy of DependencyFieldMapGeneratorTest

File

tests/src/Unit/DependencyFieldMapGeneratorTest.php, line 17

Namespace

Drupal\Tests\entity_reference_integrity\Unit
View source
class DependencyFieldMapGeneratorTest extends UnitTestCase {

  /**
   * Test the field map.
   *
   * @dataProvider fieldMapTestCases
   */
  public function testFieldMap($has_custom_storage, $has_storage_definition, $revision_metadata_fields, $field_map, $expected_result) {
    $target_entity_type_id = 'target_entity_type_id';
    $entityFieldManager = $this
      ->createMock(EntityFieldManagerInterface::class);
    $entityFieldManager
      ->expects($this
      ->any())
      ->method('getFieldMapByFieldType')
      ->willReturn($field_map);
    $storage_definition = $this
      ->createMock(FieldStorageDefinitionInterface::class);
    $storage_definition
      ->expects($this
      ->any())
      ->method('hasCustomStorage')
      ->willReturn($has_custom_storage);
    $storage_definition
      ->expects($this
      ->any())
      ->method('getSetting')
      ->willReturn($target_entity_type_id);
    $entityFieldManager
      ->expects($this
      ->any())
      ->method('getFieldStorageDefinitions')
      ->willReturn($has_storage_definition ? [
      'field_name' => $storage_definition,
    ] : []);
    $entityTypeManager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $mockDefinition = $this
      ->createMock(ContentEntityTypeInterface::class);
    $mockDefinition
      ->method('getRevisionMetadataKeys')
      ->willReturn($revision_metadata_fields);
    $entityTypeManager
      ->method('getDefinition')
      ->willReturn($mockDefinition);
    $field_map = new DependencyFieldMapGenerator($entityFieldManager, $entityTypeManager, 'entity_reference', 'target_type');
    $generated_map = $field_map
      ->getReferencingFields($target_entity_type_id);
    $this
      ->assertEquals($expected_result, $generated_map);
  }

  /**
   * Test cases for ::testFieldMap.
   */
  public function fieldMapTestCases() {
    return [
      'Standard field' => [
        FALSE,
        TRUE,
        [],
        [
          'foo_entity_type_id' => [
            'field_name' => [],
          ],
        ],
        [
          'foo_entity_type_id' => [
            'field_name',
          ],
        ],
      ],
      'Custom storage field' => [
        TRUE,
        TRUE,
        [],
        [
          'foo_entity_type_id' => [
            'field_name' => [],
          ],
        ],
        [],
      ],
      'No storage definition' => [
        FALSE,
        FALSE,
        [],
        [
          'foo_entity_type_id' => [
            'field_name' => [],
          ],
        ],
        [],
      ],
      'Field is revision metadata' => [
        FALSE,
        TRUE,
        [
          'field_name',
        ],
        [
          'foo_entity_type_id' => [
            'field_name' => [],
          ],
        ],
        [],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencyFieldMapGeneratorTest::fieldMapTestCases public function Test cases for ::testFieldMap.
DependencyFieldMapGeneratorTest::testFieldMap public function Test the field map.
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.
UnitTestCase::setUp protected function 340