|
12 | 12 | namespace Symfony\Component\VarDumper\Tests\Caster;
|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
| 15 | +use Symfony\Component\VarDumper\Caster\Caster; |
15 | 16 | use Symfony\Component\VarDumper\Caster\DateCaster;
|
16 | 17 | use Symfony\Component\VarDumper\Cloner\Stub;
|
17 | 18 | use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
|
@@ -84,4 +85,103 @@ public function provideDateTimes()
|
84 | 85 | array('2017-04-30 00:00:00.000000', '+02:00', '2017-04-30 00:00:00.000000 +02:00'),
|
85 | 86 | );
|
86 | 87 | }
|
| 88 | + |
| 89 | + /** |
| 90 | + * @dataProvider provideIntervals |
| 91 | + */ |
| 92 | + public function testDumpInterval($intervalSpec, $invert, $expected) |
| 93 | + { |
| 94 | + $interval = new \DateInterval($intervalSpec); |
| 95 | + $interval->invert = $invert; |
| 96 | + |
| 97 | + $xDump = <<<EODUMP |
| 98 | +DateInterval { |
| 99 | + interval: $expected |
| 100 | +%a |
| 101 | +} |
| 102 | +EODUMP; |
| 103 | + |
| 104 | + $this->assertDumpMatchesFormat($xDump, $interval); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @dataProvider provideIntervals |
| 109 | + */ |
| 110 | + public function testDumpIntervalExcludingVerbosity($intervalSpec, $invert, $expected) |
| 111 | + { |
| 112 | + $interval = new \DateInterval($intervalSpec); |
| 113 | + $interval->invert = $invert; |
| 114 | + |
| 115 | + $xDump = <<<EODUMP |
| 116 | +DateInterval { |
| 117 | + interval: $expected |
| 118 | +} |
| 119 | +EODUMP; |
| 120 | + |
| 121 | + $this->assertDumpMatchesFormat($xDump, $interval, Caster::EXCLUDE_VERBOSE); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @dataProvider provideIntervals |
| 126 | + */ |
| 127 | + public function testCastInterval($intervalSpec, $invert, $xInterval, $xSeconds) |
| 128 | + { |
| 129 | + $interval = new \DateInterval($intervalSpec); |
| 130 | + $interval->invert = $invert; |
| 131 | + $stub = new Stub(); |
| 132 | + |
| 133 | + $cast = DateCaster::castInterval($interval, array('foo' => 'bar'), $stub, false, Caster::EXCLUDE_VERBOSE); |
| 134 | + |
| 135 | + $xDump = <<<EODUMP |
| 136 | +array:1 [ |
| 137 | + "\\x00~\\x00interval" => $xInterval |
| 138 | +] |
| 139 | +EODUMP; |
| 140 | + |
| 141 | + $this->assertDumpMatchesFormat($xDump, $cast); |
| 142 | + |
| 143 | + if (null === $xSeconds) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + $xDump = <<<EODUMP |
| 148 | +Symfony\Component\VarDumper\Caster\ConstStub { |
| 149 | + +type: "ref" |
| 150 | + +class: "$xInterval" |
| 151 | + +value: "$xSeconds" |
| 152 | + +cut: 0 |
| 153 | + +handle: 0 |
| 154 | + +refCount: 0 |
| 155 | + +position: 0 |
| 156 | + +attr: [] |
| 157 | +} |
| 158 | +EODUMP; |
| 159 | + |
| 160 | + $this->assertDumpMatchesFormat($xDump, $cast["\0~\0interval"]); |
| 161 | + } |
| 162 | + |
| 163 | + public function provideIntervals() |
| 164 | + { |
| 165 | + $ms = \PHP_VERSION_ID >= 70100 ? '.000000' : ''; |
| 166 | + |
| 167 | + return array( |
| 168 | + array('PT0S', 0, '0s', '0s'), |
| 169 | + array('PT1S', 0, '+ 00:00:01'.$ms, '1s'), |
| 170 | + array('PT2M', 0, '+ 00:02:00'.$ms, '120s'), |
| 171 | + array('PT3H', 0, '+ 03:00:00'.$ms, '10 800s'), |
| 172 | + array('P4D', 0, '+ 4d', '345 600s'), |
| 173 | + array('P5M', 0, '+ 5m', null), |
| 174 | + array('P6Y', 0, '+ 6y', null), |
| 175 | + array('P1Y2M3DT4H5M6S', 0, '+ 1y 2m 3d 04:05:06'.$ms, null), |
| 176 | + |
| 177 | + array('PT0S', 1, '0s', '0s'), |
| 178 | + array('PT1S', 1, '- 00:00:01'.$ms, '-1s'), |
| 179 | + array('PT2M', 1, '- 00:02:00'.$ms, '-120s'), |
| 180 | + array('PT3H', 1, '- 03:00:00'.$ms, '-10 800s'), |
| 181 | + array('P4D', 1, '- 4d', '-345 600s'), |
| 182 | + array('P5M', 1, '- 5m', null), |
| 183 | + array('P6Y', 1, '- 6y', null), |
| 184 | + array('P1Y2M3DT4H5M6S', 1, '- 1y 2m 3d 04:05:06'.$ms, null), |
| 185 | + ); |
| 186 | + } |
87 | 187 | }
|
0 commit comments