Skip to content

Commit b30d276

Browse files
[PhpUnitBridge] Add enum_exists mock
1 parent 3c42a3b commit b30d276

7 files changed

+181
-3
lines changed

CHANGELOG.md

+5
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

ClassExistsMock.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,29 @@ class ClassExistsMock
1818
{
1919
private static $classes = [];
2020

21+
private static $enums = [];
22+
2123
/**
2224
* Configures the classes to be checked upon existence.
2325
*
24-
* @param array $classes Mocked class names as keys (case sensitive, without leading root namespace slash) and booleans as values
26+
* @param array $classes Mocked class names as keys (case-sensitive, without leading root namespace slash) and booleans as values
2527
*/
2628
public static function withMockedClasses(array $classes)
2729
{
2830
self::$classes = $classes;
2931
}
3032

33+
/**
34+
* Configures the enums to be checked upon existence.
35+
*
36+
* @param array $enums Mocked enums names as keys (case-sensitive, without leading root namespace slash) and booleans as values
37+
*/
38+
public static function withMockedEnums(array $enums)
39+
{
40+
self::$enums = $enums;
41+
self::$classes += $enums;
42+
}
43+
3144
public static function class_exists($name, $autoload = true)
3245
{
3346
$name = ltrim($name, '\\');
@@ -49,6 +62,13 @@ public static function trait_exists($name, $autoload = true)
4962
return isset(self::$classes[$name]) ? (bool) self::$classes[$name] : \trait_exists($name, $autoload);
5063
}
5164

65+
public static function enum_exists($name, $autoload = true)
66+
{
67+
$name = ltrim($name, '\\');
68+
69+
return isset(self::$enums[$name]) ? (bool) self::$enums[$name] : \enum_exists($name, $autoload);
70+
}
71+
5272
public static function register($class)
5373
{
5474
$self = static::class;
@@ -61,7 +81,7 @@ public static function register($class)
6181
$mockedNs[] = substr($class, 6, strrpos($class, '\\') - 6);
6282
}
6383
foreach ($mockedNs as $ns) {
64-
foreach (['class', 'interface', 'trait'] as $type) {
84+
foreach (['class', 'interface', 'trait', 'enum'] as $type) {
6585
if (\function_exists($ns.'\\'.$type.'_exists')) {
6686
continue;
6787
}

Tests/ClassExistsMockTest.php

+46
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ protected function setUp(): void
3131
ExistingTrait::class => false,
3232
'NonExistingTrait' => true,
3333
]);
34+
35+
ClassExistsMock::withMockedEnums([
36+
'NonExistingEnum' => true,
37+
]);
3438
}
3539

3640
public function testClassExists()
@@ -53,6 +57,26 @@ public function testClassExists()
5357
$this->assertFalse(class_exists('\\NonExistingClassReal', false));
5458
}
5559

60+
public function testEnumExistsOnClasses()
61+
{
62+
$this->assertFalse(enum_exists(ExistingClass::class));
63+
$this->assertFalse(enum_exists(ExistingClass::class, false));
64+
$this->assertFalse(enum_exists('\\'.ExistingClass::class));
65+
$this->assertFalse(enum_exists('\\'.ExistingClass::class, false));
66+
$this->assertFalse(enum_exists('NonExistingClass'));
67+
$this->assertFalse(enum_exists('NonExistingClass', false));
68+
$this->assertFalse(enum_exists('\\NonExistingClass'));
69+
$this->assertFalse(enum_exists('\\NonExistingClass', false));
70+
$this->assertFalse(enum_exists(ExistingClassReal::class));
71+
$this->assertFalse(enum_exists(ExistingClassReal::class, false));
72+
$this->assertFalse(enum_exists('\\'.ExistingClassReal::class));
73+
$this->assertFalse(enum_exists('\\'.ExistingClassReal::class, false));
74+
$this->assertFalse(enum_exists('NonExistingClassReal'));
75+
$this->assertFalse(enum_exists('NonExistingClassReal', false));
76+
$this->assertFalse(enum_exists('\\NonExistingClassReal'));
77+
$this->assertFalse(enum_exists('\\NonExistingClassReal', false));
78+
}
79+
5680
public function testInterfaceExists()
5781
{
5882
$this->assertFalse(interface_exists(ExistingInterface::class));
@@ -92,6 +116,28 @@ public function testTraitExists()
92116
$this->assertFalse(trait_exists('\\NonExistingTraitReal'));
93117
$this->assertFalse(trait_exists('\\NonExistingTraitReal', false));
94118
}
119+
120+
public function testEnumExists()
121+
{
122+
$this->assertTrue(enum_exists('NonExistingEnum'));
123+
$this->assertTrue(enum_exists('NonExistingEnum', false));
124+
$this->assertTrue(enum_exists('\\NonExistingEnum'));
125+
$this->assertTrue(enum_exists('\\NonExistingEnum', false));
126+
$this->assertFalse(enum_exists('NonExistingClassReal'));
127+
$this->assertFalse(enum_exists('NonExistingClassReal', false));
128+
$this->assertFalse(enum_exists('\\NonExistingEnumReal'));
129+
$this->assertFalse(enum_exists('\\NonExistingEnumReal', false));
130+
}
131+
132+
public function testClassExistsOnEnums()
133+
{
134+
$this->assertTrue(class_exists('NonExistingEnum'));
135+
$this->assertTrue(class_exists('NonExistingEnum', false));
136+
$this->assertTrue(class_exists('\\NonExistingEnum'));
137+
$this->assertTrue(class_exists('\\NonExistingEnum', false));
138+
$this->assertFalse(class_exists('\\NonExistingEnumReal'));
139+
$this->assertFalse(class_exists('\\NonExistingEnumReal', false));
140+
}
95141
}
96142

97143
class ExistingClass

Tests/EnumExistsMockTest.php

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
use Symfony\Bridge\PhpUnit\Tests\Fixtures\ExistingEnum;
17+
use Symfony\Bridge\PhpUnit\Tests\Fixtures\ExistingEnumReal;
18+
19+
/**
20+
* @requires PHP 8.1
21+
*/
22+
class EnumExistsMockTest extends TestCase
23+
{
24+
public static function setUpBeforeClass(): void
25+
{
26+
ClassExistsMock::register(__CLASS__);
27+
}
28+
29+
protected function setUp(): void
30+
{
31+
ClassExistsMock::withMockedEnums([
32+
ExistingEnum::class => false,
33+
'NonExistingEnum' => true,
34+
]);
35+
}
36+
37+
public function testClassExists()
38+
{
39+
$this->assertFalse(class_exists(ExistingEnum::class));
40+
$this->assertFalse(class_exists(ExistingEnum::class, false));
41+
$this->assertFalse(class_exists('\\'.ExistingEnum::class));
42+
$this->assertFalse(class_exists('\\'.ExistingEnum::class, false));
43+
$this->assertTrue(class_exists('NonExistingEnum'));
44+
$this->assertTrue(class_exists('NonExistingEnum', false));
45+
$this->assertTrue(class_exists('\\NonExistingEnum'));
46+
$this->assertTrue(class_exists('\\NonExistingEnum', false));
47+
$this->assertTrue(class_exists(ExistingEnumReal::class));
48+
$this->assertTrue(class_exists(ExistingEnumReal::class, false));
49+
$this->assertTrue(class_exists('\\'.ExistingEnumReal::class));
50+
$this->assertTrue(class_exists('\\'.ExistingEnumReal::class, false));
51+
$this->assertFalse(class_exists('\\NonExistingEnumReal'));
52+
$this->assertFalse(class_exists('\\NonExistingEnumReal', false));
53+
}
54+
55+
public function testEnumExists()
56+
{
57+
$this->assertFalse(enum_exists(ExistingEnum::class));
58+
$this->assertFalse(enum_exists(ExistingEnum::class, false));
59+
$this->assertFalse(enum_exists('\\'.ExistingEnum::class));
60+
$this->assertFalse(enum_exists('\\'.ExistingEnum::class, false));
61+
$this->assertTrue(enum_exists('NonExistingEnum'));
62+
$this->assertTrue(enum_exists('NonExistingEnum', false));
63+
$this->assertTrue(enum_exists('\\NonExistingEnum'));
64+
$this->assertTrue(enum_exists('\\NonExistingEnum', false));
65+
$this->assertTrue(enum_exists(ExistingEnumReal::class));
66+
$this->assertTrue(enum_exists(ExistingEnumReal::class, false));
67+
$this->assertTrue(enum_exists('\\'.ExistingEnumReal::class));
68+
$this->assertTrue(enum_exists('\\'.ExistingEnumReal::class, false));
69+
$this->assertFalse(enum_exists('NonExistingClassReal'));
70+
$this->assertFalse(enum_exists('NonExistingClassReal', false));
71+
$this->assertFalse(enum_exists('\\NonExistingEnumReal'));
72+
$this->assertFalse(enum_exists('\\NonExistingEnumReal', false));
73+
}
74+
}

Tests/Fixtures/ExistingEnum.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Fixtures;
13+
14+
enum ExistingEnum
15+
{
16+
}

Tests/Fixtures/ExistingEnumReal.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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\Fixtures;
13+
14+
enum ExistingEnumReal
15+
{
16+
}

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
},
2323
"require-dev": {
2424
"symfony/deprecation-contracts": "^2.5|^3.0",
25-
"symfony/error-handler": "^5.4|^6.0"
25+
"symfony/error-handler": "^5.4|^6.0",
26+
"symfony/polyfill-php81": "^1.27"
2627
},
2728
"suggest": {
2829
"symfony/error-handler": "For tracking deprecated interfaces usages at runtime with DebugClassLoader"

0 commit comments

Comments
 (0)