1111
1212namespace Symfony \Component \Serializer \Tests \Normalizer ;
1313
14- use PHPUnit \Framework \MockObject \MockObject ;
1514use PHPUnit \Framework \TestCase ;
1615use Symfony \Component \PropertyInfo \Extractor \PhpDocExtractor ;
1716use Symfony \Component \PropertyInfo \Extractor \ReflectionExtractor ;
3433use Symfony \Component \Serializer \Tests \Fixtures \Attributes \ClassWithIgnoreAttribute ;
3534use Symfony \Component \Serializer \Tests \Fixtures \Attributes \GroupDummy ;
3635use Symfony \Component \Serializer \Tests \Fixtures \CircularReferenceDummy ;
36+ use Symfony \Component \Serializer \Tests \Fixtures \ScalarNormalizer ;
3737use Symfony \Component \Serializer \Tests \Fixtures \SiblingHolder ;
38+ use Symfony \Component \Serializer \Tests \Fixtures \StdClassNormalizer ;
3839use Symfony \Component \Serializer \Tests \Normalizer \Features \CacheableObjectAttributesTestTrait ;
3940use Symfony \Component \Serializer \Tests \Normalizer \Features \CallbacksTestTrait ;
4041use Symfony \Component \Serializer \Tests \Normalizer \Features \CircularReferenceTestTrait ;
@@ -61,7 +62,7 @@ class GetSetMethodNormalizerTest extends TestCase
6162 use TypeEnforcementTestTrait;
6263
6364 private GetSetMethodNormalizer $ normalizer ;
64- private SerializerInterface &NormalizerInterface & MockObject $ serializer ;
65+ private SerializerInterface &NormalizerInterface $ serializer ;
6566
6667 protected function setUp (): void
6768 {
@@ -70,8 +71,8 @@ protected function setUp(): void
7071
7172 private function createNormalizer (array $ defaultContext = []): void
7273 {
73- $ this ->serializer = $ this ->createMock (SerializerNormalizer::class);
7474 $ this ->normalizer = new GetSetMethodNormalizer (null , null , null , null , null , $ defaultContext );
75+ $ this ->serializer = new Serializer ([$ this ->normalizer , new StdClassNormalizer ()]);
7576 $ this ->normalizer ->setSerializer ($ this ->serializer );
7677 }
7778
@@ -91,13 +92,6 @@ public function testNormalize()
9192 $ obj ->setCamelCase ('camelcase ' );
9293 $ obj ->setObject ($ object );
9394
94- $ this ->serializer
95- ->expects ($ this ->once ())
96- ->method ('normalize ' )
97- ->with ($ object , 'any ' )
98- ->willReturn ('string_object ' )
99- ;
100-
10195 $ this ->assertEquals (
10296 [
10397 'foo ' => 'foo ' ,
@@ -111,6 +105,29 @@ public function testNormalize()
111105 );
112106 }
113107
108+ public function testNormalizeWithoutSerializer ()
109+ {
110+ $ obj = new GetSetDummy ();
111+ $ obj ->setFoo ('foo ' );
112+ $ obj ->setBar ('bar ' );
113+ $ obj ->setBaz (true );
114+ $ obj ->setCamelCase ('camelcase ' );
115+
116+ $ this ->normalizer = new GetSetMethodNormalizer ();
117+
118+ $ this ->assertEquals (
119+ [
120+ 'foo ' => 'foo ' ,
121+ 'bar ' => 'bar ' ,
122+ 'baz ' => true ,
123+ 'fooBar ' => 'foobar ' ,
124+ 'camelCase ' => 'camelcase ' ,
125+ 'object ' => null ,
126+ ],
127+ $ this ->normalizer ->normalize ($ obj , 'any ' )
128+ );
129+ }
130+
114131 public function testDenormalize ()
115132 {
116133 $ obj = $ this ->normalizer ->denormalize (
@@ -377,8 +394,7 @@ public function testUnableToNormalizeObjectAttribute()
377394 {
378395 $ this ->expectException (LogicException::class);
379396 $ this ->expectExceptionMessage ('Cannot normalize attribute "object" because the injected serializer is not a normalizer ' );
380- $ serializer = $ this ->createMock (SerializerInterface::class);
381- $ this ->normalizer ->setSerializer ($ serializer );
397+ $ this ->normalizer ->setSerializer ($ this ->createMock (SerializerInterface::class));
382398
383399 $ obj = new GetSetDummy ();
384400 $ object = new \stdClass ();
@@ -523,6 +539,30 @@ public function testNormalizeWithMethodNamesSimilarToAccessors()
523539 $ this ->assertSame (['class ' => 'class ' , 123 => 123 ], $ normalizer ->normalize (new GetSetWithAccessorishMethod ()));
524540 }
525541
542+ public function testNormalizeWithScalarValueNormalizer ()
543+ {
544+ $ normalizer = new GetSetMethodNormalizer ();
545+ $ normalizer ->setSerializer (new Serializer ([$ normalizer , new ScalarNormalizer ()]));
546+
547+ $ obj = new GetSetDummy ();
548+ $ obj ->setFoo ('foo ' );
549+ $ obj ->setBar (10 );
550+ $ obj ->setBaz (true );
551+ $ obj ->setCamelCase ('camelcase ' );
552+
553+ $ this ->assertSame (
554+ [
555+ 'foo ' => 'FOO ' ,
556+ 'bar ' => '10 ' ,
557+ 'baz ' => '1 ' ,
558+ 'fooBar ' => 'FOO10 ' ,
559+ 'camelCase ' => 'CAMELCASE ' ,
560+ 'object ' => null ,
561+ ],
562+ $ normalizer ->normalize ($ obj , 'any ' )
563+ );
564+ }
565+
526566 public function testDenormalizeWithDiscriminator ()
527567 {
528568 $ classMetadataFactory = new ClassMetadataFactory (new AttributeLoader ());
@@ -701,10 +741,6 @@ public function otherMethod()
701741 }
702742}
703743
704- abstract class SerializerNormalizer implements SerializerInterface, NormalizerInterface
705- {
706- }
707-
708744class GetConstructorOptionalArgsDummy
709745{
710746 protected $ foo ;
0 commit comments