Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b5592ee
Initial commit and implementation of symfony/automapper
joelwurtz Feb 14, 2019
936bc2f
Add normalizer bridge
joelwurtz Feb 14, 2019
2ea1951
Add missing licence
joelwurtz Feb 14, 2019
d272d9c
Fix typo in interface
joelwurtz Feb 14, 2019
beb0f49
Add attribute checking option
joelwurtz Feb 15, 2019
e4c1385
Allow attribute checking to be set in factory
joelwurtz Feb 15, 2019
cf1fdf2
Add empty test classes
joelwurtz Feb 15, 2019
fc1d87d
Add automapper tests
joelwurtz Feb 19, 2019
bd387f5
Apply suggestions from @dunglas code review
dunglas Mar 25, 2019
20b7735
Add expiremental annotation where needed, add final to class that sho…
joelwurtz Mar 25, 2019
12e5d1f
Fix cs and typo
joelwurtz Mar 25, 2019
26e0c8a
Avoid too many function calls
joelwurtz Mar 25, 2019
678537a
Add interface for generator metadatas
joelwurtz Mar 25, 2019
ceeeaf1
Use array for context and provide helper class
joelwurtz Mar 26, 2019
ec97f74
Use new context construction in normalizer bridge
joelwurtz Mar 26, 2019
92784b1
Fix test case class test
joelwurtz Mar 26, 2019
eb6f638
Remove useless class
joelwurtz Mar 26, 2019
fab7135
expiremental > expiremental in 4.3
joelwurtz Mar 26, 2019
5154440
Fixing tests
Korbeil Dec 31, 2019
c688855
Added AutoMapperNormalizerTest
Korbeil Jan 3, 2020
9f16b43
Context tests
Korbeil Jan 5, 2020
ed00ec3
Add MapperGeneratorMetadataFactory tests
Korbeil Jan 5, 2020
ab82b9f
Add FromSourceMappingExtractor tests
Korbeil Jan 5, 2020
ee908a6
Add FromTargetMappingExtractor tests
Korbeil Jan 5, 2020
cd2d414
WIP PrivateReflectionExtractor tests
Korbeil Jan 6, 2020
cd85e2d
Remove internal for generated mapper
joelwurtz Jan 31, 2020
f74e22e
Fix context rebase
joelwurtz Jan 31, 2020
ee671dd
Add missing deps in dev
joelwurtz Jan 31, 2020
78ef805
Use property read / write info extractor
joelwurtz Jan 31, 2020
1e4bd5e
Fix cs
joelwurtz Jan 31, 2020
20a37df
Add tests and date time mutable / immutable transformations
joelwurtz Feb 2, 2020
af1fdf2
Fix header, expiremental in 5.1
joelwurtz Feb 2, 2020
c07edd2
Fix php version
joelwurtz Feb 2, 2020
ad190a2
Fix createFromImmutable not available in php 7.2
joelwurtz Feb 2, 2020
9c3cb23
Fix test
joelwurtz Feb 2, 2020
fecf949
Better conditions on automapper
joelwurtz Feb 3, 2020
ad0f487
Remove bad deps on rebase
joelwurtz Feb 3, 2020
5ba8171
Fix class exists
joelwurtz Feb 3, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests and date time mutable / immutable transformations
  • Loading branch information
joelwurtz committed Jun 20, 2020
commit 20a37dfc70894ad5a3c5c2e38dc15733f4c3fbf4
15 changes: 0 additions & 15 deletions src/Symfony/Component/AutoMapper/Extractor/ReadAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,6 @@ public function __construct(int $type, string $name, $private = false)
$this->private = $private;
}

public function getType(): int
{
return $this->type;
}

public function getName(): string
{
return $this->name;
}

public function isPrivate(): bool
{
return $this->private;
}

/**
* Get AST expression for reading property from an input.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@ class SourceTargetMappingExtractor extends MappingExtractor
*/
public function getPropertiesMapping(MapperMetadataInterface $mapperMetadata): array
{
$sourceProperties = array_unique($this->propertyInfoExtractor->getProperties($mapperMetadata->getSource()));
$targetProperties = array_unique($this->propertyInfoExtractor->getProperties($mapperMetadata->getTarget()));
$sourceProperties = $this->propertyInfoExtractor->getProperties($mapperMetadata->getSource());
$targetProperties = $this->propertyInfoExtractor->getProperties($mapperMetadata->getTarget());

if (null === $sourceProperties || null === $targetProperties) {
return [];
}

$sourceProperties = array_unique($sourceProperties ?? []);
$targetProperties = array_unique($targetProperties ?? []);

$mapping = [];

foreach ($sourceProperties as $property) {
Expand Down
14 changes: 0 additions & 14 deletions src/Symfony/Component/AutoMapper/Extractor/WriteMutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ public function getType(): int
return $this->type;
}

public function getName(): string
{
return $this->name;
}

public function isPrivate(): bool
{
return $this->private;
}

/**
* Get AST expression for writing from a value to an output.
*
Expand Down Expand Up @@ -97,10 +87,6 @@ public function getExpression(Expr\Variable $output, Expr $value, bool $byRef =
return new Expr\Assign(new Expr\ArrayDimFetch($output, new Scalar\String_($this->name)), $value);
}

if (self::TYPE_CONSTRUCTOR === $this->type) {
return null;
}

throw new CompileException('Invalid accessor for write expression');
}

Expand Down
6 changes: 1 addition & 5 deletions src/Symfony/Component/AutoMapper/MapperMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ public function getPropertiesMapping(): array
*/
public function getPropertyMapping(string $property): ?PropertyMapping
{
if (null === $this->propertiesMapping) {
$this->buildPropertyMapping();
}

return $this->propertiesMapping[$property] ?? null;
return $this->getPropertiesMapping()[$property] ?? null;
}

/**
Expand Down
123 changes: 123 additions & 0 deletions src/Symfony/Component/AutoMapper/Tests/AutoMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,29 @@ public function testAutoMapperFromArray(): void
self::assertEquals(1987, $userDto->createdAt->format('Y'));
}

public function testAutoMapperFromArrayCustomDateTime(): void
{
$dateTime = \DateTime::createFromFormat(\DateTime::RFC3339, '1987-04-30T06:00:00Z');
$customFormat = 'U';
$user = [
'id' => 1,
'address' => [
'city' => 'Toulon',
],
'createdAt' => $dateTime->format($customFormat),
];

$autoMapper = AutoMapper::create(true, $this->loader, null, 'CustomDateTime_');
$configuration = $autoMapper->getMetadata('array', Fixtures\UserDTO::class);
$configuration->setDateTimeFormat($customFormat);

/** @var Fixtures\UserDTO $userDto */
$userDto = $autoMapper->map($user, Fixtures\UserDTO::class);

self::assertInstanceOf(Fixtures\UserDTO::class, $userDto);
self::assertEquals($dateTime->format($customFormat), $userDto->createdAt->format($customFormat));
}

public function testAutoMapperToArray(): void
{
$address = new Fixtures\Address();
Expand Down Expand Up @@ -114,6 +137,62 @@ public function testAutoMapperToStdObject(): void
self::assertEquals(1, $user->id);
}

public function testNotReadable(): void
{
$autoMapper = AutoMapper::create(false, $this->loader, null, 'NotReadable_');
$address = new Fixtures\Address();
$address->setCity('test');

$addressArray = $autoMapper->map($address, 'array');

self::assertIsArray($addressArray);
self::assertArrayNotHasKey('city', $addressArray);

$addressMapped = $autoMapper->map($address, Fixtures\Address::class);

self::assertInstanceOf(Fixtures\Address::class, $addressMapped);

$property = (new \ReflectionClass($addressMapped))->getProperty('city');
$property->setAccessible(true);

$city = $property->getValue($addressMapped);

self::assertNull($city);
}

public function testNoTypes(): void
{
$autoMapper = AutoMapper::create(false, $this->loader, null, 'NotReadable_');
$address = new Fixtures\AddressNoTypes();
$address->city = 'test';

$addressArray = $autoMapper->map($address, 'array');

self::assertIsArray($addressArray);
self::assertArrayNotHasKey('city', $addressArray);
}

public function testNoTransformer(): void
{
$addressFoo = new Fixtures\AddressFoo();
$addressFoo->city = new Fixtures\CityFoo();
$addressFoo->city->name = 'test';

$addressBar = $this->autoMapper->map($addressFoo, Fixtures\AddressBar::class);

self::assertInstanceOf(Fixtures\AddressBar::class, $addressBar);
self::assertNull($addressBar->city);
}

public function testNoProperties(): void
{
$noProperties = new Fixtures\FooNoProperties();
$noPropertiesMapped = $this->autoMapper->map($noProperties, Fixtures\FooNoProperties::class);

self::assertInstanceOf(Fixtures\FooNoProperties::class, $noPropertiesMapped);
self::assertNotSame($noProperties, $noPropertiesMapped);
}

public function testGroupsSourceTarget(): void
{
$foo = new Fixtures\Foo();
Expand Down Expand Up @@ -205,6 +284,28 @@ public function testDeepCloningArray(): void
self::assertSame($newNode, $newNode['parent']['parent']['parent']);
}

public function testCircularReferenceDeep(): void
{
$foo = new Fixtures\CircularFoo();
$bar = new Fixtures\CircularBar();
$baz = new Fixtures\CircularBaz();

$foo->bar = $bar;
$bar->baz = $baz;
$baz->foo = $foo;


$newFoo = $this->autoMapper->map($foo, Fixtures\CircularFoo::class);

self::assertNotSame($foo, $newFoo);
self::assertNotNull($newFoo->bar);
self::assertNotSame($bar, $newFoo->bar);
self::assertNotNull($newFoo->bar->baz);
self::assertNotSame($baz, $newFoo->bar->baz);
self::assertNotNull($newFoo->bar->baz->foo);
self::assertSame($newFoo, $newFoo->bar->baz->foo);
}

public function testCircularReferenceArray(): void
{
$nodeA = new Fixtures\Node();
Expand Down Expand Up @@ -248,6 +349,28 @@ public function testConstructor(): void
self::assertSame('10', $userDto->getId());
self::assertSame('foo', $userDto->getName());
self::assertSame(3, $userDto->getAge());
self::assertTrue($userDto->getConstructor());
}

public function testConstructorNotAllowed(): void
{
$autoMapper = AutoMapper::create(true, $this->loader, null, 'NotAllowedMapper_');
$configuration = $autoMapper->getMetadata(Fixtures\UserDTO::class, Fixtures\UserConstructorDTO::class);
$configuration->setConstructorAllowed(false);

$user = new Fixtures\UserDTO();
$user->id = 10;
$user->setName('foo');
$user->age = 3;

/** @var Fixtures\UserConstructorDTO $userDto */
$userDto = $autoMapper->map($user, Fixtures\UserConstructorDTO::class);

self::assertInstanceOf(Fixtures\UserConstructorDTO::class, $userDto);
self::assertSame('10', $userDto->getId());
self::assertSame('foo', $userDto->getName());
self::assertSame(3, $userDto->getAge());
self::assertFalse($userDto->getConstructor());
}

public function testConstructorWithDefault(): void
Expand Down
8 changes: 0 additions & 8 deletions src/Symfony/Component/AutoMapper/Tests/Fixtures/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ class Address
*/
private $city;

/**
* @return string
*/
public function getCity(): ?string
{
return $this->city;
}

/**
* @param string $city
*/
Expand Down
20 changes: 20 additions & 0 deletions src/Symfony/Component/AutoMapper/Tests/Fixtures/AddressBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\AutoMapper\Tests\Fixtures;

class AddressBar
{
/**
* @var string|null
*/
public $city;
}
20 changes: 20 additions & 0 deletions src/Symfony/Component/AutoMapper/Tests/Fixtures/AddressFoo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\AutoMapper\Tests\Fixtures;

class AddressFoo
{
/**
* @var CityFoo
*/
public $city;
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/AutoMapper/Tests/Fixtures/AddressNoTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\AutoMapper\Tests\Fixtures;

class AddressNoTypes
{
public $city;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\AutoMapper\Tests\Fixtures;

class AddressNotWritable
{
/**
* @var string|null
*/
private $city;

public function getCity(): ?string
{
return $this->city;
}
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/AutoMapper/Tests/Fixtures/CircularBar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace Symfony\Component\AutoMapper\Tests\Fixtures;


class CircularBar
{
/** @var CircularBaz */
public $baz;
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/AutoMapper/Tests/Fixtures/CircularBaz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace Symfony\Component\AutoMapper\Tests\Fixtures;


class CircularBaz
{
/** @var CircularFoo */
public $foo;
}
11 changes: 11 additions & 0 deletions src/Symfony/Component/AutoMapper/Tests/Fixtures/CircularFoo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php


namespace Symfony\Component\AutoMapper\Tests\Fixtures;


class CircularFoo
{
/** @var CircularBar */
public $bar;
}
8 changes: 8 additions & 0 deletions src/Symfony/Component/AutoMapper/Tests/Fixtures/CityFoo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Symfony\Component\AutoMapper\Tests\Fixtures;

class CityFoo
{
public $name;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Symfony\Component\AutoMapper\Tests\Fixtures;

class FooNoProperties
{
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/AutoMapper/Tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class User
public $addresses = [];

/**
* @var \DateTimeInterface
* @var \DateTime
*/
public $createdAt;

Expand Down
Loading