/vendor/symfony/symfony/src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/DateTimeToStringTransformerTest.php

https://github.com/nattaphat/hgis · PHP · 181 lines · 108 code · 44 blank · 29 comment · 2 complexity · 56355370050bb1c3cd6cf6cf68da089d MD5 · raw file

  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\Form\Tests\Extension\Core\DataTransformer;
  11. use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToStringTransformer;
  12. class DateTimeToStringTransformerTest extends DateTimeTestCase
  13. {
  14. public function dataProvider()
  15. {
  16. $data = array(
  17. array('Y-m-d H:i:s', '2010-02-03 16:05:06', '2010-02-03 16:05:06 UTC'),
  18. array('Y-m-d H:i:00', '2010-02-03 16:05:00', '2010-02-03 16:05:00 UTC'),
  19. array('Y-m-d H:i', '2010-02-03 16:05', '2010-02-03 16:05:00 UTC'),
  20. array('Y-m-d H', '2010-02-03 16', '2010-02-03 16:00:00 UTC'),
  21. array('Y-m-d', '2010-02-03', '2010-02-03 00:00:00 UTC'),
  22. array('Y-m', '2010-12', '2010-12-01 00:00:00 UTC'),
  23. array('Y', '2010', '2010-01-01 00:00:00 UTC'),
  24. array('d-m-Y', '03-02-2010', '2010-02-03 00:00:00 UTC'),
  25. array('H:i:s', '16:05:06', '1970-01-01 16:05:06 UTC'),
  26. array('H:i:00', '16:05:00', '1970-01-01 16:05:00 UTC'),
  27. array('H:i', '16:05', '1970-01-01 16:05:00 UTC'),
  28. array('H', '16', '1970-01-01 16:00:00 UTC'),
  29. // different day representations
  30. array('Y-m-j', '2010-02-3', '2010-02-03 00:00:00 UTC'),
  31. array('z', '33', '1970-02-03 00:00:00 UTC'),
  32. // not bijective
  33. // this will not work as php will use actual date to replace missing info
  34. // and after change of date will lookup for closest Wednesday
  35. // i.e. value: 2010-02, php value: 2010-02-(today i.e. 20), parsed date: 2010-02-24
  36. //array('Y-m-D', '2010-02-Wed', '2010-02-03 00:00:00 UTC'),
  37. //array('Y-m-l', '2010-02-Wednesday', '2010-02-03 00:00:00 UTC'),
  38. // different month representations
  39. array('Y-n-d', '2010-2-03', '2010-02-03 00:00:00 UTC'),
  40. array('Y-M-d', '2010-Feb-03', '2010-02-03 00:00:00 UTC'),
  41. array('Y-F-d', '2010-February-03', '2010-02-03 00:00:00 UTC'),
  42. // different year representations
  43. array('y-m-d', '10-02-03', '2010-02-03 00:00:00 UTC'),
  44. // different time representations
  45. array('G:i:s', '16:05:06', '1970-01-01 16:05:06 UTC'),
  46. array('g:i:s a', '4:05:06 pm', '1970-01-01 16:05:06 UTC'),
  47. array('h:i:s a', '04:05:06 pm', '1970-01-01 16:05:06 UTC'),
  48. // seconds since unix
  49. array('U', '1265213106', '2010-02-03 16:05:06 UTC'),
  50. );
  51. // This test will fail < 5.3.9 - see https://bugs.php.net/51994
  52. if (version_compare(phpversion(), '5.3.9', '>=')) {
  53. $data[] = array('Y-z', '2010-33', '2010-02-03 00:00:00 UTC');
  54. }
  55. return $data;
  56. }
  57. /**
  58. * @dataProvider dataProvider
  59. */
  60. public function testTransform($format, $output, $input)
  61. {
  62. $transformer = new DateTimeToStringTransformer('UTC', 'UTC', $format);
  63. $input = new \DateTime($input);
  64. $this->assertEquals($output, $transformer->transform($input));
  65. }
  66. public function testTransformEmpty()
  67. {
  68. $transformer = new DateTimeToStringTransformer();
  69. $this->assertSame('', $transformer->transform(null));
  70. }
  71. public function testTransformWithDifferentTimezones()
  72. {
  73. $transformer = new DateTimeToStringTransformer('Asia/Hong_Kong', 'America/New_York', 'Y-m-d H:i:s');
  74. $input = new \DateTime('2010-02-03 12:05:06 America/New_York');
  75. $output = $input->format('Y-m-d H:i:s');
  76. $input->setTimezone(new \DateTimeZone('Asia/Hong_Kong'));
  77. $this->assertEquals($output, $transformer->transform($input));
  78. }
  79. public function testTransformExpectsDateTime()
  80. {
  81. $transformer = new DateTimeToStringTransformer();
  82. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  83. $transformer->transform('1234');
  84. }
  85. /**
  86. * @dataProvider dataProvider
  87. */
  88. public function testReverseTransformUsingPipe($format, $input, $output)
  89. {
  90. if (version_compare(phpversion(), '5.3.7', '<')) {
  91. $this->markTestSkipped('Pipe usage requires PHP 5.3.7 or newer.');
  92. }
  93. $reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, true);
  94. $output = new \DateTime($output);
  95. $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
  96. }
  97. /**
  98. * @dataProvider dataProvider
  99. */
  100. public function testReverseTransformWithoutUsingPipe($format, $input, $output)
  101. {
  102. $reverseTransformer = new DateTimeToStringTransformer('UTC', 'UTC', $format, false);
  103. $output = new \DateTime($output);
  104. $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
  105. }
  106. public function testReverseTransformEmpty()
  107. {
  108. $reverseTransformer = new DateTimeToStringTransformer();
  109. $this->assertNull($reverseTransformer->reverseTransform(''));
  110. }
  111. public function testReverseTransformWithDifferentTimezones()
  112. {
  113. $reverseTransformer = new DateTimeToStringTransformer('America/New_York', 'Asia/Hong_Kong', 'Y-m-d H:i:s');
  114. $output = new \DateTime('2010-02-03 16:05:06 Asia/Hong_Kong');
  115. $input = $output->format('Y-m-d H:i:s');
  116. $output->setTimeZone(new \DateTimeZone('America/New_York'));
  117. $this->assertDateTimeEquals($output, $reverseTransformer->reverseTransform($input));
  118. }
  119. public function testReverseTransformExpectsString()
  120. {
  121. $reverseTransformer = new DateTimeToStringTransformer();
  122. $this->setExpectedException('Symfony\Component\Form\Exception\UnexpectedTypeException');
  123. $reverseTransformer->reverseTransform(1234);
  124. }
  125. public function testReverseTransformExpectsValidDateString()
  126. {
  127. $reverseTransformer = new DateTimeToStringTransformer();
  128. $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
  129. $reverseTransformer->reverseTransform('2010-2010-2010');
  130. }
  131. public function testReverseTransformWithNonExistingDate()
  132. {
  133. $reverseTransformer = new DateTimeToStringTransformer();
  134. $this->setExpectedException('Symfony\Component\Form\Exception\TransformationFailedException');
  135. $reverseTransformer->reverseTransform('2010-04-31');
  136. }
  137. }