You are here

public function ExistenceCheckingTest::testCreateEntityDoesCallEntityExistsWhenEnabled in YAML Content 8

Confirm createEntity does check for existing entities when enabled.

@dataProvider contentDataProvider

@covers ::createEntity @covers ::existenceCheck @covers ::setExistenceCheck

Parameters

string $entity_type: The entity type machine name for the content being tested.

array $test_content: Import content for this test scenario.

File

tests/src/Unit/ContentLoader/ExistenceCheckingTest.php, line 208

Class

ExistenceCheckingTest
Test the existence checking functionality of the ContentLoader class.

Namespace

Drupal\Tests\yaml_content\Unit\ContentLoader

Code

public function testCreateEntityDoesCallEntityExistsWhenEnabled($entity_type, array $test_content) {

  // Stub methods used in the buildEntity() method.
  $this->contentLoader = $this
    ->getContentLoaderMock([
    'getContentAttributes',
    'getEntityStorage',
    'entityExists',
  ]);

  // Ensure existence checking should be disabled.
  $this->contentLoader
    ->setExistenceCheck(TRUE);

  // The `entityExists()` method should never be called.
  $this->contentLoader
    ->expects($this
    ->once())
    ->method('entityExists');

  // Override `getContentAttributes()` to return values based on test data.
  $attributes = $this
    ->getContentAttributes($entity_type, $test_content);
  $this->contentLoader
    ->expects($this
    ->once())
    ->method('getContentAttributes')
    ->willReturn($attributes);

  // Mock the entity storage to confirm `create()` was called.
  $storage_handler_mock = $this
    ->getMockForAbstractClass(EntityStorageInterface::class);
  $storage_handler_mock
    ->expects($this
    ->once())
    ->method('create')
    ->with($attributes['property']);

  // Return the mocked storage handler for testing.
  $this->contentLoader
    ->expects($this
    ->once())
    ->method('getEntityStorage')
    ->willReturn($storage_handler_mock);
  $this->contentLoader
    ->createEntity($entity_type, $test_content);
}