DateTimeFieldTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Symfony\Tests\Components\Form;
  3. require_once __DIR__ . '/../../../../bootstrap.php';
  4. require_once __DIR__ . '/DateTimeTestCase.php';
  5. use Symfony\Components\Form\DateTimeField;
  6. use Symfony\Components\Form\DateField;
  7. use Symfony\Components\Form\TimeField;
  8. class DateTimeFieldTest extends DateTimeTestCase
  9. {
  10. public function testBind_dateTime()
  11. {
  12. $field = new DateTimeField('name', array(
  13. 'data_timezone' => 'UTC',
  14. 'user_timezone' => 'UTC',
  15. 'date_widget' => DateField::CHOICE,
  16. 'time_widget' => TimeField::CHOICE,
  17. 'type' => DateTimeField::DATETIME,
  18. ));
  19. $field->bind(array(
  20. 'date' => array(
  21. 'day' => '2',
  22. 'month' => '6',
  23. 'year' => '2010',
  24. ),
  25. 'time' => array(
  26. 'hour' => '3',
  27. 'minute' => '4',
  28. ),
  29. ));
  30. $dateTime = new \DateTime('2010-06-02 03:04:00 UTC');
  31. $this->assertDateTimeEquals($dateTime, $field->getData());
  32. }
  33. public function testBind_string()
  34. {
  35. $field = new DateTimeField('name', array(
  36. 'data_timezone' => 'UTC',
  37. 'user_timezone' => 'UTC',
  38. 'type' => DateTimeField::STRING,
  39. 'date_widget' => DateField::CHOICE,
  40. 'time_widget' => TimeField::CHOICE,
  41. ));
  42. $field->bind(array(
  43. 'date' => array(
  44. 'day' => '2',
  45. 'month' => '6',
  46. 'year' => '2010',
  47. ),
  48. 'time' => array(
  49. 'hour' => '3',
  50. 'minute' => '4',
  51. ),
  52. ));
  53. $this->assertEquals('2010-06-02 03:04:00', $field->getData());
  54. }
  55. public function testBind_timestamp()
  56. {
  57. $field = new DateTimeField('name', array(
  58. 'data_timezone' => 'UTC',
  59. 'user_timezone' => 'UTC',
  60. 'type' => DateTimeField::TIMESTAMP,
  61. 'date_widget' => DateField::CHOICE,
  62. 'time_widget' => TimeField::CHOICE,
  63. ));
  64. $field->bind(array(
  65. 'date' => array(
  66. 'day' => '2',
  67. 'month' => '6',
  68. 'year' => '2010',
  69. ),
  70. 'time' => array(
  71. 'hour' => '3',
  72. 'minute' => '4',
  73. ),
  74. ));
  75. $dateTime = new \DateTime('2010-06-02 03:04:00 UTC');
  76. $this->assertEquals($dateTime->format('U'), $field->getData());
  77. }
  78. public function testBind_withSeconds()
  79. {
  80. $field = new DateTimeField('name', array(
  81. 'data_timezone' => 'UTC',
  82. 'user_timezone' => 'UTC',
  83. 'date_widget' => DateField::CHOICE,
  84. 'time_widget' => TimeField::CHOICE,
  85. 'type' => DateTimeField::DATETIME,
  86. 'with_seconds' => true,
  87. ));
  88. $field->setData(new \DateTime('2010-06-02 03:04:05 UTC'));
  89. $input = array(
  90. 'date' => array(
  91. 'day' => '2',
  92. 'month' => '6',
  93. 'year' => '2010',
  94. ),
  95. 'time' => array(
  96. 'hour' => '3',
  97. 'minute' => '4',
  98. 'second' => '5',
  99. ),
  100. );
  101. $field->bind($input);
  102. $this->assertDateTimeEquals(new \DateTime('2010-06-02 03:04:05 UTC'), $field->getData());
  103. }
  104. public function testBind_differentTimezones()
  105. {
  106. $field = new DateTimeField('name', array(
  107. 'data_timezone' => 'America/New_York',
  108. 'user_timezone' => 'Pacific/Tahiti',
  109. 'date_widget' => DateField::CHOICE,
  110. 'time_widget' => TimeField::CHOICE,
  111. // don't do this test with DateTime, because it leads to wrong results!
  112. 'type' => DateTimeField::STRING,
  113. 'with_seconds' => true,
  114. ));
  115. $dateTime = new \DateTime('2010-06-02 03:04:05 Pacific/Tahiti');
  116. $field->bind(array(
  117. 'date' => array(
  118. 'day' => (int)$dateTime->format('d'),
  119. 'month' => (int)$dateTime->format('m'),
  120. 'year' => (int)$dateTime->format('Y'),
  121. ),
  122. 'time' => array(
  123. 'hour' => (int)$dateTime->format('H'),
  124. 'minute' => (int)$dateTime->format('i'),
  125. 'second' => (int)$dateTime->format('s'),
  126. ),
  127. ));
  128. $dateTime->setTimezone(new \DateTimeZone('America/New_York'));
  129. $this->assertEquals($dateTime->format('Y-m-d H:i:s'), $field->getData());
  130. }
  131. public function testRender()
  132. {
  133. $field = new DateTimeField('name', array(
  134. 'years' => array(2010, 2011),
  135. 'months' => array(6, 7),
  136. 'days' => array(1, 2),
  137. 'hours' => array(3, 4),
  138. 'minutes' => array(5, 6),
  139. 'seconds' => array(7, 8),
  140. 'data_timezone' => 'UTC',
  141. 'user_timezone' => 'UTC',
  142. 'date_widget' => DateField::CHOICE,
  143. 'time_widget' => TimeField::CHOICE,
  144. 'type' => DateTimeField::DATETIME,
  145. 'with_seconds' => true,
  146. ));
  147. $field->setData(new \DateTime('2010-06-02 03:04:05 UTC'));
  148. $html = <<<EOF
  149. <select id="name_date_day" name="name[date][day]" class="foobar">
  150. <option value="1">01</option>
  151. <option value="2" selected="selected">02</option>
  152. </select>.<select id="name_date_month" name="name[date][month]" class="foobar">
  153. <option value="6" selected="selected">06</option>
  154. <option value="7">07</option>
  155. </select>.<select id="name_date_year" name="name[date][year]" class="foobar">
  156. <option value="2010" selected="selected">2010</option>
  157. <option value="2011">2011</option>
  158. </select>
  159. <select id="name_time_hour" name="name[time][hour]" class="foobar">
  160. <option value="3" selected="selected">03</option>
  161. <option value="4">04</option>
  162. </select>:<select id="name_time_minute" name="name[time][minute]" class="foobar">
  163. <option value="5">05</option>
  164. <option value="6">06</option>
  165. </select>:<select id="name_time_second" name="name[time][second]" class="foobar">
  166. <option value="7">07</option>
  167. <option value="8">08</option>
  168. </select>
  169. EOF;
  170. $this->assertEquals($html, $field->render(array('class' => 'foobar')));
  171. }
  172. }