11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Tests \Functional ;
13
13
14
+ use Symfony \Bundle \FrameworkBundle \Tests \Functional \app \AppKernel ;
15
+ use Symfony \Component \DependencyInjection \Compiler \CompilerPassInterface ;
16
+ use Symfony \Component \DependencyInjection \ContainerBuilder ;
17
+
14
18
/**
15
19
* @author Kévin Dunglas <[email protected] >
16
20
*/
@@ -33,39 +37,55 @@ public function testDeserializeArrayOfObject()
33
37
$ this ->assertEquals ($ expected , $ result );
34
38
}
35
39
36
- /**
37
- * @dataProvider provideNormalizersAndEncodersWithDefaultContextOption
38
- */
39
- public function testNormalizersAndEncodersUseDefaultContextConfigOption (string $ normalizerId )
40
+ public function testNormalizersAndEncodersUseDefaultContextConfigOption ()
40
41
{
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 );
42
47
43
- $ normalizer = static ::getContainer ()->get ($ normalizerId );
48
+ $ reflectionObject = new \ReflectionObject ($ normalizerOrEncoder );
49
+ $ property = $ reflectionObject ->getProperty ('defaultContext ' );
50
+ $ property ->setAccessible (true );
44
51
45
- $ reflectionObject = new \ReflectionObject ($ normalizer );
46
- $ property = $ reflectionObject ->getProperty ('defaultContext ' );
47
- $ property ->setAccessible (true );
52
+ $ defaultContext = $ property ->getValue ($ normalizerOrEncoder );
48
53
49
- $ defaultContext = $ property ->getValue ($ normalizer );
54
+ self ::assertArrayHasKey ('fake_context_option ' , $ defaultContext );
55
+ self ::assertEquals ('foo ' , $ defaultContext ['fake_context_option ' ]);
56
+ }
57
+ }
50
58
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;
53
62
}
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
+ ];
54
70
55
- public static function provideNormalizersAndEncodersWithDefaultContextOption (): array
71
+ public function process ( ContainerBuilder $ container )
56
72
{
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
+ }
69
89
}
70
90
}
71
91
0 commit comments