You are here

public function EntityReferenceItemNormalizerTest::testNormalize in Tome 8

Tests the normalize() method.

@covers ::normalize

File

modules/tome_sync/tests/src/Unit/Normalizer/EntityReferenceItemNormalizerTest.php, line 29

Class

EntityReferenceItemNormalizerTest
@coversDefaultClass \Drupal\tome_sync\Normalizer\EntityReferenceItemNormalizer @group tome_sync

Namespace

Drupal\Tests\tome_sync\Unit\Normalizer

Code

public function testNormalize() {
  $entity_repository = $this
    ->prophesize(EntityRepositoryInterface::class);
  $normalizer = new EntityReferenceItemNormalizer($entity_repository
    ->reveal());
  $serializer = $this
    ->prophesize(Serializer::class);
  $serializer
    ->normalize(Argument::cetera())
    ->willReturn(1);
  $normalizer
    ->setSerializer($serializer
    ->reveal());
  $entity = $this
    ->prophesize(EntityInterface::class);
  $entity
    ->hasLinkTemplate('canonical')
    ->willReturn(FALSE)
    ->shouldBeCalled();
  $entity
    ->uuid()
    ->willReturn('080e3add-f9d5-41ac-9821-eea55b7b42fb')
    ->shouldBeCalled();
  $entity
    ->getEntityTypeId()
    ->willReturn('test_type')
    ->shouldBeCalled();
  $entity_reference = $this
    ->prophesize(TypedDataInterface::class);
  $entity_reference
    ->getValue()
    ->willReturn($entity
    ->reveal())
    ->shouldBeCalled();
  $field_definition = $this
    ->prophesize(FieldDefinitionInterface::class);
  $field_definition
    ->getSetting('target_type')
    ->willReturn('test_type');
  $field_item = $this
    ->prophesize(EntityReferenceItem::class);
  $field_item
    ->getIterator()
    ->willReturn(new \ArrayIterator([
    'target_id' => [],
  ]));
  $field_item
    ->getFieldDefinition()
    ->willReturn($field_definition
    ->reveal());
  $field_item
    ->get('entity')
    ->willReturn($entity_reference)
    ->shouldBeCalled();
  $field_item
    ->getProperties(TRUE)
    ->willReturn([
    'target_id' => $this
      ->getTypedDataProperty(FALSE),
  ])
    ->shouldBeCalled();
  $normalized = $normalizer
    ->normalize($field_item
    ->reveal());
  $expected = [
    'target_type' => 'test_type',
    'target_uuid' => '080e3add-f9d5-41ac-9821-eea55b7b42fb',
  ];
  $this
    ->assertSame($expected, $normalized, 'The target_id and url keys were removed as expected.');
}