TimeFieldTest.php 7.6 KB

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