TimeFieldTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 = $this->factory->getTimeField('name', array(
  18. 'data_timezone' => 'UTC',
  19. 'user_timezone' => 'UTC',
  20. 'type' => '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 = $this->factory->getTimeField('name', array(
  34. 'data_timezone' => 'UTC',
  35. 'user_timezone' => 'UTC',
  36. 'type' => '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 = $this->factory->getTimeField('name', array(
  49. 'data_timezone' => 'UTC',
  50. 'user_timezone' => 'UTC',
  51. 'type' => '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_array()
  63. {
  64. $field = $this->factory->getTimeField('name', array(
  65. 'data_timezone' => 'UTC',
  66. 'user_timezone' => 'UTC',
  67. 'type' => 'array',
  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 = $this->factory->getTimeField('name', array(
  84. 'data_timezone' => 'UTC',
  85. 'user_timezone' => 'UTC',
  86. 'type' => '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 = $this->factory->getTimeField('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' => '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. $this->markTestSkipped('Needs to be reimplemented using validators');
  115. $field = $this->factory->getTimeField('name', array(
  116. 'hours' => array(6, 7),
  117. ));
  118. $field->submit(array('hour' => '06', 'minute' => '12'));
  119. $this->assertTrue($field->isHourWithinRange());
  120. }
  121. public function testIsHourWithinRange_returnsTrueIfEmpty()
  122. {
  123. $this->markTestSkipped('Needs to be reimplemented using validators');
  124. $field = $this->factory->getTimeField('name', array(
  125. 'hours' => array(6, 7),
  126. ));
  127. $field->submit(array('hour' => '', 'minute' => '06'));
  128. $this->assertTrue($field->isHourWithinRange());
  129. }
  130. public function testIsHourWithinRange_returnsFalseIfNotContained()
  131. {
  132. $this->markTestSkipped('Needs to be reimplemented using validators');
  133. $field = $this->factory->getTimeField('name', array(
  134. 'hours' => array(6, 7),
  135. ));
  136. $field->submit(array('hour' => '08', 'minute' => '12'));
  137. $this->assertFalse($field->isHourWithinRange());
  138. }
  139. public function testIsMinuteWithinRange_returnsTrueIfWithin()
  140. {
  141. $this->markTestSkipped('Needs to be reimplemented using validators');
  142. $field = $this->factory->getTimeField('name', array(
  143. 'minutes' => array(6, 7),
  144. ));
  145. $field->submit(array('hour' => '06', 'minute' => '06'));
  146. $this->assertTrue($field->isMinuteWithinRange());
  147. }
  148. public function testIsMinuteWithinRange_returnsTrueIfEmpty()
  149. {
  150. $this->markTestSkipped('Needs to be reimplemented using validators');
  151. $field = $this->factory->getTimeField('name', array(
  152. 'minutes' => array(6, 7),
  153. ));
  154. $field->submit(array('hour' => '06', 'minute' => ''));
  155. $this->assertTrue($field->isMinuteWithinRange());
  156. }
  157. public function testIsMinuteWithinRange_returnsFalseIfNotContained()
  158. {
  159. $this->markTestSkipped('Needs to be reimplemented using validators');
  160. $field = $this->factory->getTimeField('name', array(
  161. 'minutes' => array(6, 7),
  162. ));
  163. $field->submit(array('hour' => '06', 'minute' => '08'));
  164. $this->assertFalse($field->isMinuteWithinRange());
  165. }
  166. public function testIsSecondWithinRange_returnsTrueIfWithin()
  167. {
  168. $this->markTestSkipped('Needs to be reimplemented using validators');
  169. $field = $this->factory->getTimeField('name', array(
  170. 'seconds' => array(6, 7),
  171. 'with_seconds' => true,
  172. ));
  173. $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '06'));
  174. $this->assertTrue($field->isSecondWithinRange());
  175. }
  176. public function testIsSecondWithinRange_returnsTrueIfEmpty()
  177. {
  178. $this->markTestSkipped('Needs to be reimplemented using validators');
  179. $field = $this->factory->getTimeField('name', array(
  180. 'seconds' => array(6, 7),
  181. 'with_seconds' => true,
  182. ));
  183. $field->submit(array('hour' => '06', 'minute' => '06', 'second' => ''));
  184. $this->assertTrue($field->isSecondWithinRange());
  185. }
  186. public function testIsSecondWithinRange_returnsTrueIfNotWithSeconds()
  187. {
  188. $this->markTestSkipped('Needs to be reimplemented using validators');
  189. $field = $this->factory->getTimeField('name', array(
  190. 'seconds' => array(6, 7),
  191. ));
  192. $field->submit(array('hour' => '06', 'minute' => '06'));
  193. $this->assertTrue($field->isSecondWithinRange());
  194. }
  195. public function testIsSecondWithinRange_returnsFalseIfNotContained()
  196. {
  197. $this->markTestSkipped('Needs to be reimplemented using validators');
  198. $field = $this->factory->getTimeField('name', array(
  199. 'seconds' => array(6, 7),
  200. 'with_seconds' => true,
  201. ));
  202. $field->submit(array('hour' => '04', 'minute' => '05', 'second' => '08'));
  203. $this->assertFalse($field->isSecondWithinRange());
  204. }
  205. public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty()
  206. {
  207. $this->markTestSkipped('Needs to be reimplemented using validators');
  208. $field = $this->factory->getTimeField('name', array(
  209. 'widget' => 'choice',
  210. ));
  211. $field->submit(array(
  212. 'hour' => '',
  213. 'minute' => '',
  214. ));
  215. $this->assertFalse($field->isPartiallyFilled());
  216. }
  217. public function testIsPartiallyFilled_returnsFalseIfCompletelyEmpty_withSeconds()
  218. {
  219. $this->markTestSkipped('Needs to be reimplemented using validators');
  220. $field = $this->factory->getTimeField('name', array(
  221. 'widget' => 'choice',
  222. 'with_seconds' => true,
  223. ));
  224. $field->submit(array(
  225. 'hour' => '',
  226. 'minute' => '',
  227. 'second' => '',
  228. ));
  229. $this->assertFalse($field->isPartiallyFilled());
  230. }
  231. public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled()
  232. {
  233. $this->markTestSkipped('Needs to be reimplemented using validators');
  234. $field = $this->factory->getTimeField('name', array(
  235. 'widget' => 'choice',
  236. ));
  237. $field->submit(array(
  238. 'hour' => '0',
  239. 'minute' => '0',
  240. ));
  241. $this->assertFalse($field->isPartiallyFilled());
  242. }
  243. public function testIsPartiallyFilled_returnsFalseIfCompletelyFilled_withSeconds()
  244. {
  245. $this->markTestSkipped('Needs to be reimplemented using validators');
  246. $field = $this->factory->getTimeField('name', array(
  247. 'widget' => 'choice',
  248. 'with_seconds' => true,
  249. ));
  250. $field->submit(array(
  251. 'hour' => '0',
  252. 'minute' => '0',
  253. 'second' => '0',
  254. ));
  255. $this->assertFalse($field->isPartiallyFilled());
  256. }
  257. public function testIsPartiallyFilled_returnsTrueIfChoiceAndHourEmpty()
  258. {
  259. $this->markTestSkipped('Needs to be reimplemented using validators');
  260. $field = $this->factory->getTimeField('name', array(
  261. 'widget' => 'choice',
  262. 'with_seconds' => true,
  263. ));
  264. $field->submit(array(
  265. 'hour' => '',
  266. 'minute' => '0',
  267. 'second' => '0',
  268. ));
  269. $this->assertTrue($field->isPartiallyFilled());
  270. }
  271. public function testIsPartiallyFilled_returnsTrueIfChoiceAndMinuteEmpty()
  272. {
  273. $this->markTestSkipped('Needs to be reimplemented using validators');
  274. $field = $this->factory->getTimeField('name', array(
  275. 'widget' => 'choice',
  276. 'with_seconds' => true,
  277. ));
  278. $field->submit(array(
  279. 'hour' => '0',
  280. 'minute' => '',
  281. 'second' => '0',
  282. ));
  283. $this->assertTrue($field->isPartiallyFilled());
  284. }
  285. public function testIsPartiallyFilled_returnsTrueIfChoiceAndSecondsEmpty()
  286. {
  287. $this->markTestSkipped('Needs to be reimplemented using validators');
  288. $field = $this->factory->getTimeField('name', array(
  289. 'widget' => 'choice',
  290. 'with_seconds' => true,
  291. ));
  292. $field->submit(array(
  293. 'hour' => '0',
  294. 'minute' => '0',
  295. 'second' => '',
  296. ));
  297. $this->assertTrue($field->isPartiallyFilled());
  298. }
  299. }