You are here

class TimestampItemNormalizerTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\TimestampItemNormalizerTest
  2. 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 24

Namespace

Drupal\Tests\serialization\Unit\Normalizer
View source
class TimestampItemNormalizerTest extends UnitTestCase {

  /**
   * @var \Drupal\serialization\Normalizer\TimestampItemNormalizer
   */
  protected $normalizer;

  /**
   * The test TimestampItem.
   *
   * @var \Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem
   */
  protected $item;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->normalizer = new TimestampItemNormalizer();
  }

  /**
   * @covers ::supportsNormalization
   */
  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()));
  }

  /**
   * @covers ::supportsDenormalization
   */
  public function testSupportsDenormalization() {
    $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() {

    // 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() {
    $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
   */
  protected function createTimestampItemProphecy() {
    $timestamp_item = $this
      ->prophesize(TimestampItem::class);
    $timestamp_item
      ->getParent()
      ->willReturn(TRUE);
    return $timestamp_item;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
TimestampItemNormalizerTest::$item protected property The test TimestampItem.
TimestampItemNormalizerTest::$normalizer protected property
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::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.