-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle null data when retrieving value #33
Changes from 4 commits
6bfae0b
f8b85bc
fda9bb1
5538283
cc0ee36
42d1435
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,9 @@ | |
|
||
final class DeSerializationTest extends TestCase | ||
{ | ||
|
||
const UNINITIALIZED = '@uninitialized'; | ||
|
||
public function export($value) | ||
{ | ||
if (is_null($value)) { | ||
|
@@ -53,7 +56,7 @@ public function export($value) | |
]; | ||
foreach ($rc->getProperties() as $prop) { | ||
$prop->setAccessible(true); | ||
$v = $prop->isInitialized($value) ? $prop->getValue($value) : null; | ||
$v = $prop->isInitialized($value) ? $prop->getValue($value) : self::UNINITIALIZED; | ||
$n = $prop->getName(); | ||
|
||
$data[$n] = $this->export($v); | ||
|
@@ -70,12 +73,12 @@ public function testDeserializesSimpleClass() | |
'id' => 'myid', | ||
'name' => 'Clothes', | ||
'data_name' => null, | ||
'schedule' => null, | ||
'schedule' => self::UNINITIALIZED, | ||
'nullableSchedule' => null, | ||
'schedules' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => null, | ||
'unnamed' => self::UNINITIALIZED, | ||
'untypedSchedule' => null, | ||
], $this->export($c)); | ||
} | ||
|
@@ -95,12 +98,12 @@ public function testNullableProperty() | |
'id' => 'myid', | ||
'name' => 'Clothes', | ||
'data_name' => null, | ||
'schedule' => null, | ||
'schedule' => self::UNINITIALIZED, | ||
'nullableSchedule' => null, | ||
'schedules' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => null, | ||
'unnamed' => self::UNINITIALIZED, | ||
'untypedSchedule' => null, | ||
], $this->export($c)); | ||
$this->assertNull($c->nullableSchedule); | ||
|
@@ -123,12 +126,12 @@ public function testOmitsEmptyValues() | |
'data_name' => null, | ||
'id' => 'myid', | ||
'name' => 'Clothes', | ||
'schedule' => null, | ||
'schedule' => self::UNINITIALIZED, | ||
'nullableSchedule' => null, | ||
'schedules' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => null, | ||
'unnamed' => self::UNINITIALIZED, | ||
'untypedSchedule' => null, | ||
], $this->export($bc)); | ||
} | ||
|
@@ -167,10 +170,10 @@ public function testSerializesClassAttributesRecursively() | |
'end' => 20, | ||
], | ||
'nullableSchedule' => null, | ||
'schedules' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => null, | ||
'unnamed' => self::UNINITIALIZED, | ||
'untypedSchedule' => null, | ||
], $this->export($c)); | ||
} | ||
|
@@ -224,7 +227,7 @@ public function testSerializesObjectArrays() | |
], | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => null, | ||
'unnamed' => self::UNINITIALIZED, | ||
'untypedSchedule' => null, | ||
], $this->export($c)); | ||
} | ||
|
@@ -248,16 +251,16 @@ public function testSerializesScalarArrays() | |
'id' => 'myid', | ||
'name' => 'Clothes', | ||
'data_name' => null, | ||
'schedule' => null, | ||
'schedule' => self::UNINITIALIZED, | ||
'nullableSchedule' => null, | ||
'schedules' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [ | ||
0 => 1, | ||
1 => 'abc', | ||
2 => 678, | ||
], | ||
'unnamed' => null, | ||
'unnamed' => self::UNINITIALIZED, | ||
'untypedSchedule' => null, | ||
], $this->export($c)); | ||
} | ||
|
@@ -278,9 +281,9 @@ public function testSerializesWithNoName() | |
'id' => 'myid', | ||
'name' => 'Clothes', | ||
'data_name' => null, | ||
'schedule' => null, | ||
'schedule' => self::UNINITIALIZED, | ||
'nullableSchedule' => null, | ||
'schedules' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => 'bob', | ||
|
@@ -308,9 +311,9 @@ public function testSerializesWithUntypedProp() | |
'id' => 'myid', | ||
'name' => 'Clothes', | ||
'data_name' => null, | ||
'schedule' => null, | ||
'schedule' => self::UNINITIALIZED, | ||
'nullableSchedule' => null, | ||
'schedules' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => 'bob', | ||
|
@@ -672,4 +675,40 @@ public function testUnionTypesUsingArrayAndCustomObject() | |
], | ||
]); | ||
} | ||
|
||
public function testNonNullableFieldWithNullValue() | ||
{ | ||
$c = Category::fromJsonString('{"next_schedule": null}'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only field that's being set in JSON is not being tested in the output There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh, it is - the |
||
$this->assertEquals([ | ||
'@class' => Category::class, | ||
'id' => null, | ||
'name' => null, | ||
'data_name' => null, | ||
'schedule' => self::UNINITIALIZED, // should stay uninitialized because it's a non-nullable field | ||
'nullableSchedule' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => self::UNINITIALIZED, | ||
'untypedSchedule' => null, | ||
], $this->export($c)); | ||
} | ||
|
||
public function testNullParentObjectForNestedPath() | ||
{ | ||
$c = Category::fromJsonString('{"data": null}'); | ||
$this->assertEquals([ | ||
'@class' => Category::class, | ||
'id' => null, | ||
'name' => null, | ||
'data_name' => null, // set to null because the parent object is null and it can be null | ||
'schedule' => self::UNINITIALIZED, | ||
'nullableSchedule' => null, | ||
'schedules' => self::UNINITIALIZED, | ||
'nullableSchedules' => null, | ||
'counts' => [], | ||
'unnamed' => self::UNINITIALIZED, | ||
'untypedSchedule' => null, | ||
], $this->export($c)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied the behaviour from the
Php81DeSerializationTest
class. I think it's better to be more explicit about the returned value so the tests are self-documenting