Skip to content
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

Add assertObjectHasProperty() and assertObjectHasNotProperty() #5220

Closed
jasverix opened this issue Feb 20, 2023 · 10 comments
Closed

Add assertObjectHasProperty() and assertObjectHasNotProperty() #5220

jasverix opened this issue Feb 20, 2023 · 10 comments
Labels
feature/assertion Issues related to assertions and expectations type/enhancement A new idea that should be implemented

Comments

@jasverix
Copy link
Contributor

The assertObjectHasAttribute() is deprecated because the word "attribute" means something different in the PHP world now. But it was deprecated without any replacement, stating that it is bad code causing this assertion, one should use classes etc instead.

The problem is that I am using PhpUnit to unit-test my JSON API, and I cannot see a replacement for just checking the JSON structure. Of course, assertTrue(property_exists()) works, but the error message yielded is not nearly as good as assertObjectHasAttribute was.

Can we consider making assertObjectHasProperty()? That name will never be wrong, because it is the exact same word as in property_exists().

@jasverix jasverix added the type/enhancement A new idea that should be implemented label Feb 20, 2023
@sebastianbergmann
Copy link
Owner

I think this can be done, but it should target main (which will become PHPUnit 10.1).

@sebastianbergmann sebastianbergmann added the feature/assertion Issues related to assertions and expectations label Feb 24, 2023
@sebastianbergmann sebastianbergmann changed the title Add assertObjectHasProperty() and assertObjectHasNotProperty() Add assertObjectHasProperty() and assertObjectHasNotProperty() Mar 6, 2023
jrfnl added a commit to Yoast/PHPUnit-Polyfills that referenced this issue Apr 16, 2023
…tObject[Not]HasProperty() methods

PHPUnit 10.1.0 introduces the new `Assert::assertObjectHasProperty()` and `Assert::assertObjectNotHasProperty()` methods.

This commit:
* Adds two traits with the same name.
    One to polyfill the methods when not available in PHPUnit.
    The other - an empty trait - to allow for `use`-ing the trait in PHPUnit versions in which the methods are already natively available.
* Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used.
* An availability test and functional tests for the functionality polyfilled.

Includes:
* Adding the new polyfill to the existing `TestCases` classes.

Refs:
* sebastianbergmann/phpunit#5220
* sebastianbergmann/phpunit#5231 (and follow up commits/PRs)

Co-authored-by: Jan-Sverre Riksfjord <[email protected]>
Co-authored-by: Sebastian Bergmann <[email protected]>
@westerp
Copy link

westerp commented Jun 1, 2023

So the solution is to downgrade to 9.6.0 as all minor updates after that breaks since AFAIK there is no way to suppress this and it fails the tests. My library still supports 7.4 so I cannot go up and the fix isn't backported :-(

@stronk7
Copy link
Contributor

stronk7 commented Jul 25, 2023

Yeah, if the warnings were backported to 9.6.x (by #5160).... then the new methods should be also available there, so cases can be fixed in 9. Without the methods... the warnings are just useless noise and only solutions are:

  • Or you stay with 9.6.0 (before the warnings were added).
  • Or you convert to interim assertTrue(property_exists(....)) or implement them as custom method ... or some other hack.

Both far from ideal, I'd say, sigh. Ciao :-)

@raziel057
Copy link

In my mind the methods should be added from same release the deprecation have been added to provide an upgrade path. It's not always possible to update to phpunit 10 (if you are using the symfony/phpunit-bridge for example) you are stuck on phpunit 9.6.x.

@sebastianbergmann I can create a PR that targets the 9.6 branch if you are agree with that.

@sebastianbergmann
Copy link
Owner

I can create a PR that targets the 9.6 branch if you are agree with that.

I would like to avoid changing PHPUnit 9. You can use yoast/phpunit-polyfills to work around this issue.

@sebastianbergmann
Copy link
Owner

@jrfnl Would your polyfills have a problem if PHPUnit 9.6 would get assertObjectHasProperty() and assertObjectHasNotProperty()? If not then I would backport them. It's the least I can do to remedy this mess I created :-(

@jrfnl
Copy link
Contributor

jrfnl commented Aug 18, 2023

@jrfnl Would your polyfills have a problem if PHPUnit 9.6 would get assertObjectHasProperty() and assertObjectHasNotProperty()? If not then I would backport them. It's the least I can do to remedy this mess I created :-(

@sebastianbergmann Shouldn't be a problem at all. The autoloader does a check for the methods before loading the polyfill.
Let me know if you do/have done the backport and I'll run the tests just to be sure, but I don't expect any problems (and if there are any, I'll release a patch version).

https://github.com/Yoast/PHPUnit-Polyfills/blob/c758753e8f9dac251fed396a73c8305af3f17922/phpunitpolyfills-autoload.php#L338-L353

@sebastianbergmann
Copy link
Owner

@jrfnl Thank you!

jrfnl added a commit to Yoast/PHPUnit-Polyfills that referenced this issue Aug 19, 2023
…ertObject[Not]HasProperty() methods

PHPUnit 10.1.0 introduced the new `Assert::assertObjectHasProperty()` and `Assert::assertObjectNotHasProperty()` methods.

These methods have now been backported to PHPUnit 9.6.11, so should be made available in the PHPUnit Polyfills 1.x series.

This commit:
* Adds two traits with the same name.
    One to polyfill the methods when not available in PHPUnit.
    The other - an empty trait - to allow for `use`-ing the trait in PHPUnit versions in which the methods are already natively available.
* Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used.
* An availability test and functional tests for the functionality polyfilled.

Includes:
* Adding the new polyfill to the existing `TestCases` classes.

Refs:
* sebastianbergmann/phpunit#5220
* sebastianbergmann/phpunit#5231 (and follow up commits/PRs)
* sebastianbergmann/phpunit#5478

Co-authored-by: Jan-Sverre Riksfjord <[email protected]>
Co-authored-by: Sebastian Bergmann <[email protected]>
jrfnl added a commit to Yoast/PHPUnit-Polyfills that referenced this issue Aug 19, 2023
…ertObject[Not]HasProperty() methods

PHPUnit 10.1.0 introduced the new `Assert::assertObjectHasProperty()` and `Assert::assertObjectNotHasProperty()` methods.

These methods have now been backported to PHPUnit 9.6.11, so should be made available in the PHPUnit Polyfills 1.x series.

This commit:
* Adds two traits with the same name.
    One to polyfill the methods when not available in PHPUnit.
    The other - an empty trait - to allow for `use`-ing the trait in PHPUnit versions in which the methods are already natively available.
* Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used.
* An availability test and functional tests for the functionality polyfilled.

Includes:
* Adding the new polyfill to the existing `TestCases` classes.

Refs:
* sebastianbergmann/phpunit#5220
* sebastianbergmann/phpunit#5231 (and follow up commits/PRs)
* sebastianbergmann/phpunit#5478

Co-authored-by: Jan-Sverre Riksfjord <[email protected]>
Co-authored-by: Sebastian Bergmann <[email protected]>
jrfnl added a commit to Yoast/PHPUnit-Polyfills that referenced this issue Aug 19, 2023
…ertObject[Not]HasProperty() methods

PHPUnit 10.1.0 introduced the new `Assert::assertObjectHasProperty()` and `Assert::assertObjectNotHasProperty()` methods.

These methods have now been backported to PHPUnit 9.6.11, so should be made available in the PHPUnit Polyfills 1.x series.

This commit:
* Adds two traits with the same name.
    One to polyfill the methods when not available in PHPUnit.
    The other - an empty trait - to allow for `use`-ing the trait in PHPUnit versions in which the methods are already natively available.
* Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used.
* An availability test and functional tests for the functionality polyfilled.

Includes:
* Adding the new polyfill to the existing `TestCases` classes.

Refs:
* sebastianbergmann/phpunit#5220
* sebastianbergmann/phpunit#5231 (and follow up commits/PRs)
* sebastianbergmann/phpunit#5478

Co-authored-by: Jan-Sverre Riksfjord <[email protected]>
Co-authored-by: Sebastian Bergmann <[email protected]>
jrfnl added a commit to Yoast/PHPUnit-Polyfills that referenced this issue Aug 19, 2023
…ertObject[Not]HasProperty() methods

PHPUnit 10.1.0 introduced the new `Assert::assertObjectHasProperty()` and `Assert::assertObjectNotHasProperty()` methods.

These methods have now been backported to PHPUnit 9.6.11, so should be made available in the PHPUnit Polyfills 1.x series.

This commit:
* Adds two traits with the same name.
    One to polyfill the methods when not available in PHPUnit.
    The other - an empty trait - to allow for `use`-ing the trait in PHPUnit versions in which the methods are already natively available.
* Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used.
* An availability test and functional tests for the functionality polyfilled.

Includes:
* Adding the new polyfill to the existing `TestCases` classes.

Refs:
* sebastianbergmann/phpunit#5220
* sebastianbergmann/phpunit#5231 (and follow up commits/PRs)
* sebastianbergmann/phpunit#5478

Co-authored-by: Jan-Sverre Riksfjord <[email protected]>
Co-authored-by: Sebastian Bergmann <[email protected]>
@jrfnl
Copy link
Contributor

jrfnl commented Aug 19, 2023

@sebastianbergmann FYI: I've just released PHPUnit Polyfills 1.1.0, which backports the polyfill for these methods to the 1.x (PHPUnit 4.x - 9.x) series.
As I expected, PHPUnit Polyfills 2.x (PHPUnit 5.x - 10.x) is fine, just needed a couple of test tweaks.

@raziel057
Copy link

raziel057 commented Aug 21, 2023

@sebastianbergmann @jrfnl Thanks for the consideration of this topic and the solution.

kitsunet added a commit to neos/neos-development-distribution that referenced this issue Jun 27, 2024
We want this to get past deprecations:
sebastianbergmann/phpunit#5220
kitsunet added a commit to neos/neos-development-distribution that referenced this issue Jun 27, 2024
We want this to get past deprecations:
sebastianbergmann/phpunit#5220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/assertion Issues related to assertions and expectations type/enhancement A new idea that should be implemented
Projects
None yet
6 participants