TimeFieldTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\TimeField;
  13. class TimeFieldTest extends DateTimeTestCase
  14. {
  15. public function testSubmit_dateTime()
  16. {
  17. $field = new TimeField('name', array(
  18. 'data_timezone' => 'UTC',
  19. 'user_timezone' => 'UTC',
  20. 'type' => TimeField::DATETIME,
  21. ));
  22. $input = array(
  23. 'hour' => '3',
  24. 'minute' => '4',
  25. );
  26. $field->submit($input);
  27. $dateTime = new \DateTime('1970-01-01 03:04:00 UTC');
  28. $this->assertEquals($dateTime, $field->getData());
  29. $this->assertEquals($input, $field->getDisplayedData());
  30. }
  31. public function testSubmit_string()
  32. {
  33. $field = new TimeField('name', array(
  34. 'data_timezone' => 'UTC',
  35. 'user_timezone' => 'UTC',
  36. 'type' => TimeField::STRING,
  37. ));
  38. $input = array(
  39. 'hour' => '3',
  40. 'minute' => '4',
  41. );
  42. $field->submit($input);
  43. $this->assertEquals('03:04:00', $field->getData());
  44. $this->assertEquals($input, $field->getDisplayedData());
  45. }
  46. public function testSubmit_timestamp()
  47. {
  48. $field = new TimeField('name', array(
  49. 'data_timezone' => 'UTC',
  50. 'user_timezone' => 'UTC',
  51. 'type' => TimeField::TIMESTAMP,
  52. ));
  53. $input = array(
  54. 'hour' => '3',
  55. 'minute' => '4',
  56. );
  57. $field->submit($input);
  58. $dateTime = new \DateTime('1970-01-01 03:04:00 UTC');
  59. $this->assertEquals($dateTime->format('U'), $field->getData());
  60. $this->assertEquals($input, $field->getDisplayedData());
  61. }
  62. public function testSubmit_raw()
  63. {
  64. $field = new TimeField('name', array(
  65. 'data_timezone' => 'UTC',
  66. 'user_timezone' => 'UTC',
  67. 'type' => TimeField::RAW,
  68. ));
  69. $input = array(
  70. 'hour' => '3',
  71. 'minute' => '4',
  72. );
  73. $data = array(
  74. 'hour' => '3',
  75. 'minute' => '4',
  76. );
  77. $field->submit($input);
  78. $this->assertEquals($data, $field->getData());
  79. $this->assertEquals($input, $field->getDisplayedData());
  80. }
  81. public function testSetData_withSeconds()
  82. {
  83. $field = new TimeField('name', array(
  84. 'data_timezone' => 'UTC',
  85. 'user_timezone' => 'UTC',
  86. 'type' => TimeField::DATETIME,
  87. 'with_seconds' => true,
  88. ));
  89. $field->setData(new \DateTime('03:04:05 UTC'));
  90. $this->assertEquals(array('hour' => 3, 'minute' => 4, 'second' => 5), $field->getDisplayedData());
  91. }
  92. public function testSetData_differentTimezones()
  93. {
  94. $field = new TimeField('name', array(
  95. 'data_timezone' => 'America/New_York',
  96. 'user_timezone' => 'Pacific/Tahiti',
  97. // don't do this test with DateTime, because it leads to wrong results!
  98. 'type' => TimeField::STRING,
  99. 'with_seconds' => true,
  100. ));
  101. $dateTime = new \DateTime('03:04:05 America/New_York');
  102. $field->setData($dateTime->format('H:i:s'));
  103. $dateTime = clone $dateTime;
  104. $dateTime->setTimezone(new \DateTimeZone('Pacific/Tahiti'));
  105. $displayedData = array(
  106. 'hour' => (int)$dateTime->format('H'),
  107. 'minute' => (int)$dateTime->format('i'),
  108. 'second' => (int)$dateTime->format('s')
  109. );
  110. $this->assertEquals($displayedData, $field->getDisplayedData());
  111. }
  112. public function testIsHourWithinRange_returnsTrueIfWithin()
  113. {
  114. $field = new TimeField('name', array(
  115. 'hours' => array(6, 7),
  116. ));
  117. $field->submit(array('hour' => '06', 'minute' => '12'));
  118. $this->assertTrue($field->isHourWithinRange());
  119. }
  120. public function testIsHourWithinRange_returnsTrueIfEmpty()
  121. {
  122. $field = new TimeField('name', array(
  123. 'hours' => array(6, 7),
  124. ));
  125. $field->submit(array('hour' => '', 'minute' => ''));
  126. $this->assertTrue($field->isHourWithinRange());
  127. }
  128. public function testIsHourWithinRange_returnsFalseIfNotContained()
  129. {
  130. $field = new TimeField('name', array(
  131. 'hours' => array(6, 7),
  132. ));
  133. $field->submit(array('hour' => '08', 'minute' => '12'));
  134. $this->assertFalse($field->isHourWithinRange());
  135. }
  136. public function testIsMinuteWithinRange_returnsTrueIfWithin()
  137. {
  138. $field = new TimeField('name', array(
  139. 'minutes' => array(6, 7),
  140. ));
  141. $field->submit(array('hour' => '06', 'minute' => '06'));
  142. $this->assertTrue($field->isMinuteWithinRange());
  143. }
  144. public function testIsMinuteWithinRange_returnsTrueIfEmpty()
  145. {
  146. $field = new TimeField('name', array(
  147. 'minutes' => array(6, 7),
  148. ));
  149. $field->submit(array('hour' => '', 'minute' => ''));
  150. $this->assertTrue($field->isMinuteWithinRange());
  151. }
  152. public function testIsMinuteWithinRange_returnsFalseIfNotContained()
  153. {
  154. $field = new TimeField('name', array(
  155. 'minutes' => array(6, 7),
  156. ));
  157. $field->submit(array('hour' => '06', 'minute' => '08'));
  158. $this->assertFalse($field->isMinuteWithinRange());
  159. }
  160. public function testIsSecondWithinRange_returnsTrueIfWithin()
  161. {
  162. $field = new TimeField('name', array(
  163. 'seconds' => array(6, 7),
  164. 'with_seconds' => true,
  165. ));
  166. $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '06'));
  167. $this->assertTrue($field->isSecondWithinRange());
  168. }
  169. public function testIsSecondWithinRange_returnsTrueIfEmpty()
  170. {
  171. $field = new TimeField('name', array(
  172. 'seconds' => array(6, 7),
  173. 'with_seconds' => true,
  174. ));
  175. $field->submit(array('hour' => '', 'minute' => ''));
  176. $this->assertTrue($field->isSecondWithinRange());
  177. }
  178. public function testIsSecondWithinRange_returnsFalseIfNotContained()
  179. {
  180. $field = new TimeField('name', array(
  181. 'seconds' => array(6, 7),
  182. 'with_seconds' => true,
  183. ));
  184. $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '08'));
  185. $this->assertFalse($field->isSecondWithinRange());
  186. }
  187. }