1111use AutoMapper \Metadata \TargetPropertyMetadata ;
1212use AutoMapper \Symfony \Bundle \CacheWarmup \CacheWarmer ;
1313use AutoMapper \Symfony \Bundle \DataCollector \MetadataCollector ;
14+ use AutoMapper \Tests \Bundle \Resources \App \AppKernel ;
1415use AutoMapper \Tests \Bundle \Resources \App \Entity \AddressDTO ;
1516use AutoMapper \Tests \Bundle \Resources \App \Entity \ClassWithMapToContextAttribute ;
1617use AutoMapper \Tests \Bundle \Resources \App \Entity \ClassWithPrivateProperty ;
2324use Symfony \Component \Filesystem \Filesystem ;
2425use Symfony \Component \HttpFoundation \Request ;
2526use Symfony \Component \HttpFoundation \Response ;
27+ use Symfony \Component \HttpKernel \KernelInterface ;
2628
2729class 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