Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
Merge ed78a16 into c2941d1
Browse files Browse the repository at this point in the history
  • Loading branch information
Kal74 committed Oct 15, 2018
2 parents c2941d1 + ed78a16 commit eb50fd6
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 24 deletions.
51 changes: 33 additions & 18 deletions src/Form/Type/BasePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;

/**
Expand Down Expand Up @@ -65,27 +67,40 @@ public function __construct(MomentFormatConverter $formatConverter, TranslatorIn
$this->locale = $this->translator->getLocale();
}

public function finishView(FormView $view, FormInterface $form, array $options)
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$format = $options['format'];
$resolver->setNormalizer('format', function (Options $options, $format) {
if (isset($options['date_format']) && \is_string($options['date_format'])) {
return $options['date_format'];
}

if (isset($options['date_format']) && \is_string($options['date_format'])) {
$format = $options['date_format'];
} elseif (\is_int($format)) {
$timeFormat = \IntlDateFormatter::NONE;
if ($options['dp_pick_time']) {
$timeFormat = $options['dp_use_seconds'] ? DateTimeType::DEFAULT_TIME_FORMAT : \IntlDateFormatter::SHORT;
if (\is_int($format)) {
$timeFormat = \IntlDateFormatter::NONE;
if ($options['dp_pick_time']) {
$timeFormat = $options['dp_use_seconds'] ? DateTimeType::DEFAULT_TIME_FORMAT : \IntlDateFormatter::SHORT;
}
$intlDateFormatter = new \IntlDateFormatter(
$this->locale,
$format,
$timeFormat,
null,
\IntlDateFormatter::GREGORIAN,
null
);

return $intlDateFormatter->getPattern();
}
$intlDateFormatter = new \IntlDateFormatter(
$this->locale,
$format,
$timeFormat,
null,
\IntlDateFormatter::GREGORIAN,
null
);
$format = $intlDateFormatter->getPattern();
}

return $format;
});
}

public function finishView(FormView $view, FormInterface $form, array $options)
{
$format = $options['format'];

// use seconds if it's allowed in format
$options['dp_use_seconds'] = false !== strpos($format, 's');
Expand Down
2 changes: 2 additions & 0 deletions src/Form/Type/DatePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public function configureOptions(OptionsResolver $resolver)
'dp_pick_time' => false,
'format' => DateType::DEFAULT_FORMAT,
]));

parent::configureOptions($resolver);
}

public function getParent()
Expand Down
2 changes: 2 additions & 0 deletions src/Form/Type/DateTimePickerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function configureOptions(OptionsResolver $resolver)
'format' => DateTimeType::DEFAULT_DATE_FORMAT,
'date_format' => null,
]));

parent::configureOptions($resolver);
}

public function getParent()
Expand Down
3 changes: 1 addition & 2 deletions tests/Form/Type/BasePickerTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ public function testTimePickerIntlFormater()
$form = new Form($this->createMock(FormConfigInterface::class));

$type->finishView($view, $form, [
'format' => \IntlDateFormatter::NONE,
'format' => 'H:mm',
'dp_min_date' => '1/1/1900',
'dp_max_date' => new \DateTime('3/1/2001'),
'dp_use_seconds' => false,
'dp_pick_time' => true,
'dp_pick_date' => false,
]);
Expand Down
30 changes: 28 additions & 2 deletions tests/Form/Type/DatePickerTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@

namespace Sonata\CoreBundle\Tests\Form\Type;

use PHPUnit\Framework\TestCase;
use Sonata\CoreBundle\Date\MomentFormatConverter;
use Sonata\CoreBundle\Form\Type\DatePickerType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Translation\TranslatorInterface;

/**
* @author Hugo Briand <briand@ekino.com>
*/
class DatePickerTypeTest extends TestCase
class DatePickerTypeTest extends TypeTestCase
{
public function testBuildForm()
{
Expand Down Expand Up @@ -73,4 +74,29 @@ public function testConstructorLegacy()

$this->assertSame('sonata_type_date_picker', $type->getName());
}

public function testSubmitValidData()
{
\Locale::setDefault('en');
$form = $this->factory->create(DatePickerType::class, new \DateTime('2018-06-03'), [
'format' => \IntlDateFormatter::LONG,
]);

$this->assertSame('June 3, 2018', $form->getViewData());
$form->submit('June 5, 2018');
$this->assertSame('2018-06-05', $form->getData()->format('Y-m-d'));
$this->assertTrue($form->isSynchronized());
}

protected function getExtensions()
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->method('getLocale')->willReturn('en');

$type = new DatePickerType(new MomentFormatConverter(), $translator);

return [
new PreloadedExtension([$type], []),
];
}
}
46 changes: 44 additions & 2 deletions tests/Form/Type/DateTimePickerTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@

namespace Sonata\CoreBundle\Tests\Form\Type;

use PHPUnit\Framework\TestCase;
use Sonata\CoreBundle\Date\MomentFormatConverter;
use Sonata\CoreBundle\Form\Type\DateTimePickerType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
use Symfony\Component\Translation\TranslatorInterface;

/**
* @author Hugo Briand <briand@ekino.com>
*/
class DateTimePickerTypeTest extends TestCase
class DateTimePickerTypeTest extends TypeTestCase
{
public function testBuildForm()
{
Expand Down Expand Up @@ -77,4 +78,45 @@ public function testConstructorLegacy()

$this->assertSame('sonata_type_datetime_picker', $type->getName());
}

public function testSubmitUnmatchingDateFormat()
{
\Locale::setDefault('en');
$form = $this->factory->create(DateTimePickerType::class, new \DateTime('2018-06-03 20:02:03'), [
'format' => \IntlDateFormatter::NONE,
'dp_pick_date' => false,
'dp_use_seconds' => false,
]);

$form->submit('05:23');
$this->assertFalse($form->isSynchronized());
}

public function testSubmitMatchingDateFormat()
{
\Locale::setDefault('en');
$form = $this->factory->create(DateTimePickerType::class, new \DateTime('2018-06-03 20:02:03'), [
'format' => \IntlDateFormatter::NONE,
'dp_pick_date' => false,
'dp_use_seconds' => false,
]);

$this->assertSame('8:02 PM', $form->getViewData());

$form->submit('5:23 AM');
$this->assertSame('1970-01-01 05:23:00', $form->getData()->format('Y-m-d H:i:s'));
$this->assertTrue($form->isSynchronized());
}

protected function getExtensions()
{
$translator = $this->createMock(TranslatorInterface::class);
$translator->method('getLocale')->willReturn('en');

$type = new DateTimePickerType(new MomentFormatConverter(), $translator);

return [
new PreloadedExtension([$type], []),
];
}
}

0 comments on commit eb50fd6

Please sign in to comment.