Skip to content

Commit 7fe4191

Browse files
committed
fix(bundle): use map_private_properties when configuring ReflectionExtractor
1 parent bd17283 commit 7fe4191

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

src/Symfony/Bundle/DependencyInjection/AutoMapperExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public function load(array $configs, ContainerBuilder $container): void
6767
->setArgument('$allowReadOnlyTargetToPopulate', $config['allow_readonly_target_to_populate'])
6868
;
6969

70+
$container->setParameter('automapper.map_private_properties', $config['map_private_properties']);
71+
7072
$container->registerForAutoconfiguration(PropertyTransformerInterface::class)->addTag('automapper.property_transformer');
7173

7274
if ($config['loader']['eval']) {

src/Symfony/Bundle/DependencyInjection/Compiler/PropertyInfoPass.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public function process(ContainerBuilder $container): void
2222
return;
2323
}
2424

25+
$flags = ReflectionExtractor::ALLOW_PUBLIC;
26+
27+
if ($container->getParameter('automapper.map_private_properties')) {
28+
$flags |= ReflectionExtractor::ALLOW_PRIVATE | ReflectionExtractor::ALLOW_PROTECTED;
29+
}
30+
2531
$container->setDefinition(
2632
'automapper.property_info.reflection_extractor',
2733
new Definition(
@@ -31,7 +37,7 @@ public function process(ContainerBuilder $container): void
3137
'$accessorPrefixes' => null,
3238
'$arrayMutatorPrefixes' => null,
3339
'$enableConstructorExtraction' => true,
34-
'$accessFlags' => ReflectionExtractor::ALLOW_PUBLIC | ReflectionExtractor::ALLOW_PROTECTED | ReflectionExtractor::ALLOW_PRIVATE,
40+
'$accessFlags' => $flags,
3541
]
3642
)
3743
);

tests/Bundle/Resources/App/AppKernel.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,39 @@
55
namespace AutoMapper\Tests\Bundle\Resources\App;
66

77
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
89
use Symfony\Component\HttpKernel\Kernel;
910

1011
class AppKernel extends Kernel
1112
{
1213
use MicroKernelTrait;
1314

15+
public function __construct(
16+
string $environment,
17+
bool $debug,
18+
private ?string $additionalConfigFile = null
19+
) {
20+
parent::__construct($environment, $debug);
21+
}
22+
23+
protected function buildContainer(): ContainerBuilder
24+
{
25+
$containerBuilder = parent::buildContainer();
26+
27+
if ($this->additionalConfigFile) {
28+
$this->getContainerLoader($containerBuilder)->load($this->additionalConfigFile);
29+
}
30+
31+
return $containerBuilder;
32+
}
33+
1434
public function getProjectDir(): string
1535
{
1636
return __DIR__ . '/..';
1737
}
38+
39+
public function getCacheDir(): string
40+
{
41+
return parent::getCacheDir() . '/' . md5($this->additionalConfigFile ?? '');
42+
}
1843
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
automapper:
2+
class_prefix: "Symfony_Mapper_With_Private_Property"
3+
map_private_properties: true

tests/Bundle/ServiceInstantiationTest.php

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use AutoMapper\Metadata\TargetPropertyMetadata;
1212
use AutoMapper\Symfony\Bundle\CacheWarmup\CacheWarmer;
1313
use AutoMapper\Symfony\Bundle\DataCollector\MetadataCollector;
14+
use AutoMapper\Tests\Bundle\Resources\App\AppKernel;
1415
use AutoMapper\Tests\Bundle\Resources\App\Entity\AddressDTO;
1516
use AutoMapper\Tests\Bundle\Resources\App\Entity\ClassWithMapToContextAttribute;
1617
use AutoMapper\Tests\Bundle\Resources\App\Entity\ClassWithPrivateProperty;
@@ -23,15 +24,14 @@
2324
use Symfony\Component\Filesystem\Filesystem;
2425
use Symfony\Component\HttpFoundation\Request;
2526
use Symfony\Component\HttpFoundation\Response;
27+
use Symfony\Component\HttpKernel\KernelInterface;
2628

2729
class ServiceInstantiationTest extends WebTestCase
2830
{
29-
protected function setUp(): void
31+
public static function setUpBeforeClass(): void
3032
{
3133
static::$class = null;
3234
$_SERVER['KERNEL_DIR'] = __DIR__ . '/Resources/App';
33-
$_SERVER['KERNEL_CLASS'] = 'AutoMapper\Tests\Bundle\Resources\App\AppKernel';
34-
$_SERVER['APP_DEBUG'] = false;
3535

3636
(new Filesystem())->remove(__DIR__ . '/Resources/var/cache/test');
3737
}
@@ -47,13 +47,14 @@ public function testWarmup(): void
4747
$service = static::$kernel->getContainer()->get(CacheWarmer::class);
4848
$service->warmUp(__DIR__ . '/Resources/var/cache/test');
4949

50-
self::assertFileExists(__DIR__ . '/Resources/var/cache/test/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_NestedObject_array.php');
51-
self::assertFileExists(__DIR__ . '/Resources/var/cache/test/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_User_array.php');
52-
self::assertFileExists(__DIR__ . '/Resources/var/cache/test/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_AddressDTO_array.php');
53-
self::assertFileExists(__DIR__ . '/Resources/var/cache/test/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_Pet_array.php');
54-
self::assertFileExists(__DIR__ . '/Resources/var/cache/test/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_Dog_array.php');
55-
self::assertFileExists(__DIR__ . '/Resources/var/cache/test/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_Cat_array.php');
56-
self::assertFileExists(__DIR__ . '/Resources/var/cache/test/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Api_Entity_Book_array.php');
50+
$cacheDir = static::$kernel->getCacheDir();
51+
self::assertFileExists("{$cacheDir}/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_NestedObject_array.php");
52+
self::assertFileExists("{$cacheDir}/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_User_array.php");
53+
self::assertFileExists("{$cacheDir}/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_AddressDTO_array.php");
54+
self::assertFileExists("{$cacheDir}/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_Pet_array.php");
55+
self::assertFileExists("{$cacheDir}/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_Dog_array.php");
56+
self::assertFileExists("{$cacheDir}/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Entity_Cat_array.php");
57+
self::assertFileExists("{$cacheDir}/automapper/Symfony_Mapper_AutoMapper_Tests_Bundle_Resources_App_Api_Entity_Book_array.php");
5758
}
5859

5960
public function testAutoMapper(): void
@@ -121,9 +122,10 @@ public function testItCanMapEnums(): void
121122
/**
122123
* This test validates that PropertyInfoPass is correctly applied.
123124
*/
124-
public function testMapClassWithPrivateProperty(): void
125+
public function testMapToClassWithPrivateProperty(): void
125126
{
126127
static::bootKernel();
128+
127129
$container = static::$kernel->getContainer();
128130
$autoMapper = $container->get(AutoMapperInterface::class);
129131

@@ -133,6 +135,28 @@ public function testMapClassWithPrivateProperty(): void
133135
);
134136
}
135137

138+
/**
139+
* This test validates that PropertyInfoPass is correctly applied.
140+
*
141+
* @dataProvider mapFromClassWithPrivatePropertyProvider
142+
*/
143+
public function testMapFromClassWithPrivateProperty(array $kernelOptions, array $expected): void
144+
{
145+
static::bootKernel($kernelOptions);
146+
$autoMapper = self::getContainer()->get(AutoMapperInterface::class);
147+
148+
self::assertEquals(
149+
$expected,
150+
$autoMapper->map(new ClassWithPrivateProperty('foo'), 'array')
151+
);
152+
}
153+
154+
public static function mapFromClassWithPrivatePropertyProvider(): iterable
155+
{
156+
yield 'disallow private properties' => [[], []];
157+
yield 'allow private properties' => [['additionalConfigFile' => __DIR__ . '/Resources/config/with-private-properties.yml'], ['foo' => 'foo', 'bar' => 'bar']];
158+
}
159+
136160
/**
137161
* We need to test that the mapToContext attribute is correctly used,
138162
* because this behavior is dependent of the dependency injection.
@@ -253,4 +277,9 @@ public function getData(): array
253277
}
254278
}
255279
}
280+
281+
protected static function createKernel(array $options = []): KernelInterface
282+
{
283+
return new AppKernel('test', false, $options['additionalConfigFile'] ?? null);
284+
}
256285
}

0 commit comments

Comments
 (0)