2016-01-17 12:10:27 +00:00
|
|
|
<?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;
|
|
|
|
|
2017-02-20 13:34:33 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-01-17 12:10:27 +00:00
|
|
|
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Kévin Dunglas <dunglas@gmail.com>
|
|
|
|
*/
|
2017-02-20 13:34:33 +00:00
|
|
|
class DateTimeNormalizerTest extends TestCase
|
2016-01-17 12:10:27 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var DateTimeNormalizer
|
|
|
|
*/
|
|
|
|
private $normalizer;
|
|
|
|
|
2016-01-27 12:43:54 +00:00
|
|
|
protected function setUp()
|
2016-01-17 12:10:27 +00:00
|
|
|
{
|
|
|
|
$this->normalizer = new DateTimeNormalizer();
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:04:25 +01:00
|
|
|
public function testSupportsNormalization()
|
2016-01-17 12:10:27 +00:00
|
|
|
{
|
|
|
|
$this->assertTrue($this->normalizer->supportsNormalization(new \DateTime()));
|
|
|
|
$this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
|
|
|
|
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNormalize()
|
|
|
|
{
|
2016-02-04 14:04:32 +00:00
|
|
|
$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'))));
|
2016-01-17 12:10:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 09:04:25 +01:00
|
|
|
public function testNormalizeUsingFormatPassedInContext()
|
2016-01-17 12:10:27 +00:00
|
|
|
{
|
|
|
|
$this->assertEquals('2016', $this->normalizer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y')));
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:04:25 +01:00
|
|
|
public function testNormalizeUsingFormatPassedInConstructor()
|
2016-01-17 12:10:27 +00:00
|
|
|
{
|
2016-02-04 14:04:32 +00:00
|
|
|
$this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
|
2016-01-17 12:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage The object must implement the "\DateTimeInterface".
|
|
|
|
*/
|
2016-10-14 09:04:25 +01:00
|
|
|
public function testNormalizeInvalidObjectThrowsException()
|
2016-01-17 12:10:27 +00:00
|
|
|
{
|
|
|
|
$this->normalizer->normalize(new \stdClass());
|
|
|
|
}
|
|
|
|
|
2016-10-14 09:04:25 +01:00
|
|
|
public function testSupportsDenormalization()
|
2016-01-17 12:10:27 +00:00
|
|
|
{
|
|
|
|
$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()
|
|
|
|
{
|
2016-02-04 14:04:32 +00:00
|
|
|
$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));
|
2016-01-17 12:10:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-14 09:04:25 +01:00
|
|
|
public function testDenormalizeUsingFormatPassedInContext()
|
|
|
|
{
|
2016-11-18 21:58:27 +00:00
|
|
|
$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 \DateTimeImmutable('2016/01/01'), $this->normalizer->denormalize('2016.01.01', \DateTimeImmutable::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|')));
|
2016-10-14 09:04:25 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 12:10:27 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
|
|
|
*/
|
2016-10-14 09:04:25 +01:00
|
|
|
public function testDenormalizeInvalidDataThrowsException()
|
2016-01-17 12:10:27 +00:00
|
|
|
{
|
|
|
|
$this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
|
|
|
|
}
|
2016-10-14 09:04:25 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
|
|
|
*/
|
|
|
|
public function testDenormalizeFormatMismatchThrowsException()
|
|
|
|
{
|
|
|
|
$this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class, null, array(DateTimeNormalizer::FORMAT_KEY => 'Y-m-d|'));
|
|
|
|
}
|
2016-01-17 12:10:27 +00:00
|
|
|
}
|