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

Skip to content

Commit f53feae

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

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

src/Symfony/Bridge/PhpUnit/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add support for mocking the `enum_exists` function
8+
49
6.2
510
---
611

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_ID < 80100) {
57+
throw new \LogicException('"enum_exists" cannot be used with a version of PHP earlier than 8.1.');
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
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Tests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ClassExistsMock;
16+
17+
class EnumExistsMockTest extends TestCase
18+
{
19+
public static function setUpBeforeClass(): void
20+
{
21+
ClassExistsMock::register(__CLASS__);
22+
}
23+
24+
protected function setUp(): void
25+
{
26+
ClassExistsMock::withMockedClasses([
27+
ExistingEnum::class => false,
28+
'NonExistingEnum' => true,
29+
]);
30+
}
31+
32+
/**
33+
* @requires PHP 8.1
34+
*/
35+
public function testEnumExists()
36+
{
37+
$this->assertFalse(enum_exists(ExistingEnum::class));
38+
$this->assertFalse(enum_exists(ExistingEnum::class, false));
39+
$this->assertFalse(enum_exists('\\'.ExistingEnum::class));
40+
$this->assertFalse(enum_exists('\\'.ExistingEnum::class, false));
41+
$this->assertTrue(enum_exists('NonExistingEnum'));
42+
$this->assertTrue(enum_exists('NonExistingEnum', false));
43+
$this->assertTrue(enum_exists('\\NonExistingEnum'));
44+
$this->assertTrue(enum_exists('\\NonExistingEnum', false));
45+
$this->assertTrue(enum_exists(ExistingEnumReal::class));
46+
$this->assertTrue(enum_exists(ExistingEnumReal::class, false));
47+
$this->assertTrue(enum_exists('\\'.ExistingEnumReal::class));
48+
$this->assertTrue(enum_exists('\\'.ExistingEnumReal::class, false));
49+
$this->assertFalse(enum_exists('NonExistingClassReal'));
50+
$this->assertFalse(enum_exists('NonExistingClassReal', false));
51+
$this->assertFalse(enum_exists('\\NonExistingEnumReal'));
52+
$this->assertFalse(enum_exists('\\NonExistingEnumReal', false));
53+
}
54+
}
55+
56+
enum ExistingEnum
57+
{
58+
}
59+
60+
enum ExistingEnumReal
61+
{
62+
}

0 commit comments

Comments
 (0)