1111
1212namespace Symfony \Bundle \FrameworkBundle \Tests \Functional ;
1313
14+ use Symfony \Bundle \FrameworkBundle \Tests \Functional \app \AppKernel ;
15+ use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
16+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
17+
1418/**
1519 * @author Kévin Dunglas <dunglas@gmail.com>
1620 */
@@ -33,39 +37,55 @@ public function testDeserializeArrayOfObject()
3337 $ this ->assertEquals ($ expected , $ result );
3438 }
3539
36- /**
37- * @dataProvider provideNormalizersAndEncodersWithDefaultContextOption
38- */
39- public function testNormalizersAndEncodersUseDefaultContextConfigOption (string $ normalizerId )
40+ public function testNormalizersAndEncodersUseDefaultContextConfigOption ()
4041 {
41- static ::bootKernel (['test_case ' => 'Serializer ' ]);
42+ /** @var SerializerKernel $kernel */
43+ $ kernel = static ::bootKernel (['test_case ' => 'Serializer ' , 'root_config ' => 'default_context.yaml ' ]);
44+
45+ foreach ($ kernel ->normalizersAndEncoders as $ normalizerOrEncoderId ) {
46+ $ normalizerOrEncoder = static ::getContainer ()->get ($ normalizerOrEncoderId );
4247
43- $ normalizer = static ::getContainer ()->get ($ normalizerId );
48+ $ reflectionObject = new \ReflectionObject ($ normalizerOrEncoder );
49+ $ property = $ reflectionObject ->getProperty ('defaultContext ' );
50+ $ property ->setAccessible (true );
4451
45- $ reflectionObject = new \ReflectionObject ($ normalizer );
46- $ property = $ reflectionObject ->getProperty ('defaultContext ' );
47- $ property ->setAccessible (true );
52+ $ defaultContext = $ property ->getValue ($ normalizerOrEncoder );
4853
49- $ defaultContext = $ property ->getValue ($ normalizer );
54+ self ::assertArrayHasKey ('fake_context_option ' , $ defaultContext );
55+ self ::assertEquals ('foo ' , $ defaultContext ['fake_context_option ' ]);
56+ }
57+ }
5058
51- self ::assertArrayHasKey ('fake_context_option ' , $ defaultContext );
52- self ::assertEquals ('foo ' , $ defaultContext ['fake_context_option ' ]);
59+ protected static function getKernelClass (): string
60+ {
61+ return SerializerKernel::class;
5362 }
63+ }
64+
65+ class SerializerKernel extends AppKernel implements CompilerPassInterface
66+ {
67+ public $ normalizersAndEncoders = [
68+ 'serializer.normalizer.property.alias ' , // Special case as this normalizer isn't tagged
69+ ];
5470
55- public static function provideNormalizersAndEncodersWithDefaultContextOption (): array
71+ public function process ( ContainerBuilder $ container )
5672 {
57- return [
58- ['serializer.normalizer.constraint_violation_list.alias ' ],
59- ['serializer.normalizer.dateinterval.alias ' ],
60- ['serializer.normalizer.datetime.alias ' ],
61- ['serializer.normalizer.json_serializable.alias ' ],
62- ['serializer.normalizer.problem.alias ' ],
63- ['serializer.normalizer.uid.alias ' ],
64- ['serializer.normalizer.object.alias ' ],
65- ['serializer.encoder.xml.alias ' ],
66- ['serializer.encoder.yaml.alias ' ],
67- ['serializer.encoder.csv.alias ' ],
68- ];
73+ $ services = array_merge (
74+ $ container ->findTaggedServiceIds ('serializer.normalizer ' ),
75+ $ container ->findTaggedServiceIds ('serializer.encoder ' )
76+ );
77+ foreach ($ services as $ serviceId => $ attributes ) {
78+ $ class = $ container ->getDefinition ($ serviceId )->getClass ();
79+ if (null === $ reflectionConstructor = (new \ReflectionClass ($ class ))->getConstructor ()) {
80+ continue ;
81+ }
82+ foreach ($ reflectionConstructor ->getParameters () as $ reflectionParam ) {
83+ if ('array $defaultContext ' === $ reflectionParam ->getType ()->getName ().' $ ' .$ reflectionParam ->getName ()) {
84+ $ this ->normalizersAndEncoders [] = $ serviceId .'.alias ' ;
85+ break ;
86+ }
87+ }
88+ }
6989 }
7090}
7191
0 commit comments