@@ -32,4 +32,46 @@ 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
+ /** @var self::ObjectWithEnum $mapped */
50
+ $ mapped = $ mapper ->map ($ json , $ target );
51
+
52
+ $ this ->assertSame (
53
+ \Enums \StringBackedEnum::FOO ,
54
+ $ mapped ->stringBackedEnum ,
55
+ 'Expected JSON scalar to be converted to the corresponding backed enum case '
56
+ );
57
+ }
58
+
59
+ /**
60
+ * Test that mapping invalid string values to backed enum properties throws an exception.
61
+ */
62
+ public function testBackedEnumPropertyWithInvalidStringThrowsJsonMapperException (): void
63
+ {
64
+ $ json = (object ) [
65
+ 'stringBackedEnum ' => 'not-a-valid-enum-value ' ,
66
+ 'intBackedEnum ' => 'not-a-valid-enum-value ' ,
67
+ ];
68
+
69
+ $ mapper = new \JsonMapper ();
70
+ $ target = new \Enums \ObjectWithEnum ();
71
+
72
+ $ this ->expectException (\JsonMapper_Exception::class);
73
+ $ this ->expectExceptionMessage ('Enum value "not-a-valid-enum-value" does not belong to ' );
74
+
75
+ $ mapper ->map ($ json , $ target );
76
+ }
35
77
}
0 commit comments