DateFieldTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Symfony\Tests\Component\Form;
  3. require_once __DIR__ . '/DateTimeTestCase.php';
  4. use Symfony\Component\Form\DateField;
  5. class DateFieldTest extends DateTimeTestCase
  6. {
  7. public function testBind_fromInput_dateTime()
  8. {
  9. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::DATETIME));
  10. $field->setLocale('de_AT');
  11. $field->bind('2.6.2010');
  12. $this->assertDateTimeEquals(new \DateTime('2010-06-02 UTC'), $field->getData());
  13. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  14. }
  15. public function testBind_fromInput_string()
  16. {
  17. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::STRING));
  18. $field->setLocale('de_AT');
  19. $field->bind('2.6.2010');
  20. $this->assertEquals('2010-06-02', $field->getData());
  21. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  22. }
  23. public function testBind_fromInput_timestamp()
  24. {
  25. $field = new DateField('name', array('widget' => 'input', 'type' => DateField::TIMESTAMP));
  26. $field->setLocale('de_AT');
  27. $field->bind('2.6.2010');
  28. $dateTime = new \DateTime('2010-06-02 UTC');
  29. $this->assertEquals($dateTime->format('U'), $field->getData());
  30. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  31. }
  32. public function testBind_fromInput_raw()
  33. {
  34. $field = new DateField('name', array(
  35. 'data_timezone' => 'UTC',
  36. 'user_timezone' => 'UTC',
  37. 'widget' => 'input',
  38. 'type' => DateField::RAW,
  39. ));
  40. $field->setLocale('de_AT');
  41. $field->bind('2.6.2010');
  42. $output = array(
  43. 'day' => '2',
  44. 'month' => '6',
  45. 'year' => '2010',
  46. );
  47. $this->assertEquals($output, $field->getData());
  48. $this->assertEquals('02.06.2010', $field->getDisplayedData());
  49. }
  50. public function testBind_fromChoice()
  51. {
  52. $field = new DateField('name', array('widget' => DateField::CHOICE));
  53. $input = array(
  54. 'day' => '2',
  55. 'month' => '6',
  56. 'year' => '2010',
  57. );
  58. $field->setLocale('de_AT');
  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 testSetData_differentTimezones()
  65. {
  66. $field = new DateField('name', array(
  67. 'data_timezone' => 'America/New_York',
  68. 'user_timezone' => 'Pacific/Tahiti',
  69. // don't do this test with DateTime, because it leads to wrong results!
  70. 'type' => DateField::STRING,
  71. 'widget' => 'input',
  72. ));
  73. $field->setLocale('de_AT');
  74. $field->setData('2010-06-02');
  75. $this->assertEquals('01.06.2010', $field->getDisplayedData());
  76. }
  77. }