TimeFieldTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. require_once __DIR__ . '/DateTimeTestCase.php';
  4. use Symfony\Component\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. }