Skip to content

Commit 8e8ba79

Browse files
ReflectionClass: test enum output
In preparation for improving it, GH-15766
1 parent 1b9568d commit 8e8ba79

File tree

3 files changed

+399
-0
lines changed

3 files changed

+399
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
--TEST--
2+
ReflectionEnum::__toString() (larger case, int-backed)
3+
--FILE--
4+
<?php
5+
6+
interface MyStringable {
7+
public function toString(): string;
8+
}
9+
10+
enum MyBool: int implements MyStringable {
11+
case MyFalse = 0;
12+
case MyTrue = 1;
13+
14+
public const MyBool OtherTrue = MyBool::MyTrue;
15+
16+
public function toString(): string {
17+
return $this->name . " = " . $this->value;
18+
}
19+
}
20+
21+
$r = new ReflectionClass( MyBool::class );
22+
echo $r;
23+
echo "\n";
24+
$r = new ReflectionEnum( MyBool::class );
25+
echo $r;
26+
27+
var_export( MyBool::cases() );
28+
29+
?>
30+
--EXPECTF--
31+
Class [ <user> final class MyBool implements MyStringable, UnitEnum, BackedEnum ] {
32+
@@ %sReflectionEnum_toString_backed_int.php 7-16
33+
34+
- Constants [3] {
35+
Constant [ public MyBool MyFalse ] { Object }
36+
Constant [ public MyBool MyTrue ] { Object }
37+
Constant [ public MyBool OtherTrue ] { Object }
38+
}
39+
40+
- Static properties [0] {
41+
}
42+
43+
- Static methods [3] {
44+
Method [ <internal, prototype UnitEnum> static public method cases ] {
45+
46+
- Parameters [0] {
47+
}
48+
- Return [ array ]
49+
}
50+
51+
Method [ <internal, prototype BackedEnum> static public method from ] {
52+
53+
- Parameters [1] {
54+
Parameter #0 [ <required> string|int $value ]
55+
}
56+
- Return [ static ]
57+
}
58+
59+
Method [ <internal, prototype BackedEnum> static public method tryFrom ] {
60+
61+
- Parameters [1] {
62+
Parameter #0 [ <required> string|int $value ]
63+
}
64+
- Return [ ?static ]
65+
}
66+
}
67+
68+
- Properties [2] {
69+
Property [ public protected(set) readonly string $name ]
70+
Property [ public protected(set) readonly int $value ]
71+
}
72+
73+
- Methods [1] {
74+
Method [ <user, prototype MyStringable> public method toString ] {
75+
@@ %sReflectionEnum_toString_backed_int.php 13 - 15
76+
77+
- Parameters [0] {
78+
}
79+
- Return [ string ]
80+
}
81+
}
82+
}
83+
84+
Class [ <user> final class MyBool implements MyStringable, UnitEnum, BackedEnum ] {
85+
@@ %sReflectionEnum_toString_backed_int.php 7-16
86+
87+
- Constants [3] {
88+
Constant [ public MyBool MyFalse ] { Object }
89+
Constant [ public MyBool MyTrue ] { Object }
90+
Constant [ public MyBool OtherTrue ] { Object }
91+
}
92+
93+
- Static properties [0] {
94+
}
95+
96+
- Static methods [3] {
97+
Method [ <internal, prototype UnitEnum> static public method cases ] {
98+
99+
- Parameters [0] {
100+
}
101+
- Return [ array ]
102+
}
103+
104+
Method [ <internal, prototype BackedEnum> static public method from ] {
105+
106+
- Parameters [1] {
107+
Parameter #0 [ <required> string|int $value ]
108+
}
109+
- Return [ static ]
110+
}
111+
112+
Method [ <internal, prototype BackedEnum> static public method tryFrom ] {
113+
114+
- Parameters [1] {
115+
Parameter #0 [ <required> string|int $value ]
116+
}
117+
- Return [ ?static ]
118+
}
119+
}
120+
121+
- Properties [2] {
122+
Property [ public protected(set) readonly string $name ]
123+
Property [ public protected(set) readonly int $value ]
124+
}
125+
126+
- Methods [1] {
127+
Method [ <user, prototype MyStringable> public method toString ] {
128+
@@ %sReflectionEnum_toString_backed_int.php 13 - 15
129+
130+
- Parameters [0] {
131+
}
132+
- Return [ string ]
133+
}
134+
}
135+
}
136+
array (
137+
0 =>
138+
\MyBool::MyFalse,
139+
1 =>
140+
\MyBool::MyTrue,
141+
)
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
--TEST--
2+
ReflectionEnum::__toString() (larger case, string-backed)
3+
--FILE--
4+
<?php
5+
6+
interface MyStringable {
7+
public function toString(): string;
8+
}
9+
10+
enum MyBool: string implements MyStringable {
11+
case MyFalse = '~FALSE~';
12+
case MyTrue = '~TRUE~';
13+
14+
public const MyBool OtherTrue = MyBool::MyTrue;
15+
16+
public function toString(): string {
17+
return $this->name . " = " . $this->value;
18+
}
19+
}
20+
21+
$r = new ReflectionClass( MyBool::class );
22+
echo $r;
23+
echo "\n";
24+
$r = new ReflectionEnum( MyBool::class );
25+
echo $r;
26+
27+
var_export( MyBool::cases() );
28+
29+
?>
30+
--EXPECTF--
31+
Class [ <user> final class MyBool implements MyStringable, UnitEnum, BackedEnum ] {
32+
@@ %sReflectionEnum_toString_backed_string.php 7-16
33+
34+
- Constants [3] {
35+
Constant [ public MyBool MyFalse ] { Object }
36+
Constant [ public MyBool MyTrue ] { Object }
37+
Constant [ public MyBool OtherTrue ] { Object }
38+
}
39+
40+
- Static properties [0] {
41+
}
42+
43+
- Static methods [3] {
44+
Method [ <internal, prototype UnitEnum> static public method cases ] {
45+
46+
- Parameters [0] {
47+
}
48+
- Return [ array ]
49+
}
50+
51+
Method [ <internal, prototype BackedEnum> static public method from ] {
52+
53+
- Parameters [1] {
54+
Parameter #0 [ <required> string|int $value ]
55+
}
56+
- Return [ static ]
57+
}
58+
59+
Method [ <internal, prototype BackedEnum> static public method tryFrom ] {
60+
61+
- Parameters [1] {
62+
Parameter #0 [ <required> string|int $value ]
63+
}
64+
- Return [ ?static ]
65+
}
66+
}
67+
68+
- Properties [2] {
69+
Property [ public protected(set) readonly string $name ]
70+
Property [ public protected(set) readonly string $value ]
71+
}
72+
73+
- Methods [1] {
74+
Method [ <user, prototype MyStringable> public method toString ] {
75+
@@ %sReflectionEnum_toString_backed_string.php 13 - 15
76+
77+
- Parameters [0] {
78+
}
79+
- Return [ string ]
80+
}
81+
}
82+
}
83+
84+
Class [ <user> final class MyBool implements MyStringable, UnitEnum, BackedEnum ] {
85+
@@ %sReflectionEnum_toString_backed_string.php 7-16
86+
87+
- Constants [3] {
88+
Constant [ public MyBool MyFalse ] { Object }
89+
Constant [ public MyBool MyTrue ] { Object }
90+
Constant [ public MyBool OtherTrue ] { Object }
91+
}
92+
93+
- Static properties [0] {
94+
}
95+
96+
- Static methods [3] {
97+
Method [ <internal, prototype UnitEnum> static public method cases ] {
98+
99+
- Parameters [0] {
100+
}
101+
- Return [ array ]
102+
}
103+
104+
Method [ <internal, prototype BackedEnum> static public method from ] {
105+
106+
- Parameters [1] {
107+
Parameter #0 [ <required> string|int $value ]
108+
}
109+
- Return [ static ]
110+
}
111+
112+
Method [ <internal, prototype BackedEnum> static public method tryFrom ] {
113+
114+
- Parameters [1] {
115+
Parameter #0 [ <required> string|int $value ]
116+
}
117+
- Return [ ?static ]
118+
}
119+
}
120+
121+
- Properties [2] {
122+
Property [ public protected(set) readonly string $name ]
123+
Property [ public protected(set) readonly string $value ]
124+
}
125+
126+
- Methods [1] {
127+
Method [ <user, prototype MyStringable> public method toString ] {
128+
@@ %sReflectionEnum_toString_backed_string.php 13 - 15
129+
130+
- Parameters [0] {
131+
}
132+
- Return [ string ]
133+
}
134+
}
135+
}
136+
array (
137+
0 =>
138+
\MyBool::MyFalse,
139+
1 =>
140+
\MyBool::MyTrue,
141+
)

0 commit comments

Comments
 (0)