function TimestampItemNormalizerTest::testDenormalize

Same name in other branches
  1. 9 core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\TimestampItemNormalizerTest::testDenormalize()
  2. 8.9.x core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\TimestampItemNormalizerTest::testDenormalize()
  3. 11.x core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\TimestampItemNormalizerTest::testDenormalize()

@covers ::denormalize

File

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

Class

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

Namespace

Drupal\Tests\serialization\Unit\Normalizer

Code

public function testDenormalize() : void {
    $timestamp_item_normalization = [
        'value' => $this->randomMachineName(),
        'format' => \DateTime::RFC3339,
    ];
    $timestamp_data_denormalization = $this->randomMachineName();
    $timestamp_item = $this->createTimestampItemProphecy();
    // The field item should get the Timestamp @DataType denormalization set as
    // a value, in FieldItemNormalizer::denormalize().
    $timestamp_item->setValue([
        'value' => $timestamp_data_denormalization,
    ])
        ->shouldBeCalled();
    // Avoid a static method call by returning dummy serialized property data.
    $field_definition = $this->prophesize(FieldDefinitionInterface::class);
    $timestamp_item->getFieldDefinition()
        ->willReturn($field_definition->reveal())
        ->shouldBeCalled();
    $timestamp_item->getPluginDefinition()
        ->willReturn([
        'serialized_property_names' => [
            'foo' => 'bar',
        ],
    ])
        ->shouldBeCalled();
    $entity = $this->prophesize(EntityInterface::class);
    $entity_type = $this->prophesize(EntityTypeInterface::class);
    $entity->getEntityType()
        ->willReturn($entity_type->reveal())
        ->shouldBeCalled();
    $timestamp_item->getEntity()
        ->willReturn($entity->reveal())
        ->shouldBeCalled();
    $context = [
        'target_instance' => $timestamp_item->reveal(),
        'datetime_allowed_formats' => [
            \DateTime::RFC3339,
        ],
    ];
    // Mock Serializer service, to assert that the Timestamp @DataType
    // denormalizer would be called.
    $serializer_prophecy = $this->prophesize(Serializer::class);
    // This is where \Drupal\serialization\Normalizer\TimestampNormalizer would
    // be called.
    $serializer_prophecy->denormalize($timestamp_item_normalization['value'], Timestamp::class, NULL, $context)
        ->willReturn($timestamp_data_denormalization)
        ->shouldBeCalled();
    $this->normalizer
        ->setSerializer($serializer_prophecy->reveal());
    $denormalized = $this->normalizer
        ->denormalize($timestamp_item_normalization, TimestampItem::class, NULL, $context);
    $this->assertInstanceOf(TimestampItem::class, $denormalized);
}

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