12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Serializer\Tests\Normalizer;
- use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
- /**
- * @author Kévin Dunglas <dunglas@gmail.com>
- */
- class DateTimeNormalizerTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var DateTimeNormalizer
- */
- private $normalizer;
- protected function setUp()
- {
- $this->normalizer = new DateTimeNormalizer();
- }
- public function testSupportNormalization()
- {
- $this->assertTrue($this->normalizer->supportsNormalization(new \DateTime()));
- $this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
- $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
- }
- public function testNormalize()
- {
- $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
- $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC'))));
- }
- public function testContextFormat()
- {
- $this->assertEquals('2016', $this->normalizer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y')));
- }
- public function testConstructorFormat()
- {
- $this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
- }
- /**
- * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
- * @expectedExceptionMessage The object must implement the "\DateTimeInterface".
- */
- public function testInvalidDataThrowException()
- {
- $this->normalizer->normalize(new \stdClass());
- }
- public function testSupportDenormalization()
- {
- $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
- $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTime::class));
- $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
- $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar'));
- }
- public function testDenormalize()
- {
- $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
- $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
- $this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
- }
- /**
- * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
- */
- public function testInvalidDateThrowException()
- {
- $this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
- }
- }
|