You are here

public function TimestampItemNormalizerTest::testNormalize in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\TimestampItemNormalizerTest::testNormalize()
  2. 10 core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\TimestampItemNormalizerTest::testNormalize()

@covers ::normalize

See also

\Drupal\Tests\serialization\Unit\Normalizer\TimestampNormalizerTest

File

core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php, line 77

Class

TimestampItemNormalizerTest
Tests that TimestampItem (de)normalization uses Timestamp (de)normalization.

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testNormalize() {

  // Mock TimestampItem @FieldType, which contains a Timestamp @DataType,
  // which has a DataDefinition.
  $data_definition = $this
    ->prophesize(DataDefinitionInterface::class);
  $data_definition
    ->isInternal()
    ->willReturn(FALSE)
    ->shouldBeCalled();
  $timestamp = $this
    ->prophesize(Timestamp::class);
  $timestamp
    ->getDataDefinition()
    ->willReturn($data_definition
    ->reveal())
    ->shouldBeCalled();
  $timestamp = $timestamp
    ->reveal();
  $timestamp_item = $this
    ->createTimestampItemProphecy();
  $timestamp_item
    ->getProperties(TRUE)
    ->willReturn([
    'value' => $timestamp,
  ])
    ->shouldBeCalled();

  // Mock Serializer service, to assert that the Timestamp @DataType
  // normalizer would be called.
  $timestamp_datetype_normalization = $this
    ->randomMachineName();
  $serializer_prophecy = $this
    ->prophesize(Serializer::class);

  // This is where \Drupal\serialization\Normalizer\TimestampNormalizer would
  // be called.
  $serializer_prophecy
    ->normalize($timestamp, NULL, [])
    ->willReturn($timestamp_datetype_normalization)
    ->shouldBeCalled();
  $this->normalizer
    ->setSerializer($serializer_prophecy
    ->reveal());
  $normalized = $this->normalizer
    ->normalize($timestamp_item
    ->reveal());
  $this
    ->assertSame([
    'value' => $timestamp_datetype_normalization,
    'format' => \DateTime::RFC3339,
  ], $normalized);
}