Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fa53f7

Browse files
[PhpUnitBridge] Add enum_exists mock
1 parent 94d6bbb commit 3fa53f7

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/Symfony/Bridge/PhpUnit/ClassExistsMock.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ public static function trait_exists($name, $autoload = true)
4949
return isset(self::$classes[$name]) ? (bool) self::$classes[$name] : \trait_exists($name, $autoload);
5050
}
5151

52+
public static function enum_exists($name, $autoload = true)
53+
{
54+
$name = ltrim($name, '\\');
55+
56+
if (\PHP_VERSION < 80100) {
57+
return false;
58+
}
59+
60+
return isset(self::$classes[$name]) ? (bool) self::$classes[$name] : \enum_exists($name, $autoload);
61+
}
62+
5263
public static function register($class)
5364
{
5465
$self = static::class;
@@ -61,7 +72,7 @@ public static function register($class)
6172
$mockedNs[] = substr($class, 6, strrpos($class, '\\') - 6);
6273
}
6374
foreach ($mockedNs as $ns) {
64-
foreach (['class', 'interface', 'trait'] as $type) {
75+
foreach (['class', 'interface', 'trait', 'enum'] as $type) {
6576
if (\function_exists($ns.'\\'.$type.'_exists')) {
6677
continue;
6778
}

src/Symfony/Bridge/PhpUnit/Tests/ClassExistsMockTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ protected function setUp(): void
3030
'NonExistingInterface' => true,
3131
ExistingTrait::class => false,
3232
'NonExistingTrait' => true,
33+
ExistingEnum::class => false,
34+
'NonExistingEnum' => true,
3335
]);
3436
}
3537

@@ -92,6 +94,26 @@ public function testTraitExists()
9294
$this->assertFalse(trait_exists('\\NonExistingTraitReal'));
9395
$this->assertFalse(trait_exists('\\NonExistingTraitReal', false));
9496
}
97+
98+
public function testEnumExists()
99+
{
100+
$this->assertFalse(enum_exists(ExistingEnum::class));
101+
$this->assertFalse(enum_exists(ExistingEnum::class, false));
102+
$this->assertFalse(enum_exists('\\'.ExistingEnum::class));
103+
$this->assertFalse(enum_exists('\\'.ExistingEnum::class, false));
104+
$this->assertTrue(enum_exists('NonExistingEnum'));
105+
$this->assertTrue(enum_exists('NonExistingEnum', false));
106+
$this->assertTrue(enum_exists('\\NonExistingEnum'));
107+
$this->assertTrue(enum_exists('\\NonExistingEnum', false));
108+
$this->assertTrue(enum_exists(ExistingEnumReal::class));
109+
$this->assertTrue(enum_exists(ExistingEnumReal::class, false));
110+
$this->assertTrue(enum_exists('\\'.ExistingEnumReal::class));
111+
$this->assertTrue(enum_exists('\\'.ExistingEnumReal::class, false));
112+
$this->assertFalse(enum_exists('NonExistingClassReal'));
113+
$this->assertFalse(enum_exists('NonExistingClassReal', false));
114+
$this->assertFalse(enum_exists('\\NonExistingEnumReal'));
115+
$this->assertFalse(enum_exists('\\NonExistingEnumReal', false));
116+
}
95117
}
96118

97119
class ExistingClass
@@ -117,3 +139,11 @@ trait ExistingTrait
117139
trait ExistingTraitReal
118140
{
119141
}
142+
143+
enum ExistingEnum
144+
{
145+
}
146+
147+
enum ExistingEnumReal
148+
{
149+
}

0 commit comments

Comments
 (0)