|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\Serializer\Tests\Normalizer; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author Jérôme Parmentier <[email protected]> |
| 10 | + */ |
| 11 | +class DateIntervalNormalizerTest extends TestCase |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var DateIntervalNormalizer |
| 15 | + */ |
| 16 | + private $normalizer; |
| 17 | + |
| 18 | + protected function setUp() |
| 19 | + { |
| 20 | + $this->normalizer = new DateIntervalNormalizer(); |
| 21 | + } |
| 22 | + |
| 23 | + public function dataProviderISO() |
| 24 | + { |
| 25 | + $data = array( |
| 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'), |
| 32 | + ); |
| 33 | + |
| 34 | + return $data; |
| 35 | + } |
| 36 | + |
| 37 | + public function testSupportsNormalization() |
| 38 | + { |
| 39 | + $this->assertTrue($this->normalizer->supportsNormalization(new \DateInterval('P00Y00M00DT00H00M00S'))); |
| 40 | + $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); |
| 41 | + } |
| 42 | + |
| 43 | + public function testNormalize() |
| 44 | + { |
| 45 | + $this->assertEquals('P0Y0M0DT0H0M0S', $this->normalizer->normalize(new \DateInterval('PT0S'))); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @dataProvider dataProviderISO |
| 50 | + */ |
| 51 | + public function testNormalizeUsingFormatPassedInContext($format, $output, $input) |
| 52 | + { |
| 53 | + $this->assertEquals($output, $this->normalizer->normalize(new \DateInterval($input), null, array(DateIntervalNormalizer::FORMAT_KEY => $format))); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @dataProvider dataProviderISO |
| 58 | + */ |
| 59 | + public function testNormalizeUsingFormatPassedInConstructor($format, $output, $input) |
| 60 | + { |
| 61 | + $this->assertEquals($output, (new DateIntervalNormalizer($format))->normalize(new \DateInterval($input))); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException |
| 66 | + * @expectedExceptionMessage The object must be an instance of "\DateInterval". |
| 67 | + */ |
| 68 | + public function testNormalizeInvalidObjectThrowsException() |
| 69 | + { |
| 70 | + $this->normalizer->normalize(new \stdClass()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testSupportsDenormalization() |
| 74 | + { |
| 75 | + $this->assertTrue($this->normalizer->supportsDenormalization('P00Y00M00DT00H00M00S', \DateInterval::class)); |
| 76 | + $this->assertFalse($this->normalizer->supportsDenormalization('foo', 'Bar')); |
| 77 | + } |
| 78 | + |
| 79 | + public function testDenormalize() |
| 80 | + { |
| 81 | + $this->assertDateIntervalEquals(new \DateInterval('P00Y00M00DT00H00M00S'), $this->normalizer->denormalize('P00Y00M00DT00H00M00S', \DateInterval::class)); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @dataProvider dataProviderISO |
| 86 | + */ |
| 87 | + public function testDenormalizeUsingFormatPassedInContext($format, $input, $output) |
| 88 | + { |
| 89 | + $this->assertDateIntervalEquals(new \DateInterval($output), $this->normalizer->denormalize($input, \DateInterval::class, null, array(DateIntervalNormalizer::FORMAT_KEY => $format))); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @dataProvider dataProviderISO |
| 94 | + */ |
| 95 | + public function testDenormalizeUsingFormatPassedInConstructor($format, $input, $output) |
| 96 | + { |
| 97 | + $this->assertDateIntervalEquals(new \DateInterval($output), (new DateIntervalNormalizer($format))->denormalize($input, \DateInterval::class)); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException |
| 102 | + */ |
| 103 | + public function testDenormalizeExpectsString() |
| 104 | + { |
| 105 | + $this->normalizer->denormalize(1234, \DateInterval::class); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException |
| 110 | + * @expectedExceptionMessage Expected a valid ISO 8601 interval string. |
| 111 | + */ |
| 112 | + public function testDenormalizeNonISO8601IntervalStringThrowsException() |
| 113 | + { |
| 114 | + $this->normalizer->denormalize('10 years 2 months 3 days', \DateInterval::class, null); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException |
| 119 | + */ |
| 120 | + public function testDenormalizeInvalidDataThrowsException() |
| 121 | + { |
| 122 | + $this->normalizer->denormalize('invalid interval', \DateInterval::class); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException |
| 127 | + */ |
| 128 | + public function testDenormalizeFormatMismatchThrowsException() |
| 129 | + { |
| 130 | + $this->normalizer->denormalize('P00Y00M00DT00H00M00S', \DateInterval::class, null, array(DateIntervalNormalizer::FORMAT_KEY => 'P%yY%mM%dD')); |
| 131 | + } |
| 132 | + |
| 133 | + private function assertDateIntervalEquals(\DateInterval $expected, \DateInterval $actual) |
| 134 | + { |
| 135 | + $this->assertEquals($expected->format('%RP%yY%mM%dDT%hH%iM%sS'), $actual->format('%RP%yY%mM%dDT%hH%iM%sS')); |
| 136 | + } |
| 137 | +} |
0 commit comments