TimestampItemNormalizerTest.php in Drupal 9
File
core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php
View source
<?php
namespace Drupal\Tests\serialization\Unit\Normalizer;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\CreatedItem;
use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
use Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\Plugin\DataType\Timestamp;
use Drupal\serialization\Normalizer\TimestampItemNormalizer;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\Serializer\Serializer;
class TimestampItemNormalizerTest extends UnitTestCase {
protected $normalizer;
protected $item;
protected function setUp() : void {
parent::setUp();
$this->normalizer = new TimestampItemNormalizer();
}
public function testSupportsNormalization() {
$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()));
}
public function testSupportsDenormalization() {
$timestamp_item = $this
->createTimestampItemProphecy();
$this
->assertTrue($this->normalizer
->supportsDenormalization($timestamp_item
->reveal(), TimestampItem::class));
$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));
}
public function testNormalize() {
$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();
$timestamp_datetype_normalization = $this
->randomMachineName();
$serializer_prophecy = $this
->prophesize(Serializer::class);
$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);
}
public function testDenormalize() {
$timestamp_item_normalization = [
'value' => $this
->randomMachineName(),
'format' => \DateTime::RFC3339,
];
$timestamp_data_denormalization = $this
->randomMachineName();
$timestamp_item = $this
->createTimestampItemProphecy();
$timestamp_item
->setValue([
'value' => $timestamp_data_denormalization,
])
->shouldBeCalled();
$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,
],
];
$serializer_prophecy = $this
->prophesize(Serializer::class);
$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);
}
protected function createTimestampItemProphecy() {
$timestamp_item = $this
->prophesize(TimestampItem::class);
$timestamp_item
->getParent()
->willReturn(TRUE);
return $timestamp_item;
}
}