@@ -52,7 +52,8 @@ public function testCreatingEnumWithInvalidValue($value)
52
52
* Contains values not existing in EnumFixture
53
53
* @return array
54
54
*/
55
- public function invalidValueProvider () {
55
+ public function invalidValueProvider ()
56
+ {
56
57
return array (
57
58
"string " => array ('test ' ),
58
59
"int " => array (1234 ),
@@ -68,7 +69,8 @@ public function testToString($expected, $enumObject)
68
69
$ this ->assertSame ($ expected , (string ) $ enumObject );
69
70
}
70
71
71
- public function toStringProvider () {
72
+ public function toStringProvider ()
73
+ {
72
74
return array (
73
75
array (EnumFixture::FOO , new EnumFixture (EnumFixture::FOO )),
74
76
array (EnumFixture::BAR , new EnumFixture (EnumFixture::BAR )),
@@ -162,22 +164,23 @@ public function testIsValid($value, $isValid)
162
164
$ this ->assertSame ($ isValid , EnumFixture::isValid ($ value ));
163
165
}
164
166
165
- public function isValidProvider () {
166
- return array (
167
+ public function isValidProvider ()
168
+ {
169
+ return [
167
170
/**
168
171
* Valid values
169
172
*/
170
- array ( 'foo ' , true ) ,
171
- array ( 42 , true ) ,
172
- array ( null , true ) ,
173
- array ( 0 , true ) ,
174
- array ( '' , true ) ,
175
- array ( false , true ) ,
173
+ [ 'foo ' , true ] ,
174
+ [ 42 , true ] ,
175
+ [ null , true ] ,
176
+ [ 0 , true ] ,
177
+ [ '' , true ] ,
178
+ [ false , true ] ,
176
179
/**
177
180
* Invalid values
178
181
*/
179
- array ( 'baz ' , false )
180
- ) ;
182
+ [ 'baz ' , false ]
183
+ ] ;
181
184
}
182
185
183
186
/**
@@ -187,6 +190,7 @@ public function testIsValidKey()
187
190
{
188
191
$ this ->assertTrue (EnumFixture::isValidKey ('FOO ' ));
189
192
$ this ->assertFalse (EnumFixture::isValidKey ('BAZ ' ));
193
+ $ this ->assertTrue (EnumFixture::isValidKey ('PROBLEMATIC_NULL ' ));
190
194
}
191
195
192
196
/**
@@ -199,7 +203,8 @@ public function testSearch($value, $expected)
199
203
$ this ->assertSame ($ expected , EnumFixture::search ($ value ));
200
204
}
201
205
202
- public function searchProvider () {
206
+ public function searchProvider ()
207
+ {
203
208
return array (
204
209
array ('foo ' , 'FOO ' ),
205
210
array (0 , 'PROBLEMATIC_NUMBER ' ),
@@ -219,11 +224,15 @@ public function testEquals()
219
224
$ foo = new EnumFixture (EnumFixture::FOO );
220
225
$ number = new EnumFixture (EnumFixture::NUMBER );
221
226
$ anotherFoo = new EnumFixture (EnumFixture::FOO );
227
+ $ objectOfDifferentClass = new \stdClass ();
228
+ $ notAnObject = 'foo ' ;
222
229
223
230
$ this ->assertTrue ($ foo ->equals ($ foo ));
224
231
$ this ->assertFalse ($ foo ->equals ($ number ));
225
232
$ this ->assertTrue ($ foo ->equals ($ anotherFoo ));
226
233
$ this ->assertFalse ($ foo ->equals (null ));
234
+ $ this ->assertFalse ($ foo ->equals ($ objectOfDifferentClass ));
235
+ $ this ->assertFalse ($ foo ->equals ($ notAnObject ));
227
236
}
228
237
229
238
/**
@@ -254,12 +263,61 @@ public function testEqualsConflictValues()
254
263
*/
255
264
public function testJsonSerialize ()
256
265
{
257
- $ this ->assertJsonStringEqualsJsonString ('"foo" ' , json_encode (new EnumFixture (EnumFixture::FOO )));
258
- $ this ->assertJsonStringEqualsJsonString ('"bar" ' , json_encode (new EnumFixture (EnumFixture::BAR )));
259
- $ this ->assertJsonStringEqualsJsonString ('42 ' , json_encode (new EnumFixture (EnumFixture::NUMBER )));
260
- $ this ->assertJsonStringEqualsJsonString ('0 ' , json_encode (new EnumFixture (EnumFixture::PROBLEMATIC_NUMBER )));
261
- $ this ->assertJsonStringEqualsJsonString ('null ' , json_encode (new EnumFixture (EnumFixture::PROBLEMATIC_NULL )));
262
- $ this ->assertJsonStringEqualsJsonString ('"" ' , json_encode (new EnumFixture (EnumFixture::PROBLEMATIC_EMPTY_STRING )));
263
- $ this ->assertJsonStringEqualsJsonString ('false ' , json_encode (new EnumFixture (EnumFixture::PROBLEMATIC_BOOLEAN_FALSE )));
266
+ $ this ->assertJsonEqualsJson ('"foo" ' , json_encode (new EnumFixture (EnumFixture::FOO )));
267
+ $ this ->assertJsonEqualsJson ('"bar" ' , json_encode (new EnumFixture (EnumFixture::BAR )));
268
+ $ this ->assertJsonEqualsJson ('42 ' , json_encode (new EnumFixture (EnumFixture::NUMBER )));
269
+ $ this ->assertJsonEqualsJson ('0 ' , json_encode (new EnumFixture (EnumFixture::PROBLEMATIC_NUMBER )));
270
+ $ this ->assertJsonEqualsJson ('null ' , json_encode (new EnumFixture (EnumFixture::PROBLEMATIC_NULL )));
271
+ $ this ->assertJsonEqualsJson ('"" ' , json_encode (new EnumFixture (EnumFixture::PROBLEMATIC_EMPTY_STRING )));
272
+ $ this ->assertJsonEqualsJson ('false ' , json_encode (new EnumFixture (EnumFixture::PROBLEMATIC_BOOLEAN_FALSE )));
273
+ }
274
+
275
+ public function testNullableEnum ()
276
+ {
277
+ $ this ->assertNull (EnumFixture::PROBLEMATIC_NULL ()->getValue ());
278
+ $ this ->assertNull ((new EnumFixture (EnumFixture::PROBLEMATIC_NULL ))->getValue ());
279
+ $ this ->assertNull ((new EnumFixture (EnumFixture::PROBLEMATIC_NULL ))->jsonSerialize ());
280
+ }
281
+
282
+ public function testBooleanEnum ()
283
+ {
284
+ $ this ->assertFalse (EnumFixture::PROBLEMATIC_BOOLEAN_FALSE ()->getValue ());
285
+ $ this ->assertFalse ((new EnumFixture (EnumFixture::PROBLEMATIC_BOOLEAN_FALSE ))->jsonSerialize ());
286
+ }
287
+
288
+ public function testConstructWithSameEnumArgument ()
289
+ {
290
+ $ enum = new EnumFixture (EnumFixture::FOO );
291
+
292
+ $ enveloped = new EnumFixture ($ enum );
293
+
294
+ $ this ->assertEquals ($ enum , $ enveloped );
295
+ }
296
+
297
+ private function assertJsonEqualsJson ($ json1 , $ json2 )
298
+ {
299
+ $ this ->assertJsonStringEqualsJsonString ($ json1 , $ json2 );
300
+ }
301
+
302
+ public function testSerialize ()
303
+ {
304
+ // split string for Pretty CI: "Line exceeds 120 characters"
305
+ $ bin = '4f3a33303a224d79434c6162735c54657374735c456e756d5c456e756d4669787 ' .
306
+ '4757265223a313a7b733a383a22002a0076616c7565223b733a333a22666f6f223b7d ' ;
307
+
308
+ $ this ->assertEquals ($ bin , bin2hex (serialize (EnumFixture::FOO ())));
309
+ }
310
+
311
+ public function testUnserialize ()
312
+ {
313
+ // split string for Pretty CI: "Line exceeds 120 characters"
314
+ $ bin = '4f3a33303a224d79434c6162735c54657374735c456e756d5c456e756d4669787 ' .
315
+ '4757265223a313a7b733a383a22002a0076616c7565223b733a333a22666f6f223b7d ' ;
316
+
317
+ /* @var $value EnumFixture */
318
+ $ value = unserialize (pack ('H* ' , $ bin ));
319
+
320
+ $ this ->assertEquals (EnumFixture::FOO , $ value ->getValue ());
321
+ $ this ->assertTrue (EnumFixture::FOO ()->equals ($ value ));
264
322
}
265
323
}
0 commit comments