Skip to content

Commit 36aa911

Browse files
committed
fix tests for PHP 8.0
1 parent 9bc1b14 commit 36aa911

File tree

5 files changed

+120
-41
lines changed

5 files changed

+120
-41
lines changed

src/Mabe/Enum/Cl/EmulatedBackedEnumTrait.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,15 @@ trait EmulatedBackedEnumTrait
3535
/**
3636
* The value of the current case
3737
*
38-
* @var int|string
38+
* @var string|int
3939
* @readonly
4040
*/
4141
public $value;
4242

4343
/**
4444
* A map of case names and values by enumeration class
4545
*
46-
* @var array<class-string<static>, array<string, int|string>>
46+
* @var array<class-string<static>, array<string, string|int>>
4747
*/
4848
private static $caseConstants = [];
4949

@@ -54,7 +54,7 @@ trait EmulatedBackedEnumTrait
5454
*/
5555
private static $cases = [];
5656

57-
/** @param int|string $value */
57+
/** @param string|int $value */
5858
final private function __construct(string $name, $value)
5959
{
6060
$this->name = $name;
@@ -121,7 +121,7 @@ final public static function __callStatic(string $name, array $args)
121121

122122
/**
123123
* @param class-string<static> $enumClass
124-
* @param int|string $value
124+
* @param string|int $value
125125
* @return static
126126
* @throws ValueError If the given value is not defined in the enumeration
127127
* @throws TypeError On argument type not matching enumeration type
@@ -156,7 +156,7 @@ private static function _from(string $enumClass, $value): BackedEnum
156156

157157
/**
158158
* @param class-string<static> $enumClass
159-
* @param int|string $value
159+
* @param string|int $value
160160
* @return null|static
161161
* @throws TypeError On argument type not matching enumeration type
162162
* @throws AssertionError On ambiguous case constant values or invalid case constant types
@@ -237,7 +237,7 @@ private static function init(string $enumClass): void
237237
$cases[$name] = $case;
238238
}
239239

240-
/** @var array<string, int|string> $caseConstants */
240+
/** @var array<string, string|int> $caseConstants */
241241
self::$cases[$enumClass] = $cases;
242242
self::$caseConstants[$enumClass] = $caseConstants;
243243
}

src/Mabe/Enum/Cl/EmulatedIntEnum-80.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final public static function from(string|int $value): static
4444
* @throws TypeError On argument type not matching enumeration type
4545
* @throws AssertionError On ambiguous case constant values or invalid case constant types
4646
*/
47-
final public static function tryFrom(int|string $value): ?static
47+
final public static function tryFrom(string|int $value): ?static
4848
{
4949
return static::_tryFrom(static::class, $value);
5050
}

src/Mabe/Enum/Cl/EmulatedStringEnum-80.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ final public static function from(string|int $value): static
4141
* @throws TypeError On argument type not matching enumeration type
4242
* @throws AssertionError On ambiguous case constant values or invalid case constant types
4343
*/
44-
final public static function tryFrom(int|string $value): ?static
44+
final public static function tryFrom(string|int $value): ?static
4545
{
4646
return static::_tryFrom(static::class, $value);
4747
}

tests/BasicIntEnumTest.php

Lines changed: 56 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ public function testFromUnexpectedStrTypeError(): void
5454

5555
public function testFromUnexpectedNullTypeError(): void
5656
{
57-
58-
$class = PHP_VERSION_ID >= 80000 ? EmulatedIntEnum::class : BasicIntEnum::class;
59-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'int';
57+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
58+
$class = EmulatedIntEnum::class;
59+
$type = 'string|int';
60+
} else {
61+
$class = BasicIntEnum::class;
62+
$type = 'int';
63+
}
6064

6165
$this->expectException('TypeError');
6266
$this->expectExceptionMessage("{$class}::from(): Argument #1 (\$value) must be of type {$type}, null given");
@@ -67,8 +71,13 @@ public function testFromUnexpectedNullTypeError(): void
6771

6872
public function testFromUnexpectedBoolTypeError(): void
6973
{
70-
$class = PHP_VERSION_ID >= 80000 ? EmulatedIntEnum::class : BasicIntEnum::class;
71-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'int';
74+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
75+
$class = EmulatedIntEnum::class;
76+
$type = 'string|int';
77+
} else {
78+
$class = BasicIntEnum::class;
79+
$type = 'int';
80+
}
7281

7382
$this->expectException('TypeError');
7483
$this->expectExceptionMessage("{$class}::from(): Argument #1 (\$value) must be of type {$type}, bool given");
@@ -79,8 +88,13 @@ public function testFromUnexpectedBoolTypeError(): void
7988

8089
public function testFromUnexpectedFloatTypeError(): void
8190
{
82-
$class = PHP_VERSION_ID >= 80000 ? EmulatedIntEnum::class : BasicIntEnum::class;
83-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'int';
91+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
92+
$class = EmulatedIntEnum::class;
93+
$type = 'string|int';
94+
} else {
95+
$class = BasicIntEnum::class;
96+
$type = 'int';
97+
}
8498

8599
$this->expectException('TypeError');
86100
$this->expectExceptionMessage("{$class}::from(): Argument #1 (\$value) must be of type {$type}, float given");
@@ -91,8 +105,13 @@ public function testFromUnexpectedFloatTypeError(): void
91105

92106
public function testFromUnexpectedObjTypeError(): void
93107
{
94-
$class = PHP_VERSION_ID >= 80000 ? EmulatedIntEnum::class : BasicIntEnum::class;
95-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'int';
108+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
109+
$class = EmulatedIntEnum::class;
110+
$type = 'string|int';
111+
} else {
112+
$class = BasicIntEnum::class;
113+
$type = 'int';
114+
}
96115

97116
$this->expectException('TypeError');
98117
$this->expectExceptionMessage("{$class}::from(): Argument #1 (\$value) must be of type {$type}, stdClass given");
@@ -130,8 +149,13 @@ public function testTryFromUnexpectedStrTypeError(): void
130149

131150
public function testTryFromUnexpectedNullTypeError(): void
132151
{
133-
$class = PHP_VERSION_ID >= 80000 ? EmulatedIntEnum::class : BasicIntEnum::class;
134-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'int';
152+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
153+
$class = EmulatedIntEnum::class;
154+
$type = 'string|int';
155+
} else {
156+
$class = BasicIntEnum::class;
157+
$type = 'int';
158+
}
135159

136160
$this->expectException('TypeError');
137161
$this->expectExceptionMessage("{$class}::tryFrom(): Argument #1 (\$value) must be of type {$type}, null given");
@@ -142,8 +166,13 @@ public function testTryFromUnexpectedNullTypeError(): void
142166

143167
public function testTryFromUnexpectedBoolTypeError(): void
144168
{
145-
$class = PHP_VERSION_ID >= 80000 ? EmulatedIntEnum::class : BasicIntEnum::class;
146-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'int';
169+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
170+
$class = EmulatedIntEnum::class;
171+
$type = 'string|int';
172+
} else {
173+
$class = BasicIntEnum::class;
174+
$type = 'int';
175+
}
147176

148177
$this->expectException('TypeError');
149178
$this->expectExceptionMessage("{$class}::tryFrom(): Argument #1 (\$value) must be of type {$type}, bool given");
@@ -154,8 +183,13 @@ public function testTryFromUnexpectedBoolTypeError(): void
154183

155184
public function testTryFromUnexpectedFloatTypeError(): void
156185
{
157-
$class = PHP_VERSION_ID >= 80000 ? EmulatedIntEnum::class : BasicIntEnum::class;
158-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'int';
186+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
187+
$class = EmulatedIntEnum::class;
188+
$type = 'string|int';
189+
} else {
190+
$class = BasicIntEnum::class;
191+
$type = 'int';
192+
}
159193

160194
$this->expectException('TypeError');
161195
$this->expectExceptionMessage("{$class}::tryFrom(): Argument #1 (\$value) must be of type {$type}, float given");
@@ -166,8 +200,13 @@ public function testTryFromUnexpectedFloatTypeError(): void
166200

167201
public function testTryFromUnexpectedObjTypeError(): void
168202
{
169-
$class = PHP_VERSION_ID >= 80000 ? EmulatedIntEnum::class : BasicIntEnum::class;
170-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'int';
203+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
204+
$class = EmulatedIntEnum::class;
205+
$type = 'string|int';
206+
} else {
207+
$class = BasicIntEnum::class;
208+
$type = 'int';
209+
}
171210

172211
$this->expectException('TypeError');
173212
$this->expectExceptionMessage("{$class}::tryFrom(): Argument #1 (\$value) must be of type {$type}, stdClass given");

tests/BasicStringEnumTest.php

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,13 @@ public function testFromUnexpectedIntTypeError(): void
5454

5555
public function testFromUnexpectedNullTypeError(): void
5656
{
57-
$class = PHP_VERSION_ID >= 80000 ? EmulatedStringEnum::class : BasicStringEnum::class;
58-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'string';
57+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
58+
$class = EmulatedStringEnum::class;
59+
$type = 'string|int';
60+
} else {
61+
$class = BasicStringEnum::class;
62+
$type = 'string';
63+
}
5964

6065
$this->expectException('TypeError');
6166
$this->expectExceptionMessage("{$class}::from(): Argument #1 (\$value) must be of type {$type}, null given");
@@ -66,8 +71,13 @@ public function testFromUnexpectedNullTypeError(): void
6671

6772
public function testFromUnexpectedBoolTypeError(): void
6873
{
69-
$class = PHP_VERSION_ID >= 80000 ? EmulatedStringEnum::class : BasicStringEnum::class;
70-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'string';
74+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
75+
$class = EmulatedStringEnum::class;
76+
$type = 'string|int';
77+
} else {
78+
$class = BasicStringEnum::class;
79+
$type = 'string';
80+
}
7181

7282
$this->expectException('TypeError');
7383
$this->expectExceptionMessage("{$class}::from(): Argument #1 (\$value) must be of type {$type}, bool given");
@@ -78,8 +88,13 @@ public function testFromUnexpectedBoolTypeError(): void
7888

7989
public function testFromUnexpectedFloatTypeError(): void
8090
{
81-
$class = PHP_VERSION_ID >= 80000 ? EmulatedStringEnum::class : BasicStringEnum::class;
82-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'string';
91+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
92+
$class = EmulatedStringEnum::class;
93+
$type = 'string|int';
94+
} else {
95+
$class = BasicStringEnum::class;
96+
$type = 'string';
97+
}
8398

8499
$this->expectException('TypeError');
85100
$this->expectExceptionMessage("{$class}::from(): Argument #1 (\$value) must be of type {$type}, float given");
@@ -90,8 +105,13 @@ public function testFromUnexpectedFloatTypeError(): void
90105

91106
public function testFromUnexpectedObjTypeError(): void
92107
{
93-
$class = PHP_VERSION_ID >= 80000 ? EmulatedStringEnum::class : BasicStringEnum::class;
94-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'string';
108+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
109+
$class = EmulatedStringEnum::class;
110+
$type = 'string|int';
111+
} else {
112+
$class = BasicStringEnum::class;
113+
$type = 'string';
114+
}
95115

96116
$this->expectException('TypeError');
97117
$this->expectExceptionMessage("{$class}::from(): Argument #1 (\$value) must be of type {$type}, stdClass given");
@@ -129,8 +149,13 @@ public function testTryFromUnexpectedIntTypeError(): void
129149

130150
public function testTryFromUnexpectedNullTypeError(): void
131151
{
132-
$class = PHP_VERSION_ID >= 80000 ? EmulatedStringEnum::class : BasicStringEnum::class;
133-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'string';
152+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
153+
$class = EmulatedStringEnum::class;
154+
$type = 'string|int';
155+
} else {
156+
$class = BasicStringEnum::class;
157+
$type = 'string';
158+
}
134159

135160
$this->expectException('TypeError');
136161
$this->expectExceptionMessage("{$class}::tryFrom(): Argument #1 (\$value) must be of type {$type}, null given");
@@ -141,8 +166,13 @@ public function testTryFromUnexpectedNullTypeError(): void
141166

142167
public function testTryFromUnexpectedBoolTypeError(): void
143168
{
144-
$class = PHP_VERSION_ID >= 80000 ? EmulatedStringEnum::class : BasicStringEnum::class;
145-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'string';
169+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
170+
$class = EmulatedStringEnum::class;
171+
$type = 'string|int';
172+
} else {
173+
$class = BasicStringEnum::class;
174+
$type = 'string';
175+
}
146176

147177
$this->expectException('TypeError');
148178
$this->expectExceptionMessage("{$class}::tryFrom(): Argument #1 (\$value) must be of type {$type}, bool given");
@@ -153,8 +183,13 @@ public function testTryFromUnexpectedBoolTypeError(): void
153183

154184
public function testTryFromUnexpectedFloatTypeError(): void
155185
{
156-
$class = PHP_VERSION_ID >= 80000 ? EmulatedStringEnum::class : BasicStringEnum::class;
157-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'string';
186+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
187+
$class = EmulatedStringEnum::class;
188+
$type = 'string|int';
189+
} else {
190+
$class = BasicStringEnum::class;
191+
$type = 'string';
192+
}
158193

159194
$this->expectException('TypeError');
160195
$this->expectExceptionMessage("{$class}::tryFrom(): Argument #1 (\$value) must be of type {$type}, float given");
@@ -165,8 +200,13 @@ public function testTryFromUnexpectedFloatTypeError(): void
165200

166201
public function testTryFromUnexpectedObjTypeError(): void
167202
{
168-
$class = PHP_VERSION_ID >= 80000 ? EmulatedStringEnum::class : BasicStringEnum::class;
169-
$type = PHP_VERSION_ID >= 80000 ? 'string|int' : 'string';
203+
if (PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100) {
204+
$class = EmulatedStringEnum::class;
205+
$type = 'string|int';
206+
} else {
207+
$class = BasicStringEnum::class;
208+
$type = 'string';
209+
}
170210

171211
$this->expectException('TypeError');
172212
$this->expectExceptionMessage("{$class}::tryFrom(): Argument #1 (\$value) must be of type {$type}, stdClass given");

0 commit comments

Comments
 (0)