Skip to content

Commit

Permalink
Merge branch '6.0' into 6.1
Browse files Browse the repository at this point in the history
* 6.0:
  clean up legacy test
  fix dispatch signal event check for compatibility with the contract interface
  ignore missing keys when mapping DateTime objects to uninitialized arrays
  • Loading branch information
xabbuh committed Aug 9, 2022
2 parents 6723f2c + 0fc271d commit 0a1a3ea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions Extension/Core/DataAccessor/PropertyPathAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\Exception\AccessException;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\Exception\AccessException as PropertyAccessException;
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
use Symfony\Component\PropertyAccess\Exception\UninitializedPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
Expand Down Expand Up @@ -90,6 +91,10 @@ private function getPropertyValue(object|array $data, PropertyPathInterface $pro
try {
return $this->propertyAccessor->getValue($data, $propertyPath);
} catch (PropertyAccessException $e) {
if (\is_array($data) && $e instanceof NoSuchIndexException) {
return null;
}

if (!$e instanceof UninitializedPropertyException
// For versions without UninitializedPropertyException check the exception message
&& (class_exists(UninitializedPropertyException::class) || !str_contains($e->getMessage(), 'You should initialize it'))
Expand Down
6 changes: 3 additions & 3 deletions Tests/CompoundFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Form\Exception\AlreadySubmittedException;
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
Expand Down Expand Up @@ -1079,13 +1079,13 @@ public function testFileUpload()
$this->assertNull($this->form->get('bar')->getData());
}

public function testMapDateTimeObjectsWithEmptyArrayData()
public function testMapDateTimeObjectsWithEmptyArrayDataUsingDataMapper()
{
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
$form = $this->factory->createBuilder()
->setDataMapper(new PropertyPathMapper($propertyAccessor))
->setDataMapper(new DataMapper(new PropertyPathAccessor($propertyAccessor)))
->add('date', DateType::class, [
'auto_initialize' => false,
'format' => 'dd/MM/yyyy',
Expand Down
31 changes: 31 additions & 0 deletions Tests/Extension/Core/DataMapper/DataMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormConfigBuilder;
use Symfony\Component\Form\FormFactoryBuilder;
use Symfony\Component\Form\Tests\Fixtures\TypehintedPropertiesCar;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyPath;

class DataMapperTest extends TestCase
Expand Down Expand Up @@ -382,6 +386,33 @@ public function testMapFormsToDataUsingSetCallbackOption()

self::assertSame('Jane Doe', $person->myName());
}

public function testMapFormsToDataMapsDateTimeInstanceToArrayIfNotSetBefore()
{
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
->enableExceptionOnInvalidIndex()
->getPropertyAccessor();
$form = (new FormFactoryBuilder())->getFormFactory()->createBuilder()
->setDataMapper(new DataMapper(new PropertyPathAccessor($propertyAccessor)))
->add('date', DateType::class, [
'auto_initialize' => false,
'format' => 'dd/MM/yyyy',
'html5' => false,
'model_timezone' => 'UTC',
'view_timezone' => 'UTC',
'widget' => 'single_text',
])
->getForm();

$form->submit([
'date' => '04/08/2022',
]);

$this->assertEquals(['date' => new \DateTime('2022-08-04', new \DateTimeZone('UTC'))], $form->getData());
}
}

class SubmittedForm extends Form
Expand Down

0 comments on commit 0a1a3ea

Please sign in to comment.