@@ -52,7 +52,8 @@ public function testCreatingEnumWithInvalidValue($value)
5252 * Contains values not existing in EnumFixture
5353 * @return array
5454 */
55- public function invalidValueProvider () {
55+ public function invalidValueProvider ()
56+ {
5657 return array (
5758 "string " => array ('test ' ),
5859 "int " => array (1234 ),
@@ -68,7 +69,8 @@ public function testToString($expected, $enumObject)
6869 $ this ->assertSame ($ expected , (string ) $ enumObject );
6970 }
7071
71- public function toStringProvider () {
72+ public function toStringProvider ()
73+ {
7274 return array (
7375 array (EnumFixture::FOO , new EnumFixture (EnumFixture::FOO )),
7476 array (EnumFixture::BAR , new EnumFixture (EnumFixture::BAR )),
@@ -162,22 +164,23 @@ public function testIsValid($value, $isValid)
162164 $ this ->assertSame ($ isValid , EnumFixture::isValid ($ value ));
163165 }
164166
165- public function isValidProvider () {
166- return array (
167+ public function isValidProvider ()
168+ {
169+ return [
167170 /**
168171 * Valid values
169172 */
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 ] ,
176179 /**
177180 * Invalid values
178181 */
179- array ( 'baz ' , false )
180- ) ;
182+ [ 'baz ' , false ]
183+ ] ;
181184 }
182185
183186 /**
@@ -187,6 +190,7 @@ public function testIsValidKey()
187190 {
188191 $ this ->assertTrue (EnumFixture::isValidKey ('FOO ' ));
189192 $ this ->assertFalse (EnumFixture::isValidKey ('BAZ ' ));
193+ $ this ->assertTrue (EnumFixture::isValidKey ('PROBLEMATIC_NULL ' ));
190194 }
191195
192196 /**
@@ -199,7 +203,8 @@ public function testSearch($value, $expected)
199203 $ this ->assertSame ($ expected , EnumFixture::search ($ value ));
200204 }
201205
202- public function searchProvider () {
206+ public function searchProvider ()
207+ {
203208 return array (
204209 array ('foo ' , 'FOO ' ),
205210 array (0 , 'PROBLEMATIC_NUMBER ' ),
@@ -219,11 +224,15 @@ public function testEquals()
219224 $ foo = new EnumFixture (EnumFixture::FOO );
220225 $ number = new EnumFixture (EnumFixture::NUMBER );
221226 $ anotherFoo = new EnumFixture (EnumFixture::FOO );
227+ $ objectOfDifferentClass = new \stdClass ();
228+ $ notAnObject = 'foo ' ;
222229
223230 $ this ->assertTrue ($ foo ->equals ($ foo ));
224231 $ this ->assertFalse ($ foo ->equals ($ number ));
225232 $ this ->assertTrue ($ foo ->equals ($ anotherFoo ));
226233 $ this ->assertFalse ($ foo ->equals (null ));
234+ $ this ->assertFalse ($ foo ->equals ($ objectOfDifferentClass ));
235+ $ this ->assertFalse ($ foo ->equals ($ notAnObject ));
227236 }
228237
229238 /**
@@ -254,12 +263,61 @@ public function testEqualsConflictValues()
254263 */
255264 public function testJsonSerialize ()
256265 {
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 ));
264322 }
265323}
0 commit comments