You are here

public function FieldItemNormalizerTest::testNormalize in Tome 8

Tests the normalize() method.

@covers ::normalize

File

modules/tome_sync/tests/src/Unit/Normalizer/FieldItemNormalizerTest.php, line 25

Class

FieldItemNormalizerTest
@coversDefaultClass \Drupal\tome_sync\Normalizer\FieldItemNormalizer @group tome_sync

Namespace

Drupal\Tests\tome_sync\Unit\Normalizer

Code

public function testNormalize() {
  $normalizer = new FieldItemNormalizer();
  $serializer = $this
    ->prophesize(Serializer::class);
  $serializer
    ->normalize(Argument::cetera())
    ->willReturn('foo');
  $normalizer
    ->setSerializer($serializer
    ->reveal());
  $data = $this
    ->prophesize(DataDefinitionInterface::class);
  $data
    ->isComputed()
    ->willReturn(FALSE);
  $computed_data = $this
    ->prophesize(DataDefinitionInterface::class);
  $computed_data
    ->isComputed()
    ->willReturn(TRUE);
  $data_definitions = [
    'field_foo' => $data,
    'field_computed' => $computed_data,
  ];
  $typed_data = $this
    ->prophesize(TypedDataInterface::class);
  $data = $this
    ->prophesize(DataDefinitionInterface::class);
  $data
    ->isInternal()
    ->willReturn(FALSE);
  $typed_data
    ->getDataDefinition()
    ->willReturn($data);
  $typed_data_definitions = [
    'field_foo' => $typed_data,
    'field_computed' => $typed_data,
  ];
  $data_definition = $this
    ->prophesize(ComplexDataDefinitionInterface::class);
  $data_definition
    ->getPropertyDefinitions()
    ->willReturn($data_definitions)
    ->shouldBeCalled();
  $field_item = $this
    ->prophesize(FieldItemInterface::class);
  $field_item
    ->getProperties(TRUE)
    ->willReturn($typed_data_definitions)
    ->shouldBeCalled();
  $field_item
    ->getDataDefinition()
    ->willReturn($data_definition
    ->reveal());
  $normalized = $normalizer
    ->normalize($field_item
    ->reveal());
  $this
    ->assertSame([
    'field_foo' => 'foo',
  ], $normalized, 'Computed fields were removed properly.');
}