public function testSerializeWithNullValue() { $city = new City(); $city->setName('Palaiseau'); $city->countryIs(null); $configuration = new Configuration(); $configurationObject = new Configuration\Object(new ClassName(City::class)); $configurationObject->attributeUseMethod('name', 'setName', 'getName')->attributeUseObject('country', new ClassName(Country::class), 'countryIs', 'getCountry')->registerToConfiguration($configuration); $configurationObject = new Configuration\Object(new ClassName(Country::class)); $configurationObject->attributeUseMethod('name', 'setName', 'getName')->registerToConfiguration($configuration); $this->given($this->newTestedInstance($configuration))->string($this->testedInstance->serialize($city))->isIdenticalTo('{"name":"Palaiseau"}'); }
public function testDeserializeWithTypeHandler() { $city = new City(); $city->setName('Palaiseau'); $country = new Country(); $country->setName('France'); $city->countryIs($country); $configuration = new Configuration(); $configurationObject = new Configuration\Object(new ClassName(City::class)); $configurationObject->attributeUseMethod('name', 'setName', 'getName')->attributeUseHandler('country', new class implements Configuration\Type\HandlerDeserializerInterface { public function deserialize($object, $value) { $country = new Country(); $country->setName(str_replace(' (Handler please remove me)', '', $value['name'])); $object->countryIs($country); $object->setName(str_replace(' (Handler please remove me if i have a country)', '', $object->getName())); } }, new class implements Configuration\Type\HandlerSerializerInterface { public function serialize($object) { $country = $object->getCountry(); return ['name' => $country->getName() . ' (with Handler)']; } })->registerToConfiguration($configuration); $configurationObject = new Configuration\Object(new ClassName(Country::class)); $configurationObject->attributeUseMethod('name', 'setName', 'getName')->registerToConfiguration($configuration); $this->given($this->newTestedInstance($configuration))->object($this->testedInstance->deserialize('{"name":"Palaiseau (Handler please remove me if i have a country)","country":{"name":"France (Handler please remove me)"}}', new ClassName(City::class)))->isCloneOf($city); }