DateTimeNormalizerTest.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Serializer\Tests\Normalizer;
  11. use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
  12. /**
  13. * @author Kévin Dunglas <dunglas@gmail.com>
  14. */
  15. class DateTimeNormalizerTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var DateTimeNormalizer
  19. */
  20. private $normalizer;
  21. protected function setUp()
  22. {
  23. $this->normalizer = new DateTimeNormalizer();
  24. }
  25. public function testSupportNormalization()
  26. {
  27. $this->assertTrue($this->normalizer->supportsNormalization(new \DateTime()));
  28. $this->assertTrue($this->normalizer->supportsNormalization(new \DateTimeImmutable()));
  29. $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
  30. }
  31. public function testNormalize()
  32. {
  33. $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
  34. $this->assertEquals('2016-01-01T00:00:00+00:00', $this->normalizer->normalize(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC'))));
  35. }
  36. public function testContextFormat()
  37. {
  38. $this->assertEquals('2016', $this->normalizer->normalize(new \DateTime('2016/01/01'), null, array(DateTimeNormalizer::FORMAT_KEY => 'Y')));
  39. }
  40. public function testConstructorFormat()
  41. {
  42. $this->assertEquals('16', (new DateTimeNormalizer('y'))->normalize(new \DateTime('2016/01/01', new \DateTimeZone('UTC'))));
  43. }
  44. /**
  45. * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
  46. * @expectedExceptionMessage The object must implement the "\DateTimeInterface".
  47. */
  48. public function testInvalidDataThrowException()
  49. {
  50. $this->normalizer->normalize(new \stdClass());
  51. }
  52. public function testSupportDenormalization()
  53. {
  54. $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
  55. $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTime::class));
  56. $this->assertTrue($this->normalizer->supportsDenormalization('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
  57. $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar'));
  58. }
  59. public function testDenormalize()
  60. {
  61. $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeInterface::class));
  62. $this->assertEquals(new \DateTimeImmutable('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTimeImmutable::class));
  63. $this->assertEquals(new \DateTime('2016/01/01', new \DateTimeZone('UTC')), $this->normalizer->denormalize('2016-01-01T00:00:00+00:00', \DateTime::class));
  64. }
  65. /**
  66. * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
  67. */
  68. public function testInvalidDateThrowException()
  69. {
  70. $this->normalizer->denormalize('invalid date', \DateTimeInterface::class);
  71. }
  72. }