$this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
}
+ public function testNormalizeUsingTimeZonePassedInConstructor()
+ {
+ $normalizer = new DateTimeNormalizer(\DateTime::RFC3339, new \DateTimeZone('Japan'));
+
+ $this->assertSame('2016-12-01T00:00:00+09:00', $normalizer->normalize(new \DateTime('2016/12/01', new \DateTimeZone('Japan'))));
+ $this->assertSame('2016-12-01T09:00:00+09:00', $normalizer->normalize(new \DateTime('2016/12/01', new \DateTimeZone('UTC'))));
+ }
+
+ /**
+ * @dataProvider normalizeUsingTimeZonePassedInContextProvider
+ */
+ public function testNormalizeUsingTimeZonePassedInContext($expected, $input, $timezone)
+ {
+ $this->assertSame($expected, $this->normalizer->normalize($input, null, array(
+ DateTimeNormalizer::TIMEZONE_KEY => $timezone,
+ )));
+ }
+
+ public function normalizeUsingTimeZonePassedInContextProvider()
+ {
+ yield array('2016-12-01T00:00:00+00:00', new \DateTime('2016/12/01', new \DateTimeZone('UTC')), null);
+ yield array('2016-12-01T00:00:00+09:00', new \DateTime('2016/12/01', new \DateTimeZone('Japan')), new \DateTimeZone('Japan'));
+ yield array('2016-12-01T09:00:00+09:00', new \DateTime('2016/12/01', new \DateTimeZone('UTC')), new \DateTimeZone('Japan'));
+ yield array('2016-12-01T09:00:00+09:00', new \DateTimeImmutable('2016/12/01', new \DateTimeZone('UTC')), new \DateTimeZone('Japan'));
+ }
+
/**
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
* @expectedExceptionMessage The object must implement the "\DateTimeInterface".
$this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
}
+ public function testDenormalizeUsingTimezonePassedInConstructor()
+ {
+ $timezone = new \DateTimeZone('Japan');
+ $expected = new \DateTime('2016/12/01 17:35:00', $timezone);
+ $normalizer = new DateTimeNormalizer(null, $timezone);
+
+ $this->assertEquals($expected, $normalizer->denormalize('2016.12.01 17:35:00', \DateTime::class, null, array(
+ DateTimeNormalizer::FORMAT_KEY => 'Y.m.d H:i:s',
+ )));
+ }
+
public function testDenormalizeUsingFormatPassedInContext()
{
$this->assertEquals(new \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
$this->assertEquals(new \DateTime('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTime::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y.m.d|')));
}
+ /**
+ * @dataProvider denormalizeUsingTimezonePassedInContextProvider
+ */
+ public function testDenormalizeUsingTimezonePassedInContext($input, $expected, $timezone, $format = null)
+ {
+ $actual = $this->normalizer->denormalize($input, \DateTimeInterface::class, null, array(
+ DateTimeNormalizer::TIMEZONE_KEY => $timezone,
+ DateTimeNormalizer::FORMAT_KEY => $format,
+ ));
+
+ $this->assertEquals($expected, $actual);
+ }
+
+ public function denormalizeUsingTimezonePassedInContextProvider()
+ {
+ yield 'with timezone' => array(
+ '2016/12/01 17:35:00',
+ new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('Japan')),
+ new \DateTimeZone('Japan'),
+ );
+ yield 'with timezone as string' => array(
+ '2016/12/01 17:35:00',
+ new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('Japan')),
+ 'Japan',
+ );
+ yield 'with format without timezone information' => array(
+ '2016.12.01 17:35:00',
+ new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('Japan')),
+ new \DateTimeZone('Japan'),
+ 'Y.m.d H:i:s',
+ );
+ yield 'ignored with format with timezone information' => array(
+ '2016-12-01T17:35:00Z',
+ new \DateTimeImmutable('2016/12/01 17:35:00', new \DateTimeZone('UTC')),
+ 'Europe/Paris',
+ \DateTime::RFC3339,
+ );
+ }
+
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
}
+ /**
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+ * @expectedExceptionMessage The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.
+ */
+ public function testDenormalizeNullThrowsException()
+ {
+ $this->normalizer->denormalize(null, \DateTimeInterface::class);
+ }
+
+ /**
+ * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
+ * @expectedExceptionMessage The data is either an empty string or null, you should pass a string that can be parsed with the passed format or a valid DateTime string.
+ */
+ public function testDenormalizeEmptyStringThrowsException()
+ {
+ $this->normalizer->denormalize('', \DateTimeInterface::class);
+ }
+
/**
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
*/