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

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

Conversation

alexandre-daubois
Copy link
Member

@alexandre-daubois alexandre-daubois commented Dec 6, 2022

Q A
Branch? 6.3
Bug fix? no
New feature? yes
Deprecations? no
Tickets N/A
License MIT
Doc PR symfony/symfony-docs#17538

@alexandre-daubois alexandre-daubois force-pushed the feat/enum_exists branch 3 times, most recently from 670b219 to f53feae Compare December 8, 2022 12:26
@alexandre-daubois alexandre-daubois force-pushed the feat/enum_exists branch 2 times, most recently from 41aa4ca to bb1bc08 Compare December 8, 2022 12:41
@alexandre-daubois alexandre-daubois changed the title [PhpUnitBridge] Add enum_exists mock [PHPUnitBridge] Add enum_exists mock Dec 8, 2022
@carsonbot carsonbot changed the title [PHPUnitBridge] Add enum_exists mock [PhpUnitBridge] Add enum_exists mock Dec 14, 2022
@derrabus
Copy link
Member

Question: Can I use this mocking mechanism to test code that looks like this?

if (class_exists(SomeClass::class) && !enum_exists(SomeClass::class)) {
    // do something
}

This is a common pattern if you want to check to make sure a class is actually a class because an enum would also pass a class_exists() check. See https://3v4l.org/5qfuI

@alexandre-daubois
Copy link
Member Author

@derrabus It doesn't seem. Actually, if you declare a mocked class, it will return true for interface_exists, trait_exists and class_exists because of the way the Mocker is designed. Do we want to make a differentiation with enums? I don't have a strong opinion on this, as it is a bridge dedicated to tests. 🤔

@derrabus
Copy link
Member

Maybe we should make a difference, yes. 🤔

What do you think? Since you're contributing this feature, you must have some use case for it?

@alexandre-daubois
Copy link
Member Author

I actually don't have the use case. This contribution idea came to me when crawling the bridge documentation 😄 I updated the code so there's a diff between classes and enums. I added withMockedEnums that fills a new $enums properties, as well as $classes. This way, class_exists will return true on enums 👍

public static function withMockedEnums(array $enums)
{
self::$enums = $enums;
self::$classes += $enums;

This comment was marked as resolved.

This comment was marked as resolved.

@@ -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.

@nicolas-grekas
Copy link
Member

Thank you @alexandre-daubois.

@nicolas-grekas nicolas-grekas merged commit 918967f into symfony:6.3 Jan 9, 2023
@alexandre-daubois alexandre-daubois deleted the feat/enum_exists branch January 9, 2023 09:22
nicolas-grekas added a commit that referenced this pull request Jan 9, 2023
…daubois)

This PR was merged into the 6.3 branch.

Discussion
----------

[PhpUnitBridge] Fix `enum_exists` mock tests

| Q             | A
| ------------- | ---
| Branch?       | 6.3
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets       | _NA_
| License       | MIT
| Doc PR        | _NA_

This fixes some tests broken by #48516.
It seems that PHP needs the file to be required to be fully aware of the enum. Otherwise, even `\class_exists` and `\enum_exists` are returning false on `ExistingEnumReal::class`.

Commits
-------

693ade3 [PhpUnitBridge] Fix `enum_exists` mock tests
OskarStark added a commit to symfony/symfony-docs that referenced this pull request Jan 10, 2023
…ois)

This PR was merged into the 6.3 branch.

Discussion
----------

[PHPUnitBridge] Add `enum_exists` mock

Related to symfony/symfony#48516

Commits
-------

d18c238 [PhpUnitBridge] Add `enum_exists` mock
@fabpot fabpot mentioned this pull request May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants