DateFieldTest.php 6.1 KB

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