* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Symfony\Component\Form\Tests\Extension\Core\DataTransformer; use Symfony\Component\Form\Extension\Core\DataTransformer\DateIntervalToArrayTransformer; use Symfony\Component\Form\Exception\TransformationFailedException; use Symfony\Component\Form\Exception\UnexpectedTypeException; /** * @author Steffen Roßkamp */ class DateIntervalToArrayTransformerTest extends DateIntervalTestCase { public function testTransform() { $transformer = new DateIntervalToArrayTransformer(); $input = new \DateInterval('P1Y2M3DT4H5M6S'); $output = array( 'years' => '1', 'months' => '2', 'days' => '3', 'hours' => '4', 'minutes' => '5', 'seconds' => '6', 'invert' => false, ); $this->assertSame($output, $transformer->transform($input)); } public function testTransformEmpty() { $transformer = new DateIntervalToArrayTransformer(); $output = array( 'years' => '', 'months' => '', 'days' => '', 'hours' => '', 'minutes' => '', 'seconds' => '', 'invert' => false, ); $this->assertSame($output, $transformer->transform(null)); } public function testTransformEmptyWithFields() { $transformer = new DateIntervalToArrayTransformer(array('years', 'weeks', 'minutes', 'seconds')); $output = array( 'years' => '', 'weeks' => '', 'minutes' => '', 'seconds' => '', ); $this->assertSame($output, $transformer->transform(null)); } public function testTransformWithFields() { $transformer = new DateIntervalToArrayTransformer(array('years', 'minutes', 'seconds')); $input = new \DateInterval('P1Y2M3DT4H5M6S'); $output = array( 'years' => '1', 'minutes' => '5', 'seconds' => '6', ); $this->assertSame($output, $transformer->transform($input)); } public function testTransformWithWeek() { $transformer = new DateIntervalToArrayTransformer(array('weeks', 'minutes', 'seconds')); $input = new \DateInterval('P1Y2M3WT4H5M6S'); $output = array( 'weeks' => '3', 'minutes' => '5', 'seconds' => '6', ); $input = $transformer->transform($input); ksort($input); ksort($output); $this->assertSame($output, $input); } public function testTransformDaysToWeeks() { $transformer = new DateIntervalToArrayTransformer(array('weeks', 'minutes', 'seconds')); $input = new \DateInterval('P1Y2M23DT4H5M6S'); $output = array( 'weeks' => '3', 'minutes' => '5', 'seconds' => '6', ); $input = $transformer->transform($input); ksort($input); ksort($output); $this->assertSame($output, $input); } public function testTransformDaysNotOverflowingToWeeks() { $transformer = new DateIntervalToArrayTransformer(array('days', 'minutes', 'seconds')); $input = new \DateInterval('P1Y2M23DT4H5M6S'); $output = array( 'days' => '23', 'minutes' => '5', 'seconds' => '6', ); $this->assertSame($output, $transformer->transform($input)); } public function testTransformWithInvert() { $transformer = new DateIntervalToArrayTransformer(array('years', 'invert')); $input = new \DateInterval('P1Y'); $input->invert = 1; $output = array( 'years' => '1', 'invert' => true, ); $this->assertSame($output, $transformer->transform($input)); } public function testTransformWithPadding() { $transformer = new DateIntervalToArrayTransformer(null, true); $input = new \DateInterval('P1Y2M3DT4H5M6S'); $output = array( 'years' => '01', 'months' => '02', 'days' => '03', 'hours' => '04', 'minutes' => '05', 'seconds' => '06', 'invert' => false, ); $this->assertSame($output, $transformer->transform($input)); } public function testTransformWithFieldsAndPadding() { $transformer = new DateIntervalToArrayTransformer(array('years', 'minutes', 'seconds'), true); $input = new \DateInterval('P1Y2M3DT4H5M6S'); $output = array( 'years' => '01', 'minutes' => '05', 'seconds' => '06', ); $this->assertSame($output, $transformer->transform($input)); } public function testReverseTransformRequiresDateTime() { $transformer = new DateIntervalToArrayTransformer(); $this->assertNull($transformer->reverseTransform(null)); $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(UnexpectedTypeException::class); $transformer->reverseTransform('12345'); } public function testReverseTransformWithUnsetFields() { $transformer = new DateIntervalToArrayTransformer(); $input = array('years' => '1'); $this->{method_exists($this, $_ = 'expectException') ? $_ : 'setExpectedException'}(TransformationFailedException::class); $transformer->reverseTransform($input); } public function testReverseTransformWithEmptyFields() { $transformer = new DateIntervalToArrayTransformer(array('years', 'minutes', 'seconds')); $input = array( 'years' => '1', 'minutes' => '', 'seconds' => '6', ); if (method_exists($this, 'expectException')) { $this->expectException(TransformationFailedException::class); $this->expectExceptionMessage('This amount of "minutes" is invalid'); } else { $this->setExpectedException(TransformationFailedException::class, 'This amount of "minutes" is invalid'); } $transformer->reverseTransform($input); } public function testReverseTransformWithWrongInvertType() { $transformer = new DateIntervalToArrayTransformer(array('invert')); $input = array( 'invert' => '1', ); if (method_exists($this, 'expectException')) { $this->expectException(TransformationFailedException::class); $this->expectExceptionMessage('The value of "invert" must be boolean'); } else { $this->setExpectedException(TransformationFailedException::class, 'The value of "invert" must be boolean'); } $transformer->reverseTransform($input); } public function testReverseTransform() { $transformer = new DateIntervalToArrayTransformer(); $input = array( 'years' => '1', 'months' => '2', 'days' => '3', 'hours' => '4', 'minutes' => '5', 'seconds' => '6', 'invert' => false, ); $output = new \DateInterval('P01Y02M03DT04H05M06S'); $this->assertDateIntervalEquals($output, $transformer->reverseTransform($input)); } public function testReverseTransformWithWeek() { $transformer = new DateIntervalToArrayTransformer( array('years', 'months', 'weeks', 'hours', 'minutes', 'seconds') ); $input = array( 'years' => '1', 'months' => '2', 'weeks' => '3', 'hours' => '4', 'minutes' => '5', 'seconds' => '6', ); $output = new \DateInterval('P1Y2M21DT4H5M6S'); $this->assertDateIntervalEquals($output, $transformer->reverseTransform($input)); } public function testReverseTransformWithFields() { $transformer = new DateIntervalToArrayTransformer(array('years', 'minutes', 'seconds')); $input = array( 'years' => '1', 'minutes' => '5', 'seconds' => '6', ); $output = new \DateInterval('P1Y0M0DT0H5M6S'); $this->assertDateIntervalEquals($output, $transformer->reverseTransform($input)); } public function testBothTransformsWithWeek() { $transformer = new DateIntervalToArrayTransformer( array('years', 'months', 'weeks', 'hours', 'minutes', 'seconds') ); $interval = new \DateInterval('P1Y2M21DT4H5M6S'); $array = array( 'years' => '1', 'months' => '2', 'weeks' => '3', 'hours' => '4', 'minutes' => '5', 'seconds' => '6', ); $input = $transformer->transform($interval); ksort($input); ksort($array); $this->assertSame($array, $input); $interval = new \DateInterval('P1Y2M0DT4H5M6S'); $input['weeks'] = '0'; $this->assertDateIntervalEquals($interval, $transformer->reverseTransform($input)); } } __halt_compiler();----SIGNATURE:----X94jzmu1TMFQis5fY3yQ4NO5QVvoMftMwpndL+xmoOsQdJT7KMF/n7fqCMo3QIR32geBqXvKt7Tu7u1YKRHdSH9YPP+1AzxXD/mKh0RxjW+edHni9cFVuSbzRj0c0s7GsnHNgR/2YI6lsZM2smwVeG4l3GzNB6/9+Seo3HAX9npFw+T7srPP2E9NQUBoEYJJ1nK0eZ+uag/yM/y98QV2Ayam5QNy28MQiPRRJ0sSaPaBgvfEPLnBqBGhaonthu/rL1Nsf8xbEJ8mfncnRvP/jIKBKAUw9h9Oo9shjCxYJlSc2vjtx4dPgYb2UN0///DD9RIiMcehuZKvoc0VUWsbhZoYn3uxr0cZrxzmWmhXAuMcbrZYkL/DH1L65I93gzwUticWHLKdkC/ghbU5UI4YlxeO+vgZMFuWVWM607aKnFGtmfwOciDA2q/SiRWHxzBqneHnbBfZeedtPDVtlVzpBkPaiza/d6wheh6KmvGYjOrLz5lvifUir+vH1G1jKmjsaFO6Ux/nPKKscWBLw5v58f3LHKTarZ4XJ7LAlVmGA12c2qEqqGvJr6FmPSMKWmt4P+2/A3MLIJZIvxdWzJCkUiECo1VLOMradY9+s7hjij2Gmh/OBlEht8stdxFI40Yw+A1QOHDREzB2H55Q1gOPfTRkXW+SML20/I7fbe7egIo=----ATTACHMENT:----MTcwMDcxMTAxNTQxMjA0MSA1NDg5MTI1MDgwMTMzMTU1IDMwOTcxMTIyNTg2MjI4MTg=