class DateTimeNormalizerTest
Same name in other branches
- 9 core/modules/serialization/tests/src/Unit/Normalizer/DateTimeNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\DateTimeNormalizerTest
- 8.9.x core/modules/serialization/tests/src/Unit/Normalizer/DateTimeNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\DateTimeNormalizerTest
- 11.x core/modules/serialization/tests/src/Unit/Normalizer/DateTimeNormalizerTest.php \Drupal\Tests\serialization\Unit\Normalizer\DateTimeNormalizerTest
Unit test coverage for @DataTypes implementing DateTimeInterface.
@group serialization @coversDefaultClass \Drupal\serialization\Normalizer\DateTimeNormalizer
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\serialization\Unit\Normalizer\DateTimeNormalizerTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of DateTimeNormalizerTest
See also
\Drupal\Core\TypedData\Type\DateTimeInterface
File
-
core/
modules/ serialization/ tests/ src/ Unit/ Normalizer/ DateTimeNormalizerTest.php, line 24
Namespace
Drupal\Tests\serialization\Unit\NormalizerView source
class DateTimeNormalizerTest extends UnitTestCase {
/**
* The tested data type's normalizer.
*
* @var \Drupal\serialization\Normalizer\DateTimeNormalizer
*/
protected $normalizer;
/**
* The tested data type.
*
* @var \Drupal\Core\TypedData\Type\DateTimeInterface
*/
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 DateTimeNormalizer($config_factory->reveal());
$this->data = $this->prophesize(DateTimeInterface::class);
}
/**
* @covers ::supportsNormalization
*/
public function testSupportsNormalization() : void {
$this->assertTrue($this->normalizer
->supportsNormalization($this->data
->reveal()));
$datetimeiso8601 = $this->prophesize(DateTimeIso8601::class);
$this->assertTrue($this->normalizer
->supportsNormalization($datetimeiso8601->reveal()));
$integer = $this->prophesize(IntegerData::class);
$this->assertFalse($this->normalizer
->supportsNormalization($integer->reveal()));
}
/**
* @covers ::supportsDenormalization
*/
public function testSupportsDenormalization() : void {
$this->assertTrue($this->normalizer
->supportsDenormalization($this->data
->reveal(), DateTimeInterface::class));
}
/**
* @covers ::normalize
*/
public function testNormalize() : void {
$random_rfc_3339_string = $this->randomMachineName();
$drupal_date_time = $this->prophesize(DateTimeNormalizerTestDrupalDateTime::class);
$drupal_date_time->setTimezone(new \DateTimeZone('Australia/Sydney'))
->willReturn($drupal_date_time->reveal());
$drupal_date_time->format(\DateTime::RFC3339)
->willReturn($random_rfc_3339_string);
$this->data
->getDateTime()
->willReturn($drupal_date_time->reveal());
$normalized = $this->normalizer
->normalize($this->data
->reveal());
$this->assertSame($random_rfc_3339_string, $normalized);
}
/**
* @covers ::normalize
*/
public function testNormalizeWhenNull() : void {
$this->data
->getDateTime()
->willReturn(NULL);
$normalized = $this->normalizer
->normalize($this->data
->reveal());
$this->assertNull($normalized);
}
/**
* Tests the denormalize function with good data.
*
* @covers ::denormalize
* @dataProvider providerTestDenormalizeValidFormats
*/
public function testDenormalizeValidFormats($normalized, $expected) : void {
$denormalized = $this->normalizer
->denormalize($normalized, DateTimeInterface::class, NULL, []);
$this->assertSame(0, $denormalized->getTimestamp() - $expected->getTimestamp());
$this->assertEquals($expected, $denormalized);
}
/**
* Data provider for testDenormalizeValidFormats.
*
* @return array
*/
public static function providerTestDenormalizeValidFormats() {
$data = [];
$data['RFC3339'] = [
'2016-11-06T09:02:00+00:00',
new \DateTimeImmutable('2016-11-06T09:02:00+00:00'),
];
$data['RFC3339 +0100'] = [
'2016-11-06T09:02:00+01:00',
new \DateTimeImmutable('2016-11-06T09:02:00+01:00'),
];
$data['RFC3339 -0600'] = [
'2016-11-06T09:02:00-06:00',
new \DateTimeImmutable('2016-11-06T09:02:00-06:00'),
];
$data['ISO8601'] = [
'2016-11-06T09:02:00+0000',
new \DateTimeImmutable('2016-11-06T09:02:00+00:00'),
];
$data['ISO8601 +0100'] = [
'2016-11-06T09:02:00+0100',
new \DateTimeImmutable('2016-11-06T09:02:00+01:00'),
];
$data['ISO8601 -0600'] = [
'2016-11-06T09:02:00-0600',
new \DateTimeImmutable('2016-11-06T09:02:00-06:00'),
];
return $data;
}
/**
* Tests the denormalize function with a user supplied format.
*
* @covers ::denormalize
* @dataProvider providerTestDenormalizeUserFormats
*/
public function testDenormalizeUserFormats($normalized, $format, $expected) : void {
$denormalized = $this->normalizer
->denormalize($normalized, DateTimeInterface::class, NULL, [
'datetime_allowed_formats' => [
$format,
],
]);
$this->assertSame(0, $denormalized->getTimestamp() - $expected->getTimestamp());
$this->assertEquals($expected, $denormalized);
}
/**
* Data provider for testDenormalizeUserFormats.
*
* @return array
*/
public static function providerTestDenormalizeUserFormats() {
$data = [];
$data['Y/m/d H:i:s P'] = [
'2016/11/06 09:02:00 +00:00',
'Y/m/d H:i:s P',
new \DateTimeImmutable('2016-11-06T09:02:00+00:00'),
];
$data['H:i:s Y/m/d P'] = [
'09:02:00 2016/11/06 +01:00',
'H:i:s Y/m/d P',
new \DateTimeImmutable('2016-11-06T09:02:00+01:00'),
];
$data['Y/m/d H:i:s'] = [
'09:02:00 2016/11/06',
'H:i:s Y/m/d',
new \DateTimeImmutable('2016-11-06T09:02:00+11:00'),
];
return $data;
}
/**
* Tests the denormalize function with bad data.
*
* @covers ::denormalize
*/
public function testDenormalizeException() : void {
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('The specified date "2016/11/06 09:02am GMT" is not in an accepted format: "Y-m-d\\TH:i:sP" (RFC 3339), "Y-m-d\\TH:i:sO" (ISO 8601).');
$normalized = '2016/11/06 09:02am GMT';
$this->normalizer
->denormalize($normalized, DateTimeInterface::class, NULL, []);
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
DateTimeNormalizerTest::$data | protected | property | The tested data type. | |||
DateTimeNormalizerTest::$normalizer | protected | property | The tested data type's normalizer. | |||
DateTimeNormalizerTest::providerTestDenormalizeUserFormats | public static | function | Data provider for testDenormalizeUserFormats. | |||
DateTimeNormalizerTest::providerTestDenormalizeValidFormats | public static | function | Data provider for testDenormalizeValidFormats. | |||
DateTimeNormalizerTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
DateTimeNormalizerTest::testDenormalizeException | public | function | Tests the denormalize function with bad data. | |||
DateTimeNormalizerTest::testDenormalizeUserFormats | public | function | Tests the denormalize function with a user supplied format. | |||
DateTimeNormalizerTest::testDenormalizeValidFormats | public | function | Tests the denormalize function with good data. | |||
DateTimeNormalizerTest::testNormalize | public | function | @covers ::normalize | |||
DateTimeNormalizerTest::testNormalizeWhenNull | public | function | @covers ::normalize | |||
DateTimeNormalizerTest::testSupportsDenormalization | public | function | @covers ::supportsDenormalization | |||
DateTimeNormalizerTest::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. | |||
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. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.