TimeFieldTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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' => '06'));
  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' => '06', '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' => '06', 'minute' => '06', 'second' => ''));
  176. $this->assertTrue($field->isSecondWithinRange());
  177. }
  178. public function testIsSecondWithinRange_returnsTrueIfNotWithSeconds()
  179. {
  180. $field = new TimeField('name', array(
  181. 'seconds' => array(6, 7),
  182. ));
  183. $field->submit(array('hour' => '06', 'minute' => '06'));
  184. $this->assertTrue($field->isSecondWithinRange());
  185. }
  186. public function testIsSecondWithinRange_returnsFalseIfNotContained()
  187. {
  188. $field = new TimeField('name', array(
  189. 'seconds' => array(6, 7),
  190. 'with_seconds' => true,
  191. ));
  192. $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '08'));
  193. $this->assertFalse($field->isSecondWithinRange());
  194. }
  195. public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty()
  196. {
  197. $field = new TimeField('name', array(
  198. 'widget' => 'choice',
  199. ));
  200. $field->submit(array(
  201. 'hour' => '',
  202. 'minute' => '',
  203. ));
  204. $this->assertFalse($field->isPartiallyFilled());
  205. }
  206. public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty_withSeconds()
  207. {
  208. $field = new TimeField('name', array(
  209. 'widget' => 'choice',
  210. 'with_seconds' => true,
  211. ));
  212. $field->submit(array(
  213. 'hour' => '',
  214. 'minute' => '',
  215. 'second' => '',
  216. ));
  217. $this->assertFalse($field->isPartiallyFilled());
  218. }
  219. public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled()
  220. {
  221. $field = new TimeField('name', array(
  222. 'widget' => 'choice',
  223. ));
  224. $field->submit(array(
  225. 'hour' => '0',
  226. 'minute' => '0',
  227. ));
  228. $this->assertFalse($field->isPartiallyFilled());
  229. }
  230. public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled_withSeconds()
  231. {
  232. $field = new TimeField('name', array(
  233. 'widget' => 'choice',
  234. 'with_seconds' => true,
  235. ));
  236. $field->submit(array(
  237. 'hour' => '0',
  238. 'minute' => '0',
  239. 'second' => '0',
  240. ));
  241. $this->assertFalse($field->isPartiallyFilled());
  242. }
  243. public function testIsPartiallyFilled_returnsTrueIfChoiceAndHourEmpty()
  244. {
  245. $field = new TimeField('name', array(
  246. 'widget' => 'choice',
  247. 'with_seconds' => true,
  248. ));
  249. $field->submit(array(
  250. 'hour' => '',
  251. 'minute' => '0',
  252. 'second' => '0',
  253. ));
  254. $this->assertTrue($field->isPartiallyFilled());
  255. }
  256. public function testIsPartiallyFilled_returnsTrueIfChoiceAndMinuteEmpty()
  257. {
  258. $field = new TimeField('name', array(
  259. 'widget' => 'choice',
  260. 'with_seconds' => true,
  261. ));
  262. $field->submit(array(
  263. 'hour' => '0',
  264. 'minute' => '',
  265. 'second' => '0',
  266. ));
  267. $this->assertTrue($field->isPartiallyFilled());
  268. }
  269. public function testIsPartiallyFilled_returnsTrueIfChoiceAndSecondsEmpty()
  270. {
  271. $field = new TimeField('name', array(
  272. 'widget' => 'choice',
  273. 'with_seconds' => true,
  274. ));
  275. $field->submit(array(
  276. 'hour' => '0',
  277. 'minute' => '0',
  278. 'second' => '',
  279. ));
  280. $this->assertTrue($field->isPartiallyFilled());
  281. }
  282. }