@@ -27,6 +27,20 @@ public function testSerializesSimpleClass()
27
27
$ this ->assertEquals ('{"identifier":"myid","category_name":"Clothes","data":{"name":null}} ' , $ c ->toJson ());
28
28
}
29
29
30
+ public function testNullableProperty ()
31
+ {
32
+ $ c = new Category ;
33
+ $ c ->nullableSchedule = null ;
34
+ $ this ->assertEquals ($ this ->comparableJson ('{
35
+ "identifier": "myid",
36
+ "category_name": "Clothes",
37
+ "data": {
38
+ "name": null
39
+ },
40
+ "nullable_schedule": null
41
+ } ' ), $ c ->toJson ());
42
+ }
43
+
30
44
public function testSerializeAcceptsJsonFlags ()
31
45
{
32
46
$ c = new Category ;
@@ -78,7 +92,7 @@ public function testSerializesClassAttributesRecursively()
78
92
{
79
93
$ c = new Category ;
80
94
$ c ->setSchedule (new Schedule (1 , 20 ));
81
- $ this ->assertEquals (json_encode ( json_decode ('{
95
+ $ this ->assertEquals ($ this -> comparableJson ('{
82
96
"identifier": "myid",
83
97
"category_name": "Clothes",
84
98
"data": {
@@ -88,7 +102,7 @@ public function testSerializesClassAttributesRecursively()
88
102
"schedule_start": 1,
89
103
"schedule_end": 20
90
104
}
91
- } ' )) , $ c ->toJson ());
105
+ } ' ), $ c ->toJson ());
92
106
}
93
107
94
108
public function testSerializesObjectArrays ()
@@ -97,7 +111,7 @@ public function testSerializesObjectArrays()
97
111
$ c ->setSchedule (new Schedule (1 , 20 ));
98
112
$ c ->setUpcoming ([new Schedule (1 , 20 ), new Schedule (30 , 40 )]);
99
113
$ c ->setNullable ();
100
- $ this ->assertEquals (json_encode ( json_decode ('{
114
+ $ this ->assertEquals ($ this -> comparableJson ('{
101
115
"identifier": "myid",
102
116
"category_name": "Clothes",
103
117
"data": {
@@ -118,14 +132,14 @@ public function testSerializesObjectArrays()
118
132
}
119
133
],
120
134
"nullable_schedules": null
121
- } ' )) , $ c ->toJson ());
135
+ } ' ), $ c ->toJson ());
122
136
}
123
137
124
138
public function testSerializesScalarArrays ()
125
139
{
126
140
$ c = new Category ;
127
141
$ c ->counts = [1 , 'abc ' , 678 ];
128
- $ this ->assertEquals (json_encode ( json_decode ('{
142
+ $ this ->assertEquals ($ this -> comparableJson ('{
129
143
"identifier": "myid",
130
144
"category_name": "Clothes",
131
145
"data": {
@@ -136,35 +150,35 @@ public function testSerializesScalarArrays()
136
150
"abc",
137
151
678
138
152
]
139
- } ' )) , $ c ->toJson ());
153
+ } ' ), $ c ->toJson ());
140
154
}
141
155
142
156
public function testSerializesWithNoName ()
143
157
{
144
158
$ c = new Category ;
145
159
$ c ->unnamed = 'bob ' ;
146
- $ this ->assertEquals (json_encode ( json_decode ('{
160
+ $ this ->assertEquals ($ this -> comparableJson ('{
147
161
"identifier": "myid",
148
162
"category_name": "Clothes",
149
163
"data": {
150
164
"name": null
151
165
},
152
166
"unnamed": "bob"
153
- } ' )) , $ c ->toJson ());
167
+ } ' ), $ c ->toJson ());
154
168
}
155
169
156
170
public function testPrivateProps ()
157
171
{
158
172
$ p = new Privateer ;
159
- $ this ->assertEquals (json_encode ( json_decode ('{
173
+ $ this ->assertEquals ($ this -> comparableJson ('{
160
174
"name": "Jenna"
161
- } ' )) , $ p ->toJson ());
175
+ } ' ), $ p ->toJson ());
162
176
}
163
177
164
178
public function testHashMaps ()
165
179
{
166
180
$ w = new Weekend ;
167
- $ this ->assertEquals (json_encode ( json_decode ('{
181
+ $ this ->assertEquals ($ this -> comparableJson ('{
168
182
"weekend": {
169
183
"sat": {
170
184
"schedule_start": 1,
@@ -175,13 +189,13 @@ public function testHashMaps()
175
189
"schedule_end": 4
176
190
}
177
191
}
178
- } ' )) , $ w ->toJson ());
192
+ } ' ), $ w ->toJson ());
179
193
}
180
194
181
195
public function testTraitProps ()
182
196
{
183
197
$ t = new Traitor ;
184
- $ this ->assertEquals (json_encode ( json_decode ( '{"secretly_working_for": "MI6"} ' ) ), $ t ->toJson ());
198
+ $ this ->assertEquals ($ this -> comparableJson ( '{"secretly_working_for": "MI6"} ' ), $ t ->toJson ());
185
199
}
186
200
187
201
public function testPolymorphicClass ()
@@ -206,7 +220,7 @@ public function testList()
206
220
];
207
221
208
222
$ jl = Schedule::toJsonList ($ l );
209
- $ this ->assertEquals (json_encode ( json_decode ('[
223
+ $ this ->assertEquals ($ this -> comparableJson ('[
210
224
{
211
225
"schedule_start": 1,
212
226
"schedule_end": 2
@@ -219,7 +233,7 @@ public function testList()
219
233
"schedule_start": 111,
220
234
"schedule_end": 222
221
235
}
222
- ] ' )) , $ jl );
236
+ ] ' ), $ jl );
223
237
}
224
238
225
239
public function testClassToScalar ()
@@ -285,7 +299,7 @@ public function testCollections()
285
299
286
300
$ data = Collector::fromJsonString ($ json );
287
301
288
- $ this ->assertEquals (json_encode ( json_decode ( $ json) ), $ data ->toJson ());
302
+ $ this ->assertEquals ($ this -> comparableJson ( $ json ), $ data ->toJson ());
289
303
}
290
304
291
305
public function testMissingParent ()
@@ -295,6 +309,11 @@ public function testMissingParent()
295
309
$ data = new Child ();
296
310
$ json = '{"identifier":null,"parent":{"id":null}} ' ;
297
311
298
- $ this ->assertEquals (json_encode (json_decode ($ json )), $ data ->toJson ());
312
+ $ this ->assertEquals ($ this ->comparableJson ($ json ), $ data ->toJson ());
313
+ }
314
+
315
+ protected function comparableJson (string $ json ) : string
316
+ {
317
+ return json_encode (json_decode ($ json ));
299
318
}
300
319
}
0 commit comments