.../Unit/Normalizer/TimestampNormalizerTest.php | 126 +++++++++++++++++++++ 1 file changed, 126 insertions(+) diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/TimestampNormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/TimestampNormalizerTest.php new file mode 100644 index 0000000..f6ddcf2 --- /dev/null +++ b/core/modules/serialization/tests/src/Unit/Normalizer/TimestampNormalizerTest.php @@ -0,0 +1,126 @@ +normalizer = new TimestampNormalizer(); + $this->data = $this->prophesize(Timestamp::class); + } + + /** + * @covers ::supportsNormalization + */ + public function testSupportsNormalization() { + $this->assertTrue($this->normalizer->supportsNormalization($this->data->reveal())); + + $integer = $this->prophesize(IntegerData::class); + $this->assertFalse($this->normalizer->supportsNormalization($integer->reveal())); + + $datetime = $this->prophesize(DateTimeInterface::class); + $this->assertFalse($this->normalizer->supportsNormalization($datetime->reveal())); + } + + /** + * @covers ::supportsDenormalization + */ + public function testSupportsDenormalization() { + $this->assertTrue($this->normalizer->supportsDenormalization($this->data->reveal(), Timestamp::class)); + } + + /** + * @covers ::normalize + */ + public function testNormalize() { + $expected = ['value' => '2016-11-06T09:02:00+00:00', 'format' => \DateTime::RFC3339]; + + $this->data->getValue() + ->willReturn(1478422920); + + $normalized = $this->normalizer->normalize($this->data->reveal()); + $this->assertSame($expected, $normalized); + } + + /** + * Tests the denormalize function with good data. + * + * @covers ::denormalize + * @dataProvider providerTestDenormalizeValidFormats + */ + public function testDenormalizeValidFormats($value, $expected) { + $normalized = ['value' => $value]; + + $denormalized = $this->normalizer->denormalize($normalized, Timestamp::class, NULL, []); + $this->assertSame(['value' => $expected], $denormalized); + } + + /** + * Data provider for testDenormalizeValidFormats. + * + * @return array + */ + public function providerTestDenormalizeValidFormats() { + $expected_stamp = 1478422920; + + $data = []; + + $data['U'] = [$expected_stamp, $expected_stamp]; + $data['RFC3339'] = ['2016-11-06T09:02:00+00:00', $expected_stamp]; + $data['RFC3339 +0100'] = ['2016-11-06T09:02:00+01:00', $expected_stamp - 1 * 3600]; + $data['RFC3339 -0600'] = ['2016-11-06T09:02:00-06:00', $expected_stamp + 6 * 3600]; + + $data['ISO8601'] = ['2016-11-06T09:02:00+0000', $expected_stamp]; + $data['ISO8601 +0100'] = ['2016-11-06T09:02:00+0100', $expected_stamp - 1 * 3600]; + $data['ISO8601 -0600'] = ['2016-11-06T09:02:00-0600', $expected_stamp + 6 * 3600]; + + return $data; + } + + /** + * Tests the denormalize function with bad data. + * + * @covers ::denormalize + */ + public function testDenormalizeException() { + $this->setExpectedException(UnexpectedValueException::class, 'The specified date "2016/11/06 09:02am GMT" is not in an accepted format: "U" (UNIX timestamp), "Y-m-d\TH:i:sO" (ISO 8601), "Y-m-d\TH:i:sP" (RFC 3339).'); + + $normalized = ['value' => '2016/11/06 09:02am GMT']; + + $this->normalizer->denormalize($normalized, Timestamp::class, NULL, []); + } + +}