Add test cases for backed enum implementing interface

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 <me@agustingomes.com>

Closes GH-7593.
This commit is contained in:
Agustin Gomes 2021-10-19 21:56:30 +02:00 committed by Nikita Popov
parent 9bccbf8b92
commit 8bb19cb549
2 changed files with 92 additions and 0 deletions

View file

@ -0,0 +1,58 @@
--TEST--
Backed Enum with multiple implementing interfaces
--FILE--
<?php
interface Colorful {
public function color(): string;
}
interface Shaped {
public function shape(): string;
}
interface ExtendedShaped extends Shaped {
}
enum Suit: string implements Colorful, ExtendedShaped {
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
public function color(): string {
return match ($this) {
self::Hearts, self::Diamonds => 'Red',
self::Clubs, self::Spades => 'Black',
};
}
public function shape(): string {
return match ($this) {
self::Hearts => 'heart',
self::Diamonds => 'diamond',
self::Clubs => 'club',
self::Spades => 'spade',
};
}
}
echo Suit::Hearts->color() . "\n";
echo Suit::Hearts->shape() . "\n";
echo Suit::Diamonds->color() . "\n";
echo Suit::Diamonds->shape() . "\n";
echo Suit::Clubs->color() . "\n";
echo Suit::Clubs->shape() . "\n";
echo Suit::Spades->color() . "\n";
echo Suit::Spades->shape() . "\n";
?>
--EXPECT--
Red
heart
Red
diamond
Black
club
Black
spade

View file

@ -0,0 +1,34 @@
--TEST--
Backed Enum implements
--FILE--
<?php
interface Colorful {
public function color(): string;
}
enum Suit: string implements Colorful {
case Hearts = 'H';
case Diamonds = 'D';
case Clubs = 'C';
case Spades = 'S';
public function color(): string {
return match ($this) {
self::Hearts, self::Diamonds => 'Red',
self::Clubs, self::Spades => 'Black',
};
}
}
echo Suit::Hearts->color() . "\n";
echo Suit::Diamonds->color() . "\n";
echo Suit::Clubs->color() . "\n";
echo Suit::Spades->color() . "\n";
?>
--EXPECT--
Red
Red
Black
Black