DateFieldTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. require_once __DIR__ . '/DateTimeTestCase.php';
  4. use Symfony\Component\Form\DateField;
  5. use Symfony\Component\Form\FormConfiguration;
  6. class DateFieldTest extends DateTimeTestCase
  7. {
  8. protected function setUp()
  9. {
  10. FormConfiguration::setDefaultLocale('de_AT');
  11. }
  12. public function testBind_fromInput_dateTime()
  13. {
  14. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::DATETIME));
  15. $field->bind('2.6.2010');
  16. $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $field->getData());
  17. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  18. }
  19. public function testBind_fromInput_string()
  20. {
  21. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::STRING));
  22. $field->bind('2.6.2010');
  23. $this->assertEquals('2010-06-02', $field->getData());
  24. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  25. }
  26. public function testBind_fromInput_timestamp()
  27. {
  28. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::TIMESTAMP));
  29. $field->bind('2.6.2010');
  30. $dateTime = new \DateTime('2010-06-02 UTC');
  31. $this->assertEquals($dateTime->format('U'), $field->getData());
  32. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  33. }
  34. public function testBind_fromInput_raw()
  35. {
  36. $field = new DateField('name', array(
  37. 'data_timezone' => 'UTC',
  38. 'user_timezone' => 'UTC',
  39. 'widget' => 'input',
  40. 'type' => DateField::RAW,
  41. ));
  42. $field->bind('2.6.2010');
  43. $output = array(
  44. 'day' => '2',
  45. 'month' => '6',
  46. 'year' => '2010',
  47. );
  48. $this->assertEquals($output, $field->getData());
  49. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  50. }
  51. public function testBind_fromChoice()
  52. {
  53. $field = new DateField('name', array('widget' => DateField::CHOICE));
  54. $input = array(
  55. 'day' => '2',
  56. 'month' => '6',
  57. 'year' => '2010',
  58. );
  59. $field->bind($input);
  60. $dateTime = new \DateTime('2010-06-02 UTC');
  61. $this->assertDateTimeEquals($dateTime, $field->getData());
  62. $this->assertEquals($input, $field->getDisplayedData());
  63. }
  64. public function testBind_fromChoice_empty()
  65. {
  66. $field = new DateField('name', array('widget' => DateField::CHOICE, 'required' => false));
  67. $input = array(
  68. 'day' => '',
  69. 'month' => '',
  70. 'year' => '',
  71. );
  72. $field->bind($input);
  73. $this->assertSame(null, $field->getData());
  74. $this->assertEquals($input, $field->getDisplayedData());
  75. }
  76. public function testSetData_differentTimezones()
  77. {
  78. $field = new DateField('name', array(
  79. 'data_timezone' => 'America/New_York',
  80. 'user_timezone' => 'Pacific/Tahiti',
  81. // don't do this test with DateTime, because it leads to wrong results!
  82. 'type' => DateField::STRING,
  83. 'widget' => 'input',
  84. ));
  85. $field->setData('2010-06-02');
  86. $this->assertEquals('01.06.2010', $field->getDisplayedData());
  87. }
  88. public function testIsYearWithinRange_returnsTrueIfWithin()
  89. {
  90. $field = new DateField('name', array(
  91. 'widget' => 'input',
  92. 'years' => array(2010, 2011),
  93. ));
  94. $field->bind('2.6.2010');
  95. $this->assertTrue($field->isYearWithinRange());
  96. }
  97. public function testIsYearWithinRange_returnsTrueIfEmpty()
  98. {
  99. $field = new DateField('name', array(
  100. 'widget' => 'input',
  101. 'years' => array(2010, 2011),
  102. ));
  103. $field->bind('');
  104. $this->assertTrue($field->isYearWithinRange());
  105. }
  106. public function testIsYearWithinRange_returnsFalseIfNotContained()
  107. {
  108. $field = new DateField('name', array(
  109. 'widget' => 'input',
  110. 'years' => array(2010, 2012),
  111. ));
  112. $field->bind('2.6.2011');
  113. $this->assertFalse($field->isYearWithinRange());
  114. }
  115. public function testIsMonthWithinRange_returnsTrueIfWithin()
  116. {
  117. $field = new DateField('name', array(
  118. 'widget' => 'input',
  119. 'months' => array(6, 7),
  120. ));
  121. $field->bind('2.6.2010');
  122. $this->assertTrue($field->isMonthWithinRange());
  123. }
  124. public function testIsMonthWithinRange_returnsTrueIfEmpty()
  125. {
  126. $field = new DateField('name', array(
  127. 'widget' => 'input',
  128. 'months' => array(6, 7),
  129. ));
  130. $field->bind('');
  131. $this->assertTrue($field->isMonthWithinRange());
  132. }
  133. public function testIsMonthWithinRange_returnsFalseIfNotContained()
  134. {
  135. $field = new DateField('name', array(
  136. 'widget' => 'input',
  137. 'months' => array(6, 8),
  138. ));
  139. $field->bind('2.7.2010');
  140. $this->assertFalse($field->isMonthWithinRange());
  141. }
  142. public function testIsDayWithinRange_returnsTrueIfWithin()
  143. {
  144. $field = new DateField('name', array(
  145. 'widget' => 'input',
  146. 'days' => array(6, 7),
  147. ));
  148. $field->bind('6.6.2010');
  149. $this->assertTrue($field->isDayWithinRange());
  150. }
  151. public function testIsDayWithinRange_returnsTrueIfEmpty()
  152. {
  153. $field = new DateField('name', array(
  154. 'widget' => 'input',
  155. 'days' => array(6, 7),
  156. ));
  157. $field->bind('');
  158. $this->assertTrue($field->isDayWithinRange());
  159. }
  160. public function testIsDayWithinRange_returnsFalseIfNotContained()
  161. {
  162. $field = new DateField('name', array(
  163. 'widget' => 'input',
  164. 'days' => array(6, 8),
  165. ));
  166. $field->bind('7.6.2010');
  167. $this->assertFalse($field->isDayWithinRange());
  168. }
  169. }