Skip to content

Commit 5f1eb72

Browse files
authored
Adds tests regarding backed enums
1 parent b70c4d2 commit 5f1eb72

File tree

1 file changed

+46
-21
lines changed

1 file changed

+46
-21
lines changed

tests/SerializerPhp81Test.php

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@
33
use Tests\Fixtures\Model;
44
use Tests\Fixtures\ModelAttribute;
55

6-
enum SerializerGlobalEnum {
7-
case Admin;
8-
case Guest;
9-
case Moderator;
10-
}
11-
126
test('enums', function () {
137
$f = function (SerializerGlobalEnum $role) {
148
return $role;
@@ -57,11 +51,16 @@ enum SerializerScopedEnum {
5751
expect($f())->toBe(SerializerScopedEnum::Admin);
5852
})->with('serializers');
5953

60-
enum SerializerGlobalBackedEnum: string {
61-
case Admin = 'Administrator';
62-
case Guest = 'Guest';
63-
case Moderator = 'Moderator';
64-
}
54+
test('enums properties', function () {
55+
$object = new ClassWithEnumProperty();
56+
$f = $object->getClosure();
57+
58+
$f = s($f);
59+
60+
expect($f())
61+
->name->toBe('Admin')
62+
->value->toBeNull();
63+
})->with('serializers');
6564

6665
test('backed enums', function () {
6766
$f = function (SerializerGlobalBackedEnum $role) {
@@ -112,6 +111,17 @@ enum SerializerScopedBackedEnum: string {
112111
expect($f())->toBe(SerializerScopedBackedEnum::Admin);
113112
})->with('serializers');
114113

114+
test('backed enums properties', function () {
115+
$object = new ClassWithBackedEnumProperty();
116+
$f = $object->getClosure();
117+
118+
$f = s($f);
119+
120+
expect($f())
121+
->name->toBe('Admin')
122+
->value->toBe('Administrator');
123+
})->with('serializers');
124+
115125
test('array unpacking', function () {
116126
$f = function () {
117127
$array1 = ['a' => 1];
@@ -394,15 +404,6 @@ enum SerializerScopedBackedEnum: string {
394404
expect($f())->toBeInstanceOf(SerializerPhp81Service::class);
395405
})->with('serializers');
396406

397-
test('closure defined inside class with enum property', function () {
398-
$object = new ClassWithEnumField();
399-
$f = $object->getClosure();
400-
401-
$f = s($f);
402-
403-
expect($f()->name)->toBe('Admin');
404-
})->with('serializers');
405-
406407
interface SerializerPhp81HasId {}
407408
interface SerializerPhp81HasName {}
408409

@@ -477,6 +478,18 @@ public function getSelf(self $instance): self
477478
}
478479
}
479480

481+
enum SerializerGlobalEnum {
482+
case Admin;
483+
case Guest;
484+
case Moderator;
485+
}
486+
487+
enum SerializerGlobalBackedEnum: string {
488+
case Admin = 'Administrator';
489+
case Guest = 'Guest';
490+
case Moderator = 'Moderator';
491+
}
492+
480493
#[Attribute(Attribute::TARGET_METHOD|Attribute::TARGET_FUNCTION)]
481494
class MyAttribute
482495
{
@@ -486,7 +499,7 @@ public function __construct(public $string, public $model)
486499
}
487500
}
488501

489-
class ClassWithEnumField
502+
class ClassWithEnumProperty
490503
{
491504
public SerializerGlobalEnum $enum = SerializerGlobalEnum::Admin;
492505

@@ -497,3 +510,15 @@ public function getClosure()
497510
};
498511
}
499512
}
513+
514+
class ClassWithBackedEnumProperty
515+
{
516+
public SerializerGlobalBackedEnum $enum = SerializerGlobalBackedEnum::Admin;
517+
518+
public function getClosure()
519+
{
520+
return function () {
521+
return $this->enum;
522+
};
523+
}
524+
}

0 commit comments

Comments
 (0)