You are here

class DateTimeIso8601NormalizerTest in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\DateTimeIso8601NormalizerTest
  2. 9 core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\DateTimeIso8601NormalizerTest

Unit test coverage for the "datetime_iso8601" @DataType.

@coversDefaultClass \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer @group serialization

Hierarchy

Expanded class hierarchy of DateTimeIso8601NormalizerTest

See also

\Drupal\Core\TypedData\Plugin\DataType\DateTimeIso8601

\Drupal\datetime\Plugin\Field\FieldType\DateTimeItem::DATETIME_TYPE_DATE

File

core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php, line 29

Namespace

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

  /**
   * The tested data type's normalizer.
   *
   * @var \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer
   */
  protected $normalizer;

  /**
   * The tested data type.
   *
   * @var \Drupal\Core\TypedData\Plugin\DataType\DateTimeIso8601
   */
  protected $data;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $system_date_config = $this
      ->prophesize(ImmutableConfig::class);
    $system_date_config
      ->get('timezone.default')
      ->willReturn('Australia/Sydney');
    $config_factory = $this
      ->prophesize(ConfigFactoryInterface::class);
    $config_factory
      ->get('system.date')
      ->willReturn($system_date_config
      ->reveal());
    $this->normalizer = new DateTimeIso8601Normalizer($config_factory
      ->reveal());
    $this->data = $this
      ->prophesize(DateTimeIso8601::class);
  }

  /**
   * @covers ::supportsNormalization
   */
  public function testSupportsNormalization() {
    $this
      ->assertTrue($this->normalizer
      ->supportsNormalization($this->data
      ->reveal()));
    $datetime = $this
      ->prophesize(DateTimeInterface::class);
    $this
      ->assertFalse($this->normalizer
      ->supportsNormalization($datetime
      ->reveal()));
    $integer = $this
      ->prophesize(IntegerData::class);
    $this
      ->assertFalse($this->normalizer
      ->supportsNormalization($integer
      ->reveal()));
  }

  /**
   * @covers ::supportsDenormalization
   */
  public function testSupportsDenormalization() {
    $this
      ->assertTrue($this->normalizer
      ->supportsDenormalization($this->data
      ->reveal(), DateTimeIso8601::class));
  }

  /**
   * @covers ::normalize
   * @dataProvider providerTestNormalize
   */
  public function testNormalize($parent_field_item_class, $datetime_type, $expected_format) {
    $formatted_string = $this
      ->randomMachineName();
    $field_item = $this
      ->prophesize($parent_field_item_class);
    if ($parent_field_item_class === DateTimeItem::class) {
      $field_storage_definition = $this
        ->prophesize(FieldStorageDefinitionInterface::class);
      $field_storage_definition
        ->getSetting('datetime_type')
        ->willReturn($datetime_type);
      $field_definition = $this
        ->prophesize(FieldDefinitionInterface::class);
      $field_definition
        ->getFieldStorageDefinition()
        ->willReturn($field_storage_definition);
      $field_item
        ->getFieldDefinition()
        ->willReturn($field_definition);
    }
    else {
      $field_item
        ->getFieldDefinition(Argument::any())
        ->shouldNotBeCalled();
    }
    $this->data
      ->getParent()
      ->willReturn($field_item);
    $drupal_date_time = $this
      ->prophesize(DateTimeIso8601NormalizerTestDrupalDateTime::class);
    $drupal_date_time
      ->setTimezone(new \DateTimeZone('Australia/Sydney'))
      ->willReturn($drupal_date_time
      ->reveal());
    $drupal_date_time
      ->format($expected_format)
      ->willReturn($formatted_string);
    $this->data
      ->getDateTime()
      ->willReturn($drupal_date_time
      ->reveal());
    $normalized = $this->normalizer
      ->normalize($this->data
      ->reveal());
    $this
      ->assertSame($formatted_string, $normalized);
  }

  /**
   * @covers ::normalize
   * @dataProvider providerTestNormalize
   */
  public function testNormalizeWhenNull($parent_field_item_class, $datetime_type, $expected_format) {
    $field_item = $this
      ->prophesize($parent_field_item_class);
    if ($parent_field_item_class === DateTimeItem::class) {
      $field_storage_definition = $this
        ->prophesize(FieldStorageDefinitionInterface::class);
      $field_storage_definition
        ->getSetting('datetime_type')
        ->willReturn($datetime_type);
      $field_definition = $this
        ->prophesize(FieldDefinitionInterface::class);
      $field_definition
        ->getFieldStorageDefinition()
        ->willReturn($field_storage_definition);
      $field_item
        ->getFieldDefinition()
        ->willReturn($field_definition);
    }
    else {
      $field_item
        ->getFieldDefinition(Argument::any())
        ->shouldNotBeCalled();
    }
    $this->data
      ->getParent()
      ->willReturn($field_item);
    $this->data
      ->getDateTime()
      ->willReturn(NULL);
    $normalized = $this->normalizer
      ->normalize($this->data
      ->reveal());
    $this
      ->assertNull($normalized);
  }

  /**
   * Data provider for testNormalize.
   *
   * @return array
   */
  public function providerTestNormalize() {
    return [
      // @see \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem::DATETIME_TYPE_DATE
      'datetime field, configured to store only date: must be handled by DateTimeIso8601Normalizer' => [
        DateTimeItem::class,
        DateTimeItem::DATETIME_TYPE_DATE,
        // This expected format call proves that normalization is handled by \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer::normalize().
        'Y-m-d',
      ],
      // @see \Drupal\datetime\Plugin\Field\FieldType\DateTimeItem::DATETIME_TYPE_DATETIME
      'datetime field, configured to store date and time; must be handled by the parent normalizer' => [
        DateTimeItem::class,
        DateTimeItem::DATETIME_TYPE_DATETIME,
        \DateTime::RFC3339,
      ],
      'non-datetime field; must be handled by the parent normalizer' => [
        FieldItemBase::class,
        NULL,
        \DateTime::RFC3339,
      ],
    ];
  }

  /**
   * Tests the denormalize function with good data.
   *
   * @covers ::denormalize
   * @dataProvider providerTestDenormalizeValidFormats
   */
  public function testDenormalizeValidFormats($type, $normalized, $expected) {
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $field_definition
      ->getSetting('datetime_type')
      ->willReturn($type === 'date-only' ? DateTimeItem::DATETIME_TYPE_DATE : DateTimeItem::DATETIME_TYPE_DATETIME);
    $denormalized = $this->normalizer
      ->denormalize($normalized, DateTimeIso8601::class, NULL, [
      'field_definition' => $field_definition
        ->reveal(),
    ]);
    $this
      ->assertSame($expected, $denormalized);
  }

  /**
   * Data provider for testDenormalizeValidFormats.
   *
   * @return array
   */
  public function providerTestDenormalizeValidFormats() {
    $data = [];
    $data['just a date'] = [
      'date-only',
      '2016-11-06',
      '2016-11-06',
    ];
    $data['RFC3339'] = [
      'date+time',
      '2016-11-06T09:02:00+00:00',
      '2016-11-06T09:02:00',
    ];
    $data['RFC3339 +0100'] = [
      'date+time',
      '2016-11-06T09:02:00+01:00',
      '2016-11-06T08:02:00',
    ];
    $data['RFC3339 -0600'] = [
      'date+time',
      '2016-11-06T09:02:00-06:00',
      '2016-11-06T15:02:00',
    ];
    $data['ISO8601'] = [
      'date+time',
      '2016-11-06T09:02:00+0000',
      '2016-11-06T09:02:00',
    ];
    $data['ISO8601 +0100'] = [
      'date+time',
      '2016-11-06T09:02:00+0100',
      '2016-11-06T08:02:00',
    ];
    $data['ISO8601 -0600'] = [
      'date+time',
      '2016-11-06T09:02:00-0600',
      '2016-11-06T15:02:00',
    ];
    return $data;
  }

  /**
   * Tests the denormalize function with bad data for the date-only case.
   *
   * @covers ::denormalize
   */
  public function testDenormalizeDateOnlyException() {
    $this
      ->expectException(UnexpectedValueException::class);
    $this
      ->expectExceptionMessage('The specified date "2016/11/06" is not in an accepted format: "Y-m-d" (date-only).');
    $normalized = '2016/11/06';
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $field_definition
      ->getSetting('datetime_type')
      ->willReturn(DateTimeItem::DATETIME_TYPE_DATE);
    $this->normalizer
      ->denormalize($normalized, DateTimeIso8601::class, NULL, [
      'field_definition' => $field_definition
        ->reveal(),
    ]);
  }

  /**
   * Tests the denormalize function with bad data for the date+time case.
   *
   * @covers ::denormalize
   */
  public function testDenormalizeDateAndTimeException() {
    $this
      ->expectException(UnexpectedValueException::class);
    $this
      ->expectExceptionMessage('The specified date "on a rainy day" is not in an accepted format: "Y-m-d\\TH:i:sP" (RFC 3339), "Y-m-d\\TH:i:sO" (ISO 8601).');
    $normalized = 'on a rainy day';
    $field_definition = $this
      ->prophesize(FieldDefinitionInterface::class);
    $field_definition
      ->getSetting('datetime_type')
      ->willReturn(DateTimeItem::DATETIME_TYPE_DATETIME);
    $this->normalizer
      ->denormalize($normalized, DateTimeIso8601::class, NULL, [
      'field_definition' => $field_definition
        ->reveal(),
    ]);
  }

  /**
   * Tests the denormalize function with incomplete serialization context.
   *
   * @covers ::denormalize
   */
  public function testDenormalizeNoTargetInstanceOrFieldDefinitionException() {
    $this
      ->expectException(InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('$context[\'target_instance\'] or $context[\'field_definition\'] must be set to denormalize with the DateTimeIso8601Normalizer');
    $this->normalizer
      ->denormalize('', DateTimeIso8601::class, NULL, []);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DateTimeIso8601NormalizerTest::$data protected property The tested data type.
DateTimeIso8601NormalizerTest::$normalizer protected property The tested data type's normalizer.
DateTimeIso8601NormalizerTest::providerTestDenormalizeValidFormats public function Data provider for testDenormalizeValidFormats.
DateTimeIso8601NormalizerTest::providerTestNormalize public function Data provider for testNormalize.
DateTimeIso8601NormalizerTest::setUp protected function Overrides UnitTestCase::setUp
DateTimeIso8601NormalizerTest::testDenormalizeDateAndTimeException public function Tests the denormalize function with bad data for the date+time case.
DateTimeIso8601NormalizerTest::testDenormalizeDateOnlyException public function Tests the denormalize function with bad data for the date-only case.
DateTimeIso8601NormalizerTest::testDenormalizeNoTargetInstanceOrFieldDefinitionException public function Tests the denormalize function with incomplete serialization context.
DateTimeIso8601NormalizerTest::testDenormalizeValidFormats public function Tests the denormalize function with good data.
DateTimeIso8601NormalizerTest::testNormalize public function @covers ::normalize @dataProvider providerTestNormalize
DateTimeIso8601NormalizerTest::testNormalizeWhenNull public function @covers ::normalize @dataProvider providerTestNormalize
DateTimeIso8601NormalizerTest::testSupportsDenormalization public function @covers ::supportsDenormalization
DateTimeIso8601NormalizerTest::testSupportsNormalization public function @covers ::supportsNormalization
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 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.
UnitTestCase::setUpBeforeClass public static function