DateTimeFieldTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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\Tests\Component\Form;
  11. require_once __DIR__ . '/DateTimeTestCase.php';
  12. use Symfony\Component\Form\DateTimeField;
  13. use Symfony\Component\Form\DateField;
  14. use Symfony\Component\Form\TimeField;
  15. class DateTimeFieldTest extends DateTimeTestCase
  16. {
  17. public function testSubmit_dateTime()
  18. {
  19. $field = new DateTimeField('name', array(
  20. 'data_timezone' => 'UTC',
  21. 'user_timezone' => 'UTC',
  22. 'date_widget' => DateField::CHOICE,
  23. 'time_widget' => TimeField::CHOICE,
  24. 'type' => DateTimeField::DATETIME,
  25. ));
  26. $field->submit(array(
  27. 'date' => array(
  28. 'day' => '2',
  29. 'month' => '6',
  30. 'year' => '2010',
  31. ),
  32. 'time' => array(
  33. 'hour' => '3',
  34. 'minute' => '4',
  35. ),
  36. ));
  37. $dateTime = new \DateTime('2010-06-02 03:04:00 UTC');
  38. $this->assertDateTimeEquals($dateTime, $field->getData());
  39. }
  40. public function testSubmit_string()
  41. {
  42. $field = new DateTimeField('name', array(
  43. 'data_timezone' => 'UTC',
  44. 'user_timezone' => 'UTC',
  45. 'type' => DateTimeField::STRING,
  46. 'date_widget' => DateField::CHOICE,
  47. 'time_widget' => TimeField::CHOICE,
  48. ));
  49. $field->submit(array(
  50. 'date' => array(
  51. 'day' => '2',
  52. 'month' => '6',
  53. 'year' => '2010',
  54. ),
  55. 'time' => array(
  56. 'hour' => '3',
  57. 'minute' => '4',
  58. ),
  59. ));
  60. $this->assertEquals('2010-06-02 03:04:00', $field->getData());
  61. }
  62. public function testSubmit_timestamp()
  63. {
  64. $field = new DateTimeField('name', array(
  65. 'data_timezone' => 'UTC',
  66. 'user_timezone' => 'UTC',
  67. 'type' => DateTimeField::TIMESTAMP,
  68. 'date_widget' => DateField::CHOICE,
  69. 'time_widget' => TimeField::CHOICE,
  70. ));
  71. $field->submit(array(
  72. 'date' => array(
  73. 'day' => '2',
  74. 'month' => '6',
  75. 'year' => '2010',
  76. ),
  77. 'time' => array(
  78. 'hour' => '3',
  79. 'minute' => '4',
  80. ),
  81. ));
  82. $dateTime = new \DateTime('2010-06-02 03:04:00 UTC');
  83. $this->assertEquals($dateTime->format('U'), $field->getData());
  84. }
  85. public function testSubmit_withSeconds()
  86. {
  87. $field = new DateTimeField('name', array(
  88. 'data_timezone' => 'UTC',
  89. 'user_timezone' => 'UTC',
  90. 'date_widget' => DateField::CHOICE,
  91. 'time_widget' => TimeField::CHOICE,
  92. 'type' => DateTimeField::DATETIME,
  93. 'with_seconds' => true,
  94. ));
  95. $field->setData(new \DateTime('2010-06-02 03:04:05 UTC'));
  96. $input = array(
  97. 'date' => array(
  98. 'day' => '2',
  99. 'month' => '6',
  100. 'year' => '2010',
  101. ),
  102. 'time' => array(
  103. 'hour' => '3',
  104. 'minute' => '4',
  105. 'second' => '5',
  106. ),
  107. );
  108. $field->submit($input);
  109. $this->assertDateTimeEquals(new \DateTime('2010-06-02 03:04:05 UTC'), $field->getData());
  110. }
  111. public function testSubmit_differentTimezones()
  112. {
  113. $field = new DateTimeField('name', array(
  114. 'data_timezone' => 'America/New_York',
  115. 'user_timezone' => 'Pacific/Tahiti',
  116. 'date_widget' => DateField::CHOICE,
  117. 'time_widget' => TimeField::CHOICE,
  118. // don't do this test with DateTime, because it leads to wrong results!
  119. 'type' => DateTimeField::STRING,
  120. 'with_seconds' => true,
  121. ));
  122. $dateTime = new \DateTime('2010-06-02 03:04:05 Pacific/Tahiti');
  123. $field->submit(array(
  124. 'date' => array(
  125. 'day' => (int)$dateTime->format('d'),
  126. 'month' => (int)$dateTime->format('m'),
  127. 'year' => (int)$dateTime->format('Y'),
  128. ),
  129. 'time' => array(
  130. 'hour' => (int)$dateTime->format('H'),
  131. 'minute' => (int)$dateTime->format('i'),
  132. 'second' => (int)$dateTime->format('s'),
  133. ),
  134. ));
  135. $dateTime->setTimezone(new \DateTimeZone('America/New_York'));
  136. $this->assertEquals($dateTime->format('Y-m-d H:i:s'), $field->getData());
  137. }
  138. }