TimeFieldTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\TimeField;
  6. class TimeFieldTest extends DateTimeTestCase
  7. {
  8. public function testBind_dateTime()
  9. {
  10. $field = new TimeField('name', array(
  11. 'data_timezone' => 'UTC',
  12. 'user_timezone' => 'UTC',
  13. 'type' => TimeField::DATETIME,
  14. ));
  15. $input = array(
  16. 'hour' => '3',
  17. 'minute' => '4',
  18. );
  19. $field->bind($input);
  20. $dateTime = new \DateTime('1970-01-01 03:04:00 UTC');
  21. $this->assertEquals($dateTime, $field->getData());
  22. $this->assertEquals($input, $field->getDisplayedData());
  23. }
  24. public function testBind_string()
  25. {
  26. $field = new TimeField('name', array(
  27. 'data_timezone' => 'UTC',
  28. 'user_timezone' => 'UTC',
  29. 'type' => TimeField::STRING,
  30. ));
  31. $input = array(
  32. 'hour' => '3',
  33. 'minute' => '4',
  34. );
  35. $field->bind($input);
  36. $this->assertEquals('03:04:00', $field->getData());
  37. $this->assertEquals($input, $field->getDisplayedData());
  38. }
  39. public function testBind_timestamp()
  40. {
  41. $field = new TimeField('name', array(
  42. 'data_timezone' => 'UTC',
  43. 'user_timezone' => 'UTC',
  44. 'type' => TimeField::TIMESTAMP,
  45. ));
  46. $input = array(
  47. 'hour' => '3',
  48. 'minute' => '4',
  49. );
  50. $field->bind($input);
  51. $dateTime = new \DateTime('1970-01-01 03:04:00 UTC');
  52. $this->assertEquals($dateTime->format('U'), $field->getData());
  53. $this->assertEquals($input, $field->getDisplayedData());
  54. }
  55. public function testBind_raw()
  56. {
  57. $field = new TimeField('name', array(
  58. 'data_timezone' => 'UTC',
  59. 'user_timezone' => 'UTC',
  60. 'type' => TimeField::RAW,
  61. ));
  62. $input = array(
  63. 'hour' => '3',
  64. 'minute' => '4',
  65. );
  66. $data = array(
  67. 'hour' => '3',
  68. 'minute' => '4',
  69. 'second' => '0',
  70. );
  71. $field->bind($input);
  72. $this->assertEquals($data, $field->getData());
  73. $this->assertEquals($input, $field->getDisplayedData());
  74. }
  75. public function testSetData_withSeconds()
  76. {
  77. $field = new TimeField('name', array(
  78. 'data_timezone' => 'UTC',
  79. 'user_timezone' => 'UTC',
  80. 'type' => TimeField::DATETIME,
  81. 'with_seconds' => true,
  82. ));
  83. $field->setData(new \DateTime('03:04:05 UTC'));
  84. $this->assertEquals(array('hour' => 3, 'minute' => 4, 'second' => 5), $field->getDisplayedData());
  85. }
  86. public function testSetData_differentTimezones()
  87. {
  88. $field = new TimeField('name', array(
  89. 'data_timezone' => 'America/New_York',
  90. 'user_timezone' => 'Pacific/Tahiti',
  91. // don't do this test with DateTime, because it leads to wrong results!
  92. 'type' => TimeField::STRING,
  93. 'with_seconds' => true,
  94. ));
  95. $dateTime = new \DateTime('03:04:05 America/New_York');
  96. $field->setData($dateTime->format('H:i:s'));
  97. $dateTime = clone $dateTime;
  98. $dateTime->setTimezone(new \DateTimeZone('Pacific/Tahiti'));
  99. $displayedData = array(
  100. 'hour' => (int)$dateTime->format('H'),
  101. 'minute' => (int)$dateTime->format('i'),
  102. 'second' => (int)$dateTime->format('s')
  103. );
  104. $this->assertEquals($displayedData, $field->getDisplayedData());
  105. }
  106. public function testRenderAsInputs()
  107. {
  108. $field = new TimeField('name', array(
  109. 'widget' => TimeField::INPUT,
  110. 'data_timezone' => 'UTC',
  111. 'user_timezone' => 'UTC',
  112. ));
  113. $field->setData(new \DateTime('04:05 UTC'));
  114. $html = <<<EOF
  115. <input id="name_hour" name="name[hour]" value="04" type="text" maxlength="2" size="1" class="foobar" />
  116. :
  117. <input id="name_minute" name="name[minute]" value="05" type="text" maxlength="2" size="1" class="foobar" />
  118. EOF;
  119. $this->assertEquals(str_replace("\n", '', $html), $field->render(array('class' => 'foobar')));
  120. }
  121. public function testRenderAsInputs_withSeconds()
  122. {
  123. $field = new TimeField('name', array(
  124. 'widget' => TimeField::INPUT,
  125. 'data_timezone' => 'UTC',
  126. 'user_timezone' => 'UTC',
  127. 'with_seconds' => true,
  128. ));
  129. $field->setData(new \DateTime('04:05:06 UTC'));
  130. $html = <<<EOF
  131. <input id="name_hour" name="name[hour]" value="04" type="text" maxlength="2" size="1" class="foobar" />
  132. :
  133. <input id="name_minute" name="name[minute]" value="05" type="text" maxlength="2" size="1" class="foobar" />
  134. :
  135. <input id="name_second" name="name[second]" value="06" type="text" maxlength="2" size="1" class="foobar" />
  136. EOF;
  137. $this->assertEquals(str_replace("\n", '', $html), $field->render(array('class' => 'foobar')));
  138. }
  139. public function testRenderAsChoices()
  140. {
  141. $field = new TimeField('name', array(
  142. 'hours' => array(3, 4),
  143. 'minutes' => array(5, 6),
  144. 'widget' => TimeField::CHOICE,
  145. 'data_timezone' => 'UTC',
  146. 'user_timezone' => 'UTC',
  147. ));
  148. $field->setData(new \DateTime('04:05 UTC'));
  149. $html = <<<EOF
  150. <select id="name_hour" name="name[hour]" class="foobar">
  151. <option value="3">03</option>
  152. <option value="4" selected="selected">04</option>
  153. </select>:<select id="name_minute" name="name[minute]" class="foobar">
  154. <option value="5" selected="selected">05</option>
  155. <option value="6">06</option>
  156. </select>
  157. EOF;
  158. $this->assertEquals($html, $field->render(array('class' => 'foobar')));
  159. }
  160. public function testRenderAsChoices_withSeconds()
  161. {
  162. $field = new TimeField('name', array(
  163. 'hours' => array(3, 4),
  164. 'minutes' => array(5, 6),
  165. 'seconds' => array(7, 8),
  166. 'widget' => TimeField::CHOICE,
  167. 'data_timezone' => 'UTC',
  168. 'user_timezone' => 'UTC',
  169. 'with_seconds' => true,
  170. ));
  171. $field->setData(new \DateTime('04:05:07 UTC'));
  172. $html = <<<EOF
  173. <select id="name_hour" name="name[hour]" class="foobar">
  174. <option value="3">03</option>
  175. <option value="4" selected="selected">04</option>
  176. </select>:<select id="name_minute" name="name[minute]" class="foobar">
  177. <option value="5" selected="selected">05</option>
  178. <option value="6">06</option>
  179. </select>:<select id="name_second" name="name[second]" class="foobar">
  180. <option value="7" selected="selected">07</option>
  181. <option value="8">08</option>
  182. </select>
  183. EOF;
  184. $this->assertEquals($html, $field->render(array('class' => 'foobar')));
  185. }
  186. public function testRenderAsChoices_nonRequired()
  187. {
  188. $field = new TimeField('name', array(
  189. 'hours' => array(3, 4),
  190. 'minutes' => array(5, 6),
  191. 'widget' => TimeField::CHOICE,
  192. 'data_timezone' => 'UTC',
  193. 'user_timezone' => 'UTC',
  194. ));
  195. $field->setRequired(false);
  196. $field->setData(new \DateTime('04:05 UTC'));
  197. $html = <<<EOF
  198. <select id="name_hour" name="name[hour]" class="foobar">
  199. <option value=""></option>
  200. <option value="3">03</option>
  201. <option value="4" selected="selected">04</option>
  202. </select>:<select id="name_minute" name="name[minute]" class="foobar">
  203. <option value=""></option>
  204. <option value="5" selected="selected">05</option>
  205. <option value="6">06</option>
  206. </select>
  207. EOF;
  208. $this->assertEquals($html, $field->render(array('class' => 'foobar')));
  209. }
  210. }