@@ -32,4 +32,45 @@ public function testEnumMapping()
32
32
$ this ->assertSame (\Enums \StringBackedEnum::FOO , $ sn ->stringBackedEnum );
33
33
$ this ->assertSame (\Enums \IntBackedEnum::BAR , $ sn ->intBackedEnum );
34
34
}
35
+
36
+ /**
37
+ * Test that string values are correctly mapped to backed enum properties.
38
+ */
39
+ public function testBackedEnumPropertyIsMappedFromString (): void
40
+ {
41
+ $ json = (object ) [
42
+ 'stringBackedEnum ' => 'foo ' ,
43
+ 'intBackedEnum ' => 2 ,
44
+ ];
45
+
46
+ $ mapper = new \JsonMapper ();
47
+ $ target = new \Enums \ObjectWithEnum ();
48
+
49
+ $ mapped = $ mapper ->map ($ json , $ target );
50
+
51
+ $ this ->assertSame (
52
+ \Enums \StringBackedEnum::FOO ,
53
+ $ mapped ->stringBackedEnum ,
54
+ 'Expected JSON scalar to be converted to the corresponding backed enum case '
55
+ );
56
+ }
57
+
58
+ /**
59
+ * Test that mapping invalid string values to backed enum properties throws an exception.
60
+ */
61
+ public function testBackedEnumPropertyWithInvalidStringThrowsJsonMapperException (): void
62
+ {
63
+ $ json = (object ) [
64
+ 'stringBackedEnum ' => 'not-a-valid-enum-value ' ,
65
+ 'intBackedEnum ' => 'not-a-valid-enum-value ' ,
66
+ ];
67
+
68
+ $ mapper = new \JsonMapper ();
69
+ $ target = new \Enums \ObjectWithEnum ();
70
+
71
+ $ this ->expectException (\JsonMapper_Exception::class);
72
+ $ this ->expectExceptionMessage ('Enum value "not-a-valid-enum-value" does not belong to ' );
73
+
74
+ $ mapper ->map ($ json , $ target );
75
+ }
35
76
}
0 commit comments