class TimestampItemNormalizerTest

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

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

@group serialization @coversDefaultClass \Drupal\serialization\Normalizer\TimestampItemNormalizer

Hierarchy

Expanded class hierarchy of TimestampItemNormalizerTest

See also

\Drupal\serialization\Normalizer\TimestampNormalizer

File

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

Namespace

Drupal\Tests\serialization\Unit\Normalizer
View source
class TimestampItemNormalizerTest extends UnitTestCase {
    
    /**
     * The time stamp normalizer.
     *
     * @var \Drupal\serialization\Normalizer\TimestampItemNormalizer
     */
    protected $normalizer;
    
    /**
     * The test TimestampItem.
     *
     * @var \Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem
     */
    protected $item;
    
    /**
     * {@inheritdoc}
     */
    protected function setUp() : void {
        parent::setUp();
        $this->normalizer = new TimestampItemNormalizer();
    }
    
    /**
     * @covers ::supportsNormalization
     */
    public function testSupportsNormalization() : void {
        $timestamp_item = $this->createTimestampItemProphecy();
        $this->assertTrue($this->normalizer
            ->supportsNormalization($timestamp_item->reveal()));
        $entity_ref_item = $this->prophesize(EntityReferenceItem::class);
        $this->assertFalse($this->normalizer
            ->supportsNormalization($entity_ref_item->reveal()));
    }
    
    /**
     * @covers ::supportsDenormalization
     */
    public function testSupportsDenormalization() : void {
        $timestamp_item = $this->createTimestampItemProphecy();
        $this->assertTrue($this->normalizer
            ->supportsDenormalization($timestamp_item->reveal(), TimestampItem::class));
        // CreatedItem extends regular TimestampItem.
        $timestamp_item = $this->prophesize(CreatedItem::class);
        $this->assertTrue($this->normalizer
            ->supportsDenormalization($timestamp_item->reveal(), TimestampItem::class));
        $entity_ref_item = $this->prophesize(EntityReferenceItem::class);
        $this->assertFalse($this->normalizer
            ->supportsNormalization($entity_ref_item->reveal(), TimestampItem::class));
    }
    
    /**
     * @covers ::normalize
     * @see \Drupal\Tests\serialization\Unit\Normalizer\TimestampNormalizerTest
     */
    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);
    }
    
    /**
     * @covers ::denormalize
     */
    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);
    }
    
    /**
     * Creates a TimestampItem prophecy.
     *
     * @return \Prophecy\Prophecy\ObjectProphecy<\Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem>
     *   The TimestampItem prophecy.
     */
    protected function createTimestampItemProphecy() : ObjectProphecy {
        $timestamp_item = $this->prophesize(TimestampItem::class);
        $timestamp_item->getParent()
            ->willReturn(TRUE);
        return $timestamp_item;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
TimestampItemNormalizerTest::$item protected property The test TimestampItem.
TimestampItemNormalizerTest::$normalizer protected property The time stamp normalizer.
TimestampItemNormalizerTest::createTimestampItemProphecy protected function Creates a TimestampItem prophecy.
TimestampItemNormalizerTest::setUp protected function Overrides UnitTestCase::setUp
TimestampItemNormalizerTest::testDenormalize public function @covers ::denormalize
TimestampItemNormalizerTest::testNormalize public function @covers ::normalize
TimestampItemNormalizerTest::testSupportsDenormalization public function @covers ::supportsDenormalization
TimestampItemNormalizerTest::testSupportsNormalization public function @covers ::supportsNormalization
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setDebugDumpHandler public static function Registers the dumper CLI handler when the DebugDump extension is enabled.

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