function TimestampItemNormalizerTest::testNormalize

Same name and namespace in other branches
  1. 9 core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\TimestampItemNormalizerTest::testNormalize()
  2. 8.9.x core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\TimestampItemNormalizerTest::testNormalize()
  3. 11.x 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 79

Class

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

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testNormalize() : void {
  // 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.