4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Tests\Extension\Core\Type
;
14 use Symfony\Component\Form\Extension\Core\View\ChoiceView
;
15 use Symfony\Component\Form\FormError
;
16 use Symfony\Component\Intl\Util\IntlTestHelper
;
18 class DateTypeTest
extends TypeTestCase
20 protected function setUp()
24 // we test against "de_AT", so we need the full implementation
25 IntlTestHelper
::requireFullIntl($this);
27 \Locale
::setDefault('de_AT');
31 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
33 public function testInvalidWidgetOption()
35 $this->factory
->create('date', null, array(
36 'widget' => 'fake_widget',
41 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
43 public function testInvalidInputOption()
45 $this->factory
->create('date', null, array(
46 'input' => 'fake_input',
50 public function testSubmitFromSingleTextDateTimeWithDefaultFormat()
52 $form = $this->factory
->create('date', null, array(
53 'model_timezone' => 'UTC',
54 'view_timezone' => 'UTC',
55 'widget' => 'single_text',
56 'input' => 'datetime',
59 $form->submit('2010-06-02');
61 $this->assertDateTimeEquals(new \
DateTime('2010-06-02 UTC'), $form->getData());
62 $this->assertEquals('2010-06-02', $form->getViewData());
65 public function testSubmitFromSingleTextDateTime()
67 $form = $this->factory
->create('date', null, array(
68 'format' => \IntlDateFormatter
::MEDIUM
,
69 'model_timezone' => 'UTC',
70 'view_timezone' => 'UTC',
71 'widget' => 'single_text',
72 'input' => 'datetime',
75 $form->submit('2.6.2010');
77 $this->assertDateTimeEquals(new \
DateTime('2010-06-02 UTC'), $form->getData());
78 $this->assertEquals('02.06.2010', $form->getViewData());
81 public function testSubmitFromSingleTextString()
83 $form = $this->factory
->create('date', null, array(
84 'format' => \IntlDateFormatter
::MEDIUM
,
85 'model_timezone' => 'UTC',
86 'view_timezone' => 'UTC',
87 'widget' => 'single_text',
91 $form->submit('2.6.2010');
93 $this->assertEquals('2010-06-02', $form->getData());
94 $this->assertEquals('02.06.2010', $form->getViewData());
97 public function testSubmitFromSingleTextTimestamp()
99 $form = $this->factory
->create('date', null, array(
100 'format' => \IntlDateFormatter
::MEDIUM
,
101 'model_timezone' => 'UTC',
102 'view_timezone' => 'UTC',
103 'widget' => 'single_text',
104 'input' => 'timestamp',
107 $form->submit('2.6.2010');
109 $dateTime = new \
DateTime('2010-06-02 UTC');
111 $this->assertEquals($dateTime->format('U'), $form->getData());
112 $this->assertEquals('02.06.2010', $form->getViewData());
115 public function testSubmitFromSingleTextRaw()
117 $form = $this->factory
->create('date', null, array(
118 'format' => \IntlDateFormatter
::MEDIUM
,
119 'model_timezone' => 'UTC',
120 'view_timezone' => 'UTC',
121 'widget' => 'single_text',
125 $form->submit('2.6.2010');
133 $this->assertEquals($output, $form->getData());
134 $this->assertEquals('02.06.2010', $form->getViewData());
137 public function testSubmitFromText()
139 $form = $this->factory
->create('date', null, array(
140 'model_timezone' => 'UTC',
141 'view_timezone' => 'UTC',
151 $form->submit($text);
153 $dateTime = new \
DateTime('2010-06-02 UTC');
155 $this->assertDateTimeEquals($dateTime, $form->getData());
156 $this->assertEquals($text, $form->getViewData());
159 public function testSubmitFromChoice()
161 $form = $this->factory
->create('date', null, array(
162 'model_timezone' => 'UTC',
163 'view_timezone' => 'UTC',
164 'widget' => 'choice',
173 $form->submit($text);
175 $dateTime = new \
DateTime('2010-06-02 UTC');
177 $this->assertDateTimeEquals($dateTime, $form->getData());
178 $this->assertEquals($text, $form->getViewData());
181 public function testSubmitFromChoiceEmpty()
183 $form = $this->factory
->create('date', null, array(
184 'model_timezone' => 'UTC',
185 'view_timezone' => 'UTC',
186 'widget' => 'choice',
196 $form->submit($text);
198 $this->assertNull($form->getData());
199 $this->assertEquals($text, $form->getViewData());
202 public function testSubmitFromInputDateTimeDifferentPattern()
204 $form = $this->factory
->create('date', null, array(
205 'model_timezone' => 'UTC',
206 'view_timezone' => 'UTC',
207 'format' => 'MM*yyyy*dd',
208 'widget' => 'single_text',
209 'input' => 'datetime',
212 $form->submit('06*2010*02');
214 $this->assertDateTimeEquals(new \
DateTime('2010-06-02 UTC'), $form->getData());
215 $this->assertEquals('06*2010*02', $form->getViewData());
218 public function testSubmitFromInputStringDifferentPattern()
220 $form = $this->factory
->create('date', null, array(
221 'model_timezone' => 'UTC',
222 'view_timezone' => 'UTC',
223 'format' => 'MM*yyyy*dd',
224 'widget' => 'single_text',
228 $form->submit('06*2010*02');
230 $this->assertEquals('2010-06-02', $form->getData());
231 $this->assertEquals('06*2010*02', $form->getViewData());
234 public function testSubmitFromInputTimestampDifferentPattern()
236 $form = $this->factory
->create('date', null, array(
237 'model_timezone' => 'UTC',
238 'view_timezone' => 'UTC',
239 'format' => 'MM*yyyy*dd',
240 'widget' => 'single_text',
241 'input' => 'timestamp',
244 $form->submit('06*2010*02');
246 $dateTime = new \
DateTime('2010-06-02 UTC');
248 $this->assertEquals($dateTime->format('U'), $form->getData());
249 $this->assertEquals('06*2010*02', $form->getViewData());
252 public function testSubmitFromInputRawDifferentPattern()
254 $form = $this->factory
->create('date', null, array(
255 'model_timezone' => 'UTC',
256 'view_timezone' => 'UTC',
257 'format' => 'MM*yyyy*dd',
258 'widget' => 'single_text',
262 $form->submit('06*2010*02');
270 $this->assertEquals($output, $form->getData());
271 $this->assertEquals('06*2010*02', $form->getViewData());
275 * @dataProvider provideDateFormats
277 public function testDatePatternWithFormatOption($format, $pattern)
279 $form = $this->factory
->create('date', null, array(
283 $view = $form->createView();
285 $this->assertEquals($pattern, $view->vars
['date_pattern']);
288 public function provideDateFormats()
291 array('dMy', '{{ day }}{{ month }}{{ year }}'),
292 array('d-M-yyyy', '{{ day }}-{{ month }}-{{ year }}'),
293 array('M d y', '{{ month }} {{ day }} {{ year }}'),
298 * This test is to check that the strings '0', '1', '2', '3' are no accepted
299 * as valid IntlDateFormatter constants for FULL, LONG, MEDIUM or SHORT respectively.
301 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
303 public function testThrowExceptionIfFormatIsNoPattern()
305 $this->factory
->create('date', null, array(
307 'widget' => 'single_text',
313 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
315 public function testThrowExceptionIfFormatDoesNotContainYearMonthAndDay()
317 $this->factory
->create('date', null, array(
318 'months' => array(6, 7),
324 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
326 public function testThrowExceptionIfFormatIsNoConstant()
328 $this->factory
->create('date', null, array(
334 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
336 public function testThrowExceptionIfFormatIsInvalid()
338 $this->factory
->create('date', null, array(
343 public function testSetDataWithDifferentTimezones()
345 $form = $this->factory
->create('date', null, array(
346 'format' => \IntlDateFormatter
::MEDIUM
,
347 'model_timezone' => 'America/New_York',
348 'view_timezone' => 'Pacific/Tahiti',
350 'widget' => 'single_text',
353 $form->setData('2010-06-02');
355 $this->assertEquals('01.06.2010', $form->getViewData());
358 public function testSetDataWithDifferentTimezonesDateTime()
360 $form = $this->factory
->create('date', null, array(
361 'format' => \IntlDateFormatter
::MEDIUM
,
362 'model_timezone' => 'America/New_York',
363 'view_timezone' => 'Pacific/Tahiti',
364 'input' => 'datetime',
365 'widget' => 'single_text',
368 $dateTime = new \
DateTime('2010-06-02 America/New_York');
370 $form->setData($dateTime);
372 $this->assertDateTimeEquals($dateTime, $form->getData());
373 $this->assertEquals('01.06.2010', $form->getViewData());
376 public function testYearsOption()
378 $form = $this->factory
->create('date', null, array(
379 'years' => array(2010, 2011),
382 $view = $form->createView();
384 $this->assertEquals(array(
385 new ChoiceView('2010', '2010', '2010'),
386 new ChoiceView('2011', '2011', '2011'),
387 ), $view['year']->vars
['choices']);
390 public function testMonthsOption()
392 $form = $this->factory
->create('date', null, array(
393 'months' => array(6, 7),
396 $view = $form->createView();
398 $this->assertEquals(array(
399 new ChoiceView('6', '6', '06'),
400 new ChoiceView('7', '7', '07'),
401 ), $view['month']->vars
['choices']);
404 public function testMonthsOptionShortFormat()
406 $form = $this->factory
->create('date', null, array(
407 'months' => array(1, 4),
408 'format' => 'dd.MMM.yy',
411 $view = $form->createView();
413 $this->assertEquals(array(
414 new ChoiceView('1', '1', 'Jän'),
415 new ChoiceView('4', '4', 'Apr.')
416 ), $view['month']->vars
['choices']);
419 public function testMonthsOptionLongFormat()
421 $form = $this->factory
->create('date', null, array(
422 'months' => array(1, 4),
423 'format' => 'dd.MMMM.yy',
426 $view = $form->createView();
428 $this->assertEquals(array(
429 new ChoiceView('1', '1', 'Jänner'),
430 new ChoiceView('4', '4', 'April'),
431 ), $view['month']->vars
['choices']);
434 public function testMonthsOptionLongFormatWithDifferentTimezone()
436 $form = $this->factory
->create('date', null, array(
437 'months' => array(1, 4),
438 'format' => 'dd.MMMM.yy',
441 $view = $form->createView();
443 $this->assertEquals(array(
444 new ChoiceView('1', '1', 'Jänner'),
445 new ChoiceView('4', '4', 'April'),
446 ), $view['month']->vars
['choices']);
449 public function testIsDayWithinRangeReturnsTrueIfWithin()
451 $form = $this->factory
->create('date', null, array(
452 'days' => array(6, 7),
455 $view = $form->createView();
457 $this->assertEquals(array(
458 new ChoiceView('6', '6', '06'),
459 new ChoiceView('7', '7', '07'),
460 ), $view['day']->vars
['choices']);
463 public function testIsPartiallyFilledReturnsFalseIfSingleText()
465 $this->markTestIncomplete('Needs to be reimplemented using validators');
467 $form = $this->factory
->create('date', null, array(
468 'model_timezone' => 'UTC',
469 'view_timezone' => 'UTC',
470 'widget' => 'single_text',
473 $form->submit('7.6.2010');
475 $this->assertFalse($form->isPartiallyFilled());
478 public function testIsPartiallyFilledReturnsFalseIfChoiceAndCompletelyEmpty()
480 $this->markTestIncomplete('Needs to be reimplemented using validators');
482 $form = $this->factory
->create('date', null, array(
483 'model_timezone' => 'UTC',
484 'view_timezone' => 'UTC',
485 'widget' => 'choice',
494 $this->assertFalse($form->isPartiallyFilled());
497 public function testIsPartiallyFilledReturnsFalseIfChoiceAndCompletelyFilled()
499 $this->markTestIncomplete('Needs to be reimplemented using validators');
501 $form = $this->factory
->create('date', null, array(
502 'model_timezone' => 'UTC',
503 'view_timezone' => 'UTC',
504 'widget' => 'choice',
513 $this->assertFalse($form->isPartiallyFilled());
516 public function testIsPartiallyFilledReturnsTrueIfChoiceAndDayEmpty()
518 $this->markTestIncomplete('Needs to be reimplemented using validators');
520 $form = $this->factory
->create('date', null, array(
521 'model_timezone' => 'UTC',
522 'view_timezone' => 'UTC',
523 'widget' => 'choice',
532 $this->assertTrue($form->isPartiallyFilled());
535 public function testPassDatePatternToView()
537 $form = $this->factory
->create('date');
538 $view = $form->createView();
540 $this->assertSame('{{ day }}{{ month }}{{ year }}', $view->vars
['date_pattern']);
543 public function testPassDatePatternToViewDifferentFormat()
545 $form = $this->factory
->create('date', null, array(
546 'format' => \IntlDateFormatter
::LONG
,
549 $view = $form->createView();
551 $this->assertSame('{{ day }}{{ month }}{{ year }}', $view->vars
['date_pattern']);
554 public function testPassDatePatternToViewDifferentPattern()
556 $form = $this->factory
->create('date', null, array(
557 'format' => 'MMyyyydd'
560 $view = $form->createView();
562 $this->assertSame('{{ month }}{{ year }}{{ day }}', $view->vars
['date_pattern']);
565 public function testPassDatePatternToViewDifferentPatternWithSeparators()
567 $form = $this->factory
->create('date', null, array(
568 'format' => 'MM*yyyy*dd'
571 $view = $form->createView();
573 $this->assertSame('{{ month }}*{{ year }}*{{ day }}', $view->vars
['date_pattern']);
576 public function testDontPassDatePatternIfText()
578 $form = $this->factory
->create('date', null, array(
579 'widget' => 'single_text',
581 $view = $form->createView();
583 $this->assertFalse(isset($view->vars
['date_pattern']));
586 public function testPassWidgetToView()
588 $form = $this->factory
->create('date', null, array(
589 'widget' => 'single_text',
591 $view = $form->createView();
593 $this->assertSame('single_text', $view->vars
['widget']);
597 public function testInitializeWithDateTime()
599 // Throws an exception if "data_class" option is not explicitly set
600 // to null in the type
601 $this->factory
->create('date', new \
DateTime());
604 public function testSingleTextWidgetShouldUseTheRightInputType()
606 $form = $this->factory
->create('date', null, array(
607 'widget' => 'single_text',
610 $view = $form->createView();
611 $this->assertEquals('date', $view->vars
['type']);
614 public function testPassDefaultEmptyValueToViewIfNotRequired()
616 $form = $this->factory
->create('date', null, array(
620 $view = $form->createView();
621 $this->assertSame('', $view['year']->vars
['empty_value']);
622 $this->assertSame('', $view['month']->vars
['empty_value']);
623 $this->assertSame('', $view['day']->vars
['empty_value']);
626 public function testPassNoEmptyValueToViewIfRequired()
628 $form = $this->factory
->create('date', null, array(
632 $view = $form->createView();
633 $this->assertNull($view['year']->vars
['empty_value']);
634 $this->assertNull($view['month']->vars
['empty_value']);
635 $this->assertNull($view['day']->vars
['empty_value']);
638 public function testPassEmptyValueAsString()
640 $form = $this->factory
->create('date', null, array(
641 'empty_value' => 'Empty',
644 $view = $form->createView();
645 $this->assertSame('Empty', $view['year']->vars
['empty_value']);
646 $this->assertSame('Empty', $view['month']->vars
['empty_value']);
647 $this->assertSame('Empty', $view['day']->vars
['empty_value']);
650 public function testPassEmptyValueAsArray()
652 $form = $this->factory
->create('date', null, array(
653 'empty_value' => array(
654 'year' => 'Empty year',
655 'month' => 'Empty month',
656 'day' => 'Empty day',
660 $view = $form->createView();
661 $this->assertSame('Empty year', $view['year']->vars
['empty_value']);
662 $this->assertSame('Empty month', $view['month']->vars
['empty_value']);
663 $this->assertSame('Empty day', $view['day']->vars
['empty_value']);
666 public function testPassEmptyValueAsPartialArrayAddEmptyIfNotRequired()
668 $form = $this->factory
->create('date', null, array(
670 'empty_value' => array(
671 'year' => 'Empty year',
672 'day' => 'Empty day',
676 $view = $form->createView();
677 $this->assertSame('Empty year', $view['year']->vars
['empty_value']);
678 $this->assertSame('', $view['month']->vars
['empty_value']);
679 $this->assertSame('Empty day', $view['day']->vars
['empty_value']);
682 public function testPassEmptyValueAsPartialArrayAddNullIfRequired()
684 $form = $this->factory
->create('date', null, array(
686 'empty_value' => array(
687 'year' => 'Empty year',
688 'day' => 'Empty day',
692 $view = $form->createView();
693 $this->assertSame('Empty year', $view['year']->vars
['empty_value']);
694 $this->assertNull($view['month']->vars
['empty_value']);
695 $this->assertSame('Empty day', $view['day']->vars
['empty_value']);
698 public function testPassHtml5TypeIfSingleTextAndHtml5Format()
700 $form = $this->factory
->create('date', null, array(
701 'widget' => 'single_text',
704 $view = $form->createView();
705 $this->assertSame('date', $view->vars
['type']);
708 public function testDontPassHtml5TypeIfNotHtml5Format()
710 $form = $this->factory
->create('date', null, array(
711 'widget' => 'single_text',
712 'format' => \IntlDateFormatter
::MEDIUM
,
715 $view = $form->createView();
716 $this->assertFalse(isset($view->vars
['type']));
719 public function testDontPassHtml5TypeIfNotSingleText()
721 $form = $this->factory
->create('date', null, array(
725 $view = $form->createView();
726 $this->assertFalse(isset($view->vars
['type']));
729 public function provideCompoundWidgets()
738 * @dataProvider provideCompoundWidgets
740 public function testYearErrorsBubbleUp($widget)
742 $error = new FormError('Invalid!');
743 $form = $this->factory
->create('date', null, array(
746 $form['year']->addError($error);
748 $this->assertSame(array(), $form['year']->getErrors());
749 $this->assertSame(array($error), $form->getErrors());
753 * @dataProvider provideCompoundWidgets
755 public function testMonthErrorsBubbleUp($widget)
757 $error = new FormError('Invalid!');
758 $form = $this->factory
->create('date', null, array(
761 $form['month']->addError($error);
763 $this->assertSame(array(), $form['month']->getErrors());
764 $this->assertSame(array($error), $form->getErrors());
768 * @dataProvider provideCompoundWidgets
770 public function testDayErrorsBubbleUp($widget)
772 $error = new FormError('Invalid!');
773 $form = $this->factory
->create('date', null, array(
776 $form['day']->addError($error);
778 $this->assertSame(array(), $form['day']->getErrors());
779 $this->assertSame(array($error), $form->getErrors());