Skip to content

[PhpUnitBridge] Add enum_exists mock #48516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Bridge/PhpUnit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add support for mocking the `enum_exists` function

6.2
---

Expand Down
24 changes: 22 additions & 2 deletions src/Symfony/Bridge/PhpUnit/ClassExistsMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,29 @@ class ClassExistsMock
{
private static $classes = [];

private static $enums = [];

/**
* Configures the classes to be checked upon existence.
*
* @param array $classes Mocked class names as keys (case sensitive, without leading root namespace slash) and booleans as values
* @param array $classes Mocked class names as keys (case-sensitive, without leading root namespace slash) and booleans as values
*/
public static function withMockedClasses(array $classes)
{
self::$classes = $classes;
}

/**
* Configures the enums to be checked upon existence.
*
* @param array $enums Mocked enums names as keys (case-sensitive, without leading root namespace slash) and booleans as values
*/
public static function withMockedEnums(array $enums)
{
self::$enums = $enums;
self::$classes += $enums;

This comment was marked as resolved.

This comment was marked as resolved.

}

public static function class_exists($name, $autoload = true)
{
$name = ltrim($name, '\\');
Expand All @@ -49,6 +62,13 @@ public static function trait_exists($name, $autoload = true)
return isset(self::$classes[$name]) ? (bool) self::$classes[$name] : \trait_exists($name, $autoload);
}

public static function enum_exists($name, $autoload = true)
{
$name = ltrim($name, '\\');

return isset(self::$enums[$name]) ? (bool) self::$enums[$name] : \enum_exists($name, $autoload);
}

public static function register($class)
{
$self = static::class;
Expand All @@ -61,7 +81,7 @@ public static function register($class)
$mockedNs[] = substr($class, 6, strrpos($class, '\\') - 6);
}
foreach ($mockedNs as $ns) {
foreach (['class', 'interface', 'trait'] as $type) {
foreach (['class', 'interface', 'trait', 'enum'] as $type) {
if (\function_exists($ns.'\\'.$type.'_exists')) {
continue;
}
Expand Down
46 changes: 46 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/ClassExistsMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ protected function setUp(): void
ExistingTrait::class => false,
'NonExistingTrait' => true,
]);

ClassExistsMock::withMockedEnums([
'NonExistingEnum' => true,
]);
}

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

public function testEnumExistsOnClasses()
{
$this->assertFalse(enum_exists(ExistingClass::class));
$this->assertFalse(enum_exists(ExistingClass::class, false));
$this->assertFalse(enum_exists('\\'.ExistingClass::class));
$this->assertFalse(enum_exists('\\'.ExistingClass::class, false));
$this->assertFalse(enum_exists('NonExistingClass'));
$this->assertFalse(enum_exists('NonExistingClass', false));
$this->assertFalse(enum_exists('\\NonExistingClass'));
$this->assertFalse(enum_exists('\\NonExistingClass', false));
$this->assertFalse(enum_exists(ExistingClassReal::class));
$this->assertFalse(enum_exists(ExistingClassReal::class, false));
$this->assertFalse(enum_exists('\\'.ExistingClassReal::class));
$this->assertFalse(enum_exists('\\'.ExistingClassReal::class, false));
$this->assertFalse(enum_exists('NonExistingClassReal'));
$this->assertFalse(enum_exists('NonExistingClassReal', false));
$this->assertFalse(enum_exists('\\NonExistingClassReal'));
$this->assertFalse(enum_exists('\\NonExistingClassReal', false));
}

public function testInterfaceExists()
{
$this->assertFalse(interface_exists(ExistingInterface::class));
Expand Down Expand Up @@ -92,6 +116,28 @@ public function testTraitExists()
$this->assertFalse(trait_exists('\\NonExistingTraitReal'));
$this->assertFalse(trait_exists('\\NonExistingTraitReal', false));
}

public function testEnumExists()
{
$this->assertTrue(enum_exists('NonExistingEnum'));
$this->assertTrue(enum_exists('NonExistingEnum', false));
$this->assertTrue(enum_exists('\\NonExistingEnum'));
$this->assertTrue(enum_exists('\\NonExistingEnum', false));
$this->assertFalse(enum_exists('NonExistingClassReal'));
$this->assertFalse(enum_exists('NonExistingClassReal', false));
$this->assertFalse(enum_exists('\\NonExistingEnumReal'));
$this->assertFalse(enum_exists('\\NonExistingEnumReal', false));
}

public function testClassExistsOnEnums()
{
$this->assertTrue(class_exists('NonExistingEnum'));
$this->assertTrue(class_exists('NonExistingEnum', false));
$this->assertTrue(class_exists('\\NonExistingEnum'));
$this->assertTrue(class_exists('\\NonExistingEnum', false));
$this->assertFalse(class_exists('\\NonExistingEnumReal'));
$this->assertFalse(class_exists('\\NonExistingEnumReal', false));
}
}

class ExistingClass
Expand Down
74 changes: 74 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/EnumExistsMockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClassExistsMock;
use Symfony\Bridge\PhpUnit\Tests\Fixtures\ExistingEnum;
use Symfony\Bridge\PhpUnit\Tests\Fixtures\ExistingEnumReal;

/**
* @requires PHP 8.1
*/
class EnumExistsMockTest extends TestCase
{
public static function setUpBeforeClass(): void
{
ClassExistsMock::register(__CLASS__);
}

protected function setUp(): void
{
ClassExistsMock::withMockedEnums([
ExistingEnum::class => false,
'NonExistingEnum' => true,
]);
}

public function testClassExists()
{
$this->assertFalse(class_exists(ExistingEnum::class));
$this->assertFalse(class_exists(ExistingEnum::class, false));
$this->assertFalse(class_exists('\\'.ExistingEnum::class));
$this->assertFalse(class_exists('\\'.ExistingEnum::class, false));
$this->assertTrue(class_exists('NonExistingEnum'));
$this->assertTrue(class_exists('NonExistingEnum', false));
$this->assertTrue(class_exists('\\NonExistingEnum'));
$this->assertTrue(class_exists('\\NonExistingEnum', false));
$this->assertTrue(class_exists(ExistingEnumReal::class));
$this->assertTrue(class_exists(ExistingEnumReal::class, false));
$this->assertTrue(class_exists('\\'.ExistingEnumReal::class));
$this->assertTrue(class_exists('\\'.ExistingEnumReal::class, false));
$this->assertFalse(class_exists('\\NonExistingEnumReal'));
$this->assertFalse(class_exists('\\NonExistingEnumReal', false));
}

public function testEnumExists()
{
$this->assertFalse(enum_exists(ExistingEnum::class));
$this->assertFalse(enum_exists(ExistingEnum::class, false));
$this->assertFalse(enum_exists('\\'.ExistingEnum::class));
$this->assertFalse(enum_exists('\\'.ExistingEnum::class, false));
$this->assertTrue(enum_exists('NonExistingEnum'));
$this->assertTrue(enum_exists('NonExistingEnum', false));
$this->assertTrue(enum_exists('\\NonExistingEnum'));
$this->assertTrue(enum_exists('\\NonExistingEnum', false));
$this->assertTrue(enum_exists(ExistingEnumReal::class));
$this->assertTrue(enum_exists(ExistingEnumReal::class, false));
$this->assertTrue(enum_exists('\\'.ExistingEnumReal::class));
$this->assertTrue(enum_exists('\\'.ExistingEnumReal::class, false));
$this->assertFalse(enum_exists('NonExistingClassReal'));
$this->assertFalse(enum_exists('NonExistingClassReal', false));
$this->assertFalse(enum_exists('\\NonExistingEnumReal'));
$this->assertFalse(enum_exists('\\NonExistingEnumReal', false));
}
}
16 changes: 16 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/Fixtures/ExistingEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Tests\Fixtures;

enum ExistingEnum
{
}
16 changes: 16 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/Fixtures/ExistingEnumReal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Tests\Fixtures;

enum ExistingEnumReal
{
}
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/PhpUnit/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},
"require-dev": {
"symfony/deprecation-contracts": "^2.5|^3.0",
"symfony/error-handler": "^5.4|^6.0"
"symfony/error-handler": "^5.4|^6.0",
"symfony/polyfill-php81": "^1.27"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this dependency?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because of this: #48516 (comment) 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, okay, it's just a dev dependency, so I guess it's fine.

},
"suggest": {
"symfony/error-handler": "For tracking deprecated interfaces usages at runtime with DebugClassLoader"
Expand Down