3 namespace Symfony\Component\Serializer\Tests\Normalizer;
5 use PHPUnit\Framework\TestCase;
6 use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer;
9 * @author Jérôme Parmentier <jerome@prmntr.me>
11 class DateIntervalNormalizerTest extends TestCase
14 * @var DateIntervalNormalizer
18 protected function setUp()
20 $this->normalizer = new DateIntervalNormalizer();
23 public function dataProviderISO()
26 array('P%YY%MM%DDT%HH%IM%SS', 'P00Y00M00DT00H00M00S', 'PT0S'),
27 array('P%yY%mM%dDT%hH%iM%sS', 'P0Y0M0DT0H0M0S', 'PT0S'),
28 array('P%yY%mM%dDT%hH%iM%sS', 'P10Y2M3DT16H5M6S', 'P10Y2M3DT16H5M6S'),
29 array('P%yY%mM%dDT%hH%iM', 'P10Y2M3DT16H5M', 'P10Y2M3DT16H5M'),
30 array('P%yY%mM%dDT%hH', 'P10Y2M3DT16H', 'P10Y2M3DT16H'),
31 array('P%yY%mM%dD', 'P10Y2M3D', 'P10Y2M3DT0H'),
37 public function testSupportsNormalization()
39 $this->assertTrue($this->normalizer->supportsNormalization(new \DateInterval('P00Y00M00DT00H00M00S')));
40 $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
43 public function testNormalize()
45 $this->assertEquals('P0Y0M0DT0H0M0S', $this->normalizer->normalize(new \DateInterval('PT0S')));
49 * @dataProvider dataProviderISO
51 public function testNormalizeUsingFormatPassedInContext($format, $output, $input)
53 $this->assertEquals($output, $this->normalizer->normalize(new \DateInterval($input), null, array(DateIntervalNormalizer::FORMAT_KEY => $format)));
57 * @dataProvider dataProviderISO
59 public function testNormalizeUsingFormatPassedInConstructor($format, $output, $input)
61 $this->assertEquals($output, (new DateIntervalNormalizer($format))->normalize(new \DateInterval($input)));
65 * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
66 * @expectedExceptionMessage The object must be an instance of "\DateInterval".
68 public function testNormalizeInvalidObjectThrowsException()
70 $this->normalizer->normalize(new \stdClass());
73 public function testSupportsDenormalization()
75 $this->assertTrue($this->normalizer->supportsDenormalization('P00Y00M00DT00H00M00S', \DateInterval::class));
76 $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar'));
79 public function testDenormalize()
81 $this->assertDateIntervalEquals(new \DateInterval('P00Y00M00DT00H00M00S'), $this->normalizer->denormalize('P00Y00M00DT00H00M00S', \DateInterval::class));
85 * @dataProvider dataProviderISO
87 public function testDenormalizeUsingFormatPassedInContext($format, $input, $output)
89 $this->assertDateIntervalEquals(new \DateInterval($output), $this->normalizer->denormalize($input, \DateInterval::class, null, array(DateIntervalNormalizer::FORMAT_KEY => $format)));
93 * @dataProvider dataProviderISO
95 public function testDenormalizeUsingFormatPassedInConstructor($format, $input, $output)
97 $this->assertDateIntervalEquals(new \DateInterval($output), (new DateIntervalNormalizer($format))->denormalize($input, \DateInterval::class));
101 * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
103 public function testDenormalizeExpectsString()
105 $this->normalizer->denormalize(1234, \DateInterval::class);
109 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
110 * @expectedExceptionMessage Expected a valid ISO 8601 interval string.
112 public function testDenormalizeNonISO8601IntervalStringThrowsException()
114 $this->normalizer->denormalize('10 years 2 months 3 days', \DateInterval::class, null);
118 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
120 public function testDenormalizeInvalidDataThrowsException()
122 $this->normalizer->denormalize('invalid interval', \DateInterval::class);
126 * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException
128 public function testDenormalizeFormatMismatchThrowsException()
130 $this->normalizer->denormalize('P00Y00M00DT00H00M00S', \DateInterval::class, null, array(DateIntervalNormalizer::FORMAT_KEY => 'P%yY%mM%dD'));
133 private function assertDateIntervalEquals(\DateInterval $expected, \DateInterval $actual)
135 $this->assertEquals($expected->format('%RP%yY%mM%dDT%hH%iM%sS'), $actual->format('%RP%yY%mM%dDT%hH%iM%sS'));