@@ -47,6 +47,30 @@ public function __construct(
4747 }
4848}
4949
50+ enum TicketPriority: string
51+ {
52+ case Low = 'low ' ;
53+ case High = 'high ' ;
54+ }
55+
56+ class Ticket implements BaseModel
57+ {
58+ /** @use SdkModel<array<string, mixed>> */
59+ use SdkModel;
60+
61+ #[Required(enum: TicketPriority::class)]
62+ public TicketPriority $ priority ;
63+
64+ /** @var list<TicketPriority> */
65+ #[Required(list: TicketPriority::class)]
66+ public array $ labels ;
67+
68+ public function __construct ()
69+ {
70+ $ this ->initialize ();
71+ }
72+ }
73+
5074/**
5175 * @internal
5276 *
@@ -141,4 +165,42 @@ public function testSerializeModelWithExplicitNull(): void
141165 json_encode ($ model )
142166 );
143167 }
168+
169+ #[Test]
170+ public function testScalarEnumCoercesToInstance (): void
171+ {
172+ $ model = Ticket::fromArray (['priority ' => 'low ' , 'labels ' => []]);
173+ $ this ->assertSame (TicketPriority::Low, $ model ->priority );
174+ }
175+
176+ #[Test]
177+ public function testListOfEnumCoercesElementsToInstances (): void
178+ {
179+ $ model = Ticket::fromArray (['priority ' => 'low ' , 'labels ' => ['low ' , 'high ' ]]);
180+ $ this ->assertCount (2 , $ model ->labels );
181+ $ this ->assertSame (TicketPriority::Low, $ model ->labels [0 ]);
182+ $ this ->assertSame (TicketPriority::High, $ model ->labels [1 ]);
183+ }
184+
185+ #[Test]
186+ public function testEnumInstancePassesThrough (): void
187+ {
188+ $ model = Ticket::fromArray (['priority ' => TicketPriority::High, 'labels ' => []]);
189+ $ this ->assertSame (TicketPriority::High, $ model ->priority );
190+ }
191+
192+ #[Test]
193+ public function testInvalidEnumScalarFallsBackToData (): void
194+ {
195+ $ model = Ticket::fromArray (['priority ' => 'urgent ' , 'labels ' => []]);
196+ $ this ->assertSame ('urgent ' , $ model ['priority ' ]);
197+ }
198+
199+ #[Test]
200+ public function testEnumWireFormatStableAcrossConstruction (): void
201+ {
202+ $ fromScalar = Ticket::fromArray (['priority ' => 'low ' , 'labels ' => ['high ' ]]);
203+ $ fromInstance = Ticket::fromArray (['priority ' => TicketPriority::Low, 'labels ' => [TicketPriority::High]]);
204+ $ this ->assertSame (json_encode ($ fromScalar ), json_encode ($ fromInstance ));
205+ }
144206}
0 commit comments