Skip to content

Commit 18e66aa

Browse files
committed
Add Backed Enum test cases
In the Enumeration RFC, it states Backed Enums can implement interfaces, but it was not clear where the backed Enum type would need to be placed in such situation. This commit adds a test case for these scenarios. Signed-off-by: Agustin Gomes <[email protected]>
1 parent a2bc57e commit 18e66aa

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
--TEST--
2+
Backed Enum with multiple implementing interfaces
3+
--FILE--
4+
<?php
5+
6+
interface Colorful {
7+
public function color(): string;
8+
}
9+
10+
interface Shaped {
11+
public function shape(): string;
12+
}
13+
14+
interface ExtendedShaped extends Shaped {
15+
}
16+
17+
enum Suit: string implements Colorful, ExtendedShaped {
18+
case Hearts = 'H';
19+
case Diamonds = 'D';
20+
case Clubs = 'C';
21+
case Spades = 'S';
22+
23+
public function color(): string {
24+
return match ($this) {
25+
self::Hearts, self::Diamonds => 'Red',
26+
self::Clubs, self::Spades => 'Black',
27+
};
28+
}
29+
30+
public function shape(): string {
31+
return match ($this) {
32+
self::Hearts => 'heart',
33+
self::Diamonds => 'diamond',
34+
self::Clubs => 'club',
35+
self::Spades => 'spade',
36+
};
37+
}
38+
}
39+
40+
echo Suit::Hearts->color() . "\n";
41+
echo Suit::Hearts->shape() . "\n";
42+
echo Suit::Diamonds->color() . "\n";
43+
echo Suit::Diamonds->shape() . "\n";
44+
echo Suit::Clubs->color() . "\n";
45+
echo Suit::Clubs->shape() . "\n";
46+
echo Suit::Spades->color() . "\n";
47+
echo Suit::Spades->shape() . "\n";
48+
49+
?>
50+
--EXPECT--
51+
Red
52+
heart
53+
Red
54+
diamond
55+
Black
56+
club
57+
Black
58+
spade
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Backed Enum implements
3+
--FILE--
4+
<?php
5+
6+
interface Colorful {
7+
public function color(): string;
8+
}
9+
10+
enum Suit: string implements Colorful {
11+
case Hearts = 'H';
12+
case Diamonds = 'D';
13+
case Clubs = 'C';
14+
case Spades = 'S';
15+
16+
public function color(): string {
17+
return match ($this) {
18+
self::Hearts, self::Diamonds => 'Red',
19+
self::Clubs, self::Spades => 'Black',
20+
};
21+
}
22+
}
23+
24+
echo Suit::Hearts->color() . "\n";
25+
echo Suit::Diamonds->color() . "\n";
26+
echo Suit::Clubs->color() . "\n";
27+
echo Suit::Spades->color() . "\n";
28+
29+
?>
30+
--EXPECT--
31+
Red
32+
Red
33+
Black
34+
Black

0 commit comments

Comments
 (0)