Symfony Unit Test Serialization Groups, DateTime normalizatino

Im serializeing an object that contains DateTimeImmutable fields (creadted_ad, updated_at).
When using a serialization object as dependecnyc injection it’s all good.
But then i’m writing an unit tests and my first problem is that serializer doesn’t see the @Groups (after serialization there are fields taht are restricted by @Group annotation,btw. its working well with symfony service serializer), another problem is with a DateTimeImmutable fields, it is using ObjectNormalizer instead of DateTimeNormalizer.
Beacuse i can’t get service from container in unit tests (not allowed anymore, Symfony 5.2) i am createing an instance of serializer in unit test, and clearly there are some differences with the one i created and the "symofny" build in serializer.

So what i do in unit test (basic serializer) is:

        $dbSignups = .... entities from database
        $normalizers = [
            new ObjectNormalizer(),
            new DateTimeNormalizer()
        ];

        $encoders = [
            new JsonEncoder()
        ];

        $this->serializer = new Serializer($normalizers, $encoders);

        $jsonData = $this->serializer->serialize($dbSignups, 'json', [
                'groups' => ['api_readds']
            ]
        );

After "symfony" serialization my data looks like:

[
   {
      "id":1,
      "firstName":"Fist Name Signup 0",
      "lastName":"Last Name 0",
      "email":"[email protected]",
      "phone":"12312125430",
      "zip":"3210",
      "streetname":"Street 0",
      "buildingnumber":"0",
      "door":"0",
      "floor":"3",
      "campaign":"campaign_0",
      "comment":"comment 0",
      "updatedAt":"2020-12-14T06:51:51.000Z",
      "createdAt":"2020-12-14T06:51:51.000Z",
      "notMappedFields":[
         
      ],
      "dataProvider":"API Importer"
   }
]

And using a serializer in unit test, after passing exactly the same data:

[
   {
      "id":1,
      "firstName":"Fist Name Signup 0",
      "notMappedFields":[
         
      ],
      "updatedAt":{
         "timezone":{
            "name":"UTC",
            "transitions":[
               {
                  "ts":-9223372036854775808,
                  "time":"-292277022657-01-27T08:29:52+0000",
                  "offset":0,
                  "isdst":false,
                  "abbr":"UTC"
               }
            ],
            "location":{
               "country_code":"??",
               "latitude":0,
               "longitude":0,
               "comments":""
            }
         },
         "offset":0,
         "timestamp":1607928711
      },
      "createdAt":{
         "timezone":{
            "name":"UTC",
            "transitions":[
               {
                  "ts":-9223372036854775808,
                  "time":"-292277022657-01-27T08:29:52+0000",
                  "offset":0,
                  "isdst":false,
                  "abbr":"UTC"
               }
            ],
            "location":{
               "country_code":"??",
               "latitude":0,
               "longitude":0,
               "comments":""
            }
         },
         "offset":0,
         "timestamp":1607928711
      },
      "email":"[email protected]",
      "lastName":"Last Name 0",
      "phone":"12312125430",
      "zip":"3210",
      "streetname":"Street 0",
      "buildingnumber":"0",
      "door":"0",
      "floor":"3",
      "campaign":"campaign_0",
      "fullName":"Fist Name Signup 0 Last Name 0",
      "exportedAt":null,
      "comment":"comment 0",
      "apiUser":{
         "id":1,
         "email":"[email protected]",
         "username":"api-importer-65c2-55f1-a516-6671485jsh8a",
         "roles":[
            "ROLE_API_IMPORTER"
         ],
         "password":"$argon2i$v=19$m=65536,t=4,p=1$WVpXSWhJSGh0aGF5Llh1dw$eAVMuthb99FErWfOdEacFd1fKV1e8ws63FwF5vEWsuc",
         "salt":null,
         "uuid":"api-importer-65c2-55f1-a516-6671485jsh8a",
         "createdAt":{
            "timezone":{
               "name":"UTC",
               "transitions":[
                  {
                     "ts":-9223372036854775808,
                     "time":"-292277022657-01-27T08:29:52+0000",
                     "offset":0,
                     "isdst":false,
                     "abbr":"UTC"
                  }
               ],
               "location":{
                  "country_code":"??",
                  "latitude":0,
                  "longitude":0,
                  "comments":""
               }
            },
            "offset":0,
            "timestamp":1607928710
         },
         "updatedAt":{
            "timezone":{
               "name":"UTC",
               "transitions":[
                  {
                     "ts":-9223372036854775808,
                     "time":"-292277022657-01-27T08:29:52+0000",
                     "offset":0,
                     "isdst":false,
                     "abbr":"UTC"
                  }
               ],
               "location":{
                  "country_code":"??",
                  "latitude":0,
                  "longitude":0,
                  "comments":""
               }
            },
            "offset":0,
            "timestamp":1607928710
         },
         "name":"API Importer",
         "__initializer__":null,
         "__cloner__":null,
         "__isInitialized__":true
      },
      "dataProvider":"API Importer"
   }
]

So as one can see the DateTime fields are not serialized properly and also there are some additional fields that shouldn`t be there because of @Groups definition.

My conclusion is that im missing something in my Unit Test Serilizer but after sitting on for a while I cant fid what it is.
Also , i know that i sould mock the serializer and return data i want but well, i just want to do it wit serializer and actually right now im really keen to know where is the problem, so let`s ust figure aout how to do it this way.

Cheers

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *