1111
1212namespace Symfony \Component \Serializer \Tests \Encoder ;
1313
14- use PHPUnit \Framework \MockObject \MockObject ;
1514use PHPUnit \Framework \TestCase ;
1615use Symfony \Component \Serializer \Encoder \ChainDecoder ;
1716use Symfony \Component \Serializer \Encoder \ContextAwareDecoderInterface ;
@@ -24,72 +23,90 @@ class ChainDecoderTest extends TestCase
2423 private const FORMAT_2 = 'format2 ' ;
2524 private const FORMAT_3 = 'format3 ' ;
2625
27- private ChainDecoder $ chainDecoder ;
28- private MockObject &ContextAwareDecoderInterface $ decoder1 ;
29- private MockObject &DecoderInterface $ decoder2 ;
30-
31- protected function setUp (): void
32- {
33- $ this ->decoder1 = $ this ->createMock (ContextAwareDecoderInterface::class);
34- $ this ->decoder1
35- ->method ('supportsDecoding ' )
36- ->willReturnMap ([
37- [self ::FORMAT_1 , [], true ],
38- [self ::FORMAT_2 , [], false ],
39- [self ::FORMAT_3 , [], false ],
40- [self ::FORMAT_3 , ['foo ' => 'bar ' ], true ],
41- [self ::FORMAT_3 , ['foo ' => 'bar2 ' ], false ],
42- ]);
43-
44- $ this ->decoder2 = $ this ->createMock (DecoderInterface::class);
45- $ this ->decoder2
46- ->method ('supportsDecoding ' )
47- ->willReturnMap ([
48- [self ::FORMAT_1 , [], false ],
49- [self ::FORMAT_2 , [], true ],
50- [self ::FORMAT_3 , [], false ],
51- [self ::FORMAT_3 , ['foo ' => 'bar ' ], false ],
52- [self ::FORMAT_3 , ['foo ' => 'bar2 ' ], true ],
53- ]);
54-
55- $ this ->chainDecoder = new ChainDecoder ([$ this ->decoder1 , $ this ->decoder2 ]);
56- }
57-
5826 public function testSupportsDecoding ()
5927 {
60- $ this ->decoder1
28+ $ decoder1 = $ this ->createDecoder1 ();
29+ $ decoder1
6130 ->method ('decode ' )
6231 ->willReturn ('result1 ' );
63- $ this ->decoder2
32+ $ decoder2 = $ this ->createDecoder2 ();
33+ $ decoder2
6434 ->method ('decode ' )
6535 ->willReturn ('result2 ' );
36+ $ chainDecoder = new ChainDecoder ([$ decoder1 , $ decoder2 ]);
6637
67- $ this ->assertTrue ($ this -> chainDecoder ->supportsDecoding (self ::FORMAT_1 ));
68- $ this ->assertEquals ('result1 ' , $ this -> chainDecoder ->decode ('' , self ::FORMAT_1 , []));
38+ $ this ->assertTrue ($ chainDecoder ->supportsDecoding (self ::FORMAT_1 ));
39+ $ this ->assertEquals ('result1 ' , $ chainDecoder ->decode ('' , self ::FORMAT_1 , []));
6940
70- $ this ->assertTrue ($ this -> chainDecoder ->supportsDecoding (self ::FORMAT_2 ));
71- $ this ->assertEquals ('result2 ' , $ this -> chainDecoder ->decode ('' , self ::FORMAT_2 , []));
41+ $ this ->assertTrue ($ chainDecoder ->supportsDecoding (self ::FORMAT_2 ));
42+ $ this ->assertEquals ('result2 ' , $ chainDecoder ->decode ('' , self ::FORMAT_2 , []));
7243
73- $ this ->assertFalse ($ this -> chainDecoder ->supportsDecoding (self ::FORMAT_3 ));
44+ $ this ->assertFalse ($ chainDecoder ->supportsDecoding (self ::FORMAT_3 ));
7445
75- $ this ->assertTrue ($ this -> chainDecoder ->supportsDecoding (self ::FORMAT_3 , ['foo ' => 'bar ' ]));
76- $ this ->assertEquals ('result1 ' , $ this -> chainDecoder ->decode ('' , self ::FORMAT_3 , ['foo ' => 'bar ' ]));
46+ $ this ->assertTrue ($ chainDecoder ->supportsDecoding (self ::FORMAT_3 , ['foo ' => 'bar ' ]));
47+ $ this ->assertEquals ('result1 ' , $ chainDecoder ->decode ('' , self ::FORMAT_3 , ['foo ' => 'bar ' ]));
7748
78- $ this ->assertTrue ($ this -> chainDecoder ->supportsDecoding (self ::FORMAT_3 , ['foo ' => 'bar2 ' ]));
79- $ this ->assertEquals ('result2 ' , $ this -> chainDecoder ->decode ('' , self ::FORMAT_3 , ['foo ' => 'bar2 ' ]));
49+ $ this ->assertTrue ($ chainDecoder ->supportsDecoding (self ::FORMAT_3 , ['foo ' => 'bar2 ' ]));
50+ $ this ->assertEquals ('result2 ' , $ chainDecoder ->decode ('' , self ::FORMAT_3 , ['foo ' => 'bar2 ' ]));
8051 }
8152
8253 public function testDecode ()
8354 {
84- $ this ->decoder1 ->expects ($ this ->never ())->method ('decode ' );
85- $ this ->decoder2 ->expects ($ this ->once ())->method ('decode ' );
55+ $ decoder1 = $ this ->createDecoder1 (true );
56+ $ decoder1 ->expects ($ this ->never ())->method ('decode ' );
57+ $ decoder2 = $ this ->createDecoder2 (true );
58+ $ decoder2 ->expects ($ this ->once ())->method ('decode ' );
59+ $ chainDecoder = new ChainDecoder ([$ decoder1 , $ decoder2 ]);
8660
87- $ this -> chainDecoder ->decode ('string_to_decode ' , self ::FORMAT_2 );
61+ $ chainDecoder ->decode ('string_to_decode ' , self ::FORMAT_2 );
8862 }
8963
9064 public function testDecodeUnsupportedFormat ()
9165 {
66+ $ chainDecoder = new ChainDecoder ([$ this ->createDecoder1 (), $ this ->createDecoder2 ()]);
9267 $ this ->expectException (RuntimeException::class);
93- $ this ->chainDecoder ->decode ('string_to_decode ' , self ::FORMAT_3 );
68+ $ chainDecoder ->decode ('string_to_decode ' , self ::FORMAT_3 );
69+ }
70+
71+ private function createDecoder1 (bool $ mock = false ): DecoderInterface
72+ {
73+ if ($ mock ) {
74+ $ decoder = $ this ->createMock (ContextAwareDecoderInterface::class);
75+ } else {
76+ $ decoder = $ this ->createStub (ContextAwareDecoderInterface::class);
77+ }
78+
79+ $ decoder
80+ ->method ('supportsDecoding ' )
81+ ->willReturnMap ([
82+ [self ::FORMAT_1 , [], true ],
83+ [self ::FORMAT_2 , [], false ],
84+ [self ::FORMAT_3 , [], false ],
85+ [self ::FORMAT_3 , ['foo ' => 'bar ' ], true ],
86+ [self ::FORMAT_3 , ['foo ' => 'bar2 ' ], false ],
87+ ]);
88+
89+ return $ decoder ;
90+ }
91+
92+ private function createDecoder2 (bool $ mock = false ): DecoderInterface
93+ {
94+ if ($ mock ) {
95+ $ decoder = $ this ->createMock (DecoderInterface::class);
96+ } else {
97+ $ decoder = $ this ->createStub (DecoderInterface::class);
98+ }
99+
100+ $ decoder
101+ ->method ('supportsDecoding ' )
102+ ->willReturnMap ([
103+ [self ::FORMAT_1 , [], false ],
104+ [self ::FORMAT_2 , [], true ],
105+ [self ::FORMAT_3 , [], false ],
106+ [self ::FORMAT_3 , ['foo ' => 'bar ' ], false ],
107+ [self ::FORMAT_3 , ['foo ' => 'bar2 ' ], true ],
108+ ]);
109+
110+ return $ decoder ;
94111 }
95112}
0 commit comments