33namespace Illuminate \Tests \JsonSchema ;
44
55use Illuminate \JsonSchema \JsonSchema ;
6+ use Illuminate \Tests \JsonSchema \Fixtures \Enums \IntBackedEnum ;
7+ use Illuminate \Tests \JsonSchema \Fixtures \Enums \StringBackedEnum ;
8+ use Illuminate \Tests \JsonSchema \Fixtures \Enums \UnitEnum ;
9+ use InvalidArgumentException ;
610use Opis \JsonSchema \Resolvers \SchemaResolver ;
711use Opis \JsonSchema \SchemaLoader ;
812use Opis \JsonSchema \Validator ;
913use Opis \Uri \Uri ;
1014use PHPUnit \Framework \Attributes \DataProvider ;
1115use PHPUnit \Framework \TestCase ;
16+ use stdClass ;
1217use Stringable ;
1318
1419class TypeTest extends TestCase
@@ -102,6 +107,33 @@ public function test_types_in_object_schema(): void
102107 $ this ->assertInstanceOf (JsonSchema::class, $ schema );
103108 }
104109
110+ public function test_throws_with_invalid_enum_string (): void
111+ {
112+ $ this ->expectException (InvalidArgumentException::class);
113+ $ this ->expectExceptionMessage ('The provided class must be a BackedEnum. ' );
114+ $ this ->expectExceptionCode (0 );
115+
116+ JsonSchema::string ()->enum ('NonExistentEnumClass ' );
117+ }
118+
119+ public function test_throws_with_not_an_enum_class (): void
120+ {
121+ $ this ->expectException (InvalidArgumentException::class);
122+ $ this ->expectExceptionMessage ('The provided class must be a BackedEnum. ' );
123+ $ this ->expectExceptionCode (0 );
124+
125+ JsonSchema::string ()->enum (stdClass::class);
126+ }
127+
128+ public function test_throws_with_unit_enum_class (): void
129+ {
130+ $ this ->expectException (InvalidArgumentException::class);
131+ $ this ->expectExceptionMessage ('The provided class must be a BackedEnum. ' );
132+ $ this ->expectExceptionCode (0 );
133+
134+ JsonSchema::string ()->enum (UnitEnum::class);
135+ }
136+
105137 public static function validSchemasProvider (): array
106138 {
107139 return [
@@ -118,6 +150,7 @@ public static function validSchemasProvider(): array
118150 [JsonSchema::string ()->min (1 )->max (3 ), 'a ' ], // boundary at min
119151 [JsonSchema::string ()->pattern ('^[A-Z]{2}[0-9]{2}$ ' ), 'AB12 ' ], // complex pattern
120152 [JsonSchema::string ()->enum (['' , 'x ' , 'y ' ]), '' ], // enum including empty string
153+ [JsonSchema::string ()->enum (StringBackedEnum::class), 'one ' ], // string backed enum cases
121154 [JsonSchema::string ()->nullable (), null ],
122155 [JsonSchema::string ()->nullable (false ), '' ],
123156
@@ -132,6 +165,7 @@ public static function validSchemasProvider(): array
132165 [JsonSchema::integer ()->max (10 ), 9 ], // below max
133166 [JsonSchema::integer ()->min (1 )->max (3 ), 3 ], // boundary at max
134167 [JsonSchema::integer ()->enum ([0 , -1 , 5 ]), 0 ], // enum with zero
168+ [JsonSchema::integer ()->enum (IntBackedEnum::class), 1 ], // integer backed enum cases
135169 [JsonSchema::integer ()->default (0 ), 0 ], // default value
136170 [JsonSchema::integer ()->nullable (), null ],
137171 [JsonSchema::integer ()->nullable (false ), 0 ],
@@ -265,6 +299,7 @@ public static function invalidSchemasProvider(): array
265299 [JsonSchema::string ()->max (0 ), 'a ' ], // too long for zero max
266300 [JsonSchema::string ()->pattern ('^[a]+$ ' ), 'ab ' ], // pattern mismatch
267301 [JsonSchema::string ()->enum (['a ' , 'b ' ]), 'A ' ], // case sensitive mismatch
302+ [JsonSchema::string ()->enum (StringBackedEnum::class), 'three ' ], // string backed enum cases mismatch
268303 [JsonSchema::string (), null ], // null not allowed
269304 [JsonSchema::string ()->nullable (false ), null ], // not nullable
270305
@@ -279,6 +314,7 @@ public static function invalidSchemasProvider(): array
279314 [JsonSchema::integer ()->max (0 ), 1 ], // above max boundary
280315 [JsonSchema::integer (), 3.14 ], // not an integer
281316 [JsonSchema::integer ()->enum ([1 , 2 ]), 2.5 ], // not in enum and not an integer
317+ [JsonSchema::integer ()->enum (IntBackedEnum::class), 3 ], // integer backed enum cases mismatch
282318 [JsonSchema::integer ()->default (1 ), null ], // wrong type
283319 [JsonSchema::integer ()->nullable (false ), null ], // not nullable
284320
0 commit comments