-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Suggestion: remove return type declaration check from assertObjectEquals #4707
Comments
Call me paranoid, but I believe that this check is needed. Could you not make it optional in your polyfill and only perform it when you're able to? |
Well, I've been trying to think through everything what's being done in the assertion /
However, the return type check does not prevent any PHP errors when calling the comparator method. The only "risk" there, would be that the comparator method would return a non-boolean value, but as I propose above, that risk can be mitigated by checking that the received return value is a boolean, before doing the value check for What am I missing ?
Of course, except that doesn't solve the problem. Having a return type would result in a parse error on PHP 5, so in that case, it would require the "project under test" to have two different versions of their Does my proposal make more sense now ? |
It prevents calling the method when it is not certain that it returns
You are right, of course.
I think I have a better understanding of your problem now, thank you. |
So, would a PR to change this be acceptable ? |
If you want an answer right now, that answer would be "no". Maybe that will change after I had a chance to think about this some more. In my opinion, using this assertion in projects that cannot use return type declarations does not make sense. Making old versions of PHPUnit compatible with new versions of PHP is one thing. But making an assertion such as Sure, we could remove that check, simply call the method, and then check the return type. This is less safe, though, in my opinion at least, than requiring the Removing this check would make the polyfill work, but at the expense of less safety for developers that use current versions of PHPUnit with current versions of PHP. |
I'll leave you to think it over some more in that case 😉 |
As additional input for thinking it over - to me, this feels like PHPUnit overstepping its remit and dictating what The choice for a boolean return type is arbitrary. After all for It could even be argued that PHPUnit should also support class ValueObject {
public function equals( self $other )
{
return $this->value <=> $other->value;
}
} |
I would use different words, but you are right. This feature is opinionated. It is based on the assumptions outlined here. If you do not share the opinion that is expressed by these assumptions and implemented in |
Well, it's not about me. I'm presuming the feature was added to PHPUnit with the intention of it being used. And used widely-enough for it to warrant the assertion being added to PHPUnit itself and not published as an add-on. So, I'm just pointing out some things to think over which could be seen as blockers for this assertion being widely used (based on my review for the polyfills). |
Another question regarding the implementation of this assertion: When I totally get that it is absolutely 100% debatable whether two objects of different types, but with the same parent class, should ever be considered as "equal", however, in my humble opinion, that debate should be held in the project under test and not necessarily enforced by PHPUnit. |
…ethod PHPUnit 9.4.0 introduced the new `Assert::assertObjectEquals()` method. This commit: * Adds two traits with the same name. One to polyfill the method when not available in PHPUnit. The other to allow for `use`-ing the trait in PHPUnit versions in which the method is already natively available. * Adds an `InvalidComparisonMethodException` exception class. _PHPUnit natively throws a range of different exceptions._ _The polyfill included in this library throws one exception type - the `InvalidComparisonMethodException` - with a range of different messages._ * Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used. * Adds tests. As the polyfill contains logic to match the PHPUnit native implementation as closely as possible, while still being PHP and PHPUnit cross-version compatible, extensive unit tests have been added to ensure the behaviour of the polyfill matches that of the original function, with the exception of the _return type verification_. As return types were not available in PHP prior to PHP 7.0, the return type verification as done in the PHPUnit native implementation, has been replaced by a verification that the _returned value_ is of the required type. This provides the same safeguard as the PHPUnit native implementation, but in a PHP cross-version compatible manner. Note: the method uses `static::` to call the PHPUnit native functionality. This allows for existing method overloads in a child class of the PHPUnit native `TestCase` to be respected. Includes: * Adding information on the new polyfill to the README. * Adding the new polyfill to the existing `TestCases` classes. * Adding a few select exceptions to the PHPCS ruleset for the fixtures used in the tests. Refs: * sebastianbergmann/phpunit#4467 * sebastianbergmann/phpunit#4707 * sebastianbergmann/phpunit@1dba8c3 * sebastianbergmann/phpunit@6099c5e
…ethod PHPUnit 9.4.0 introduced the new `Assert::assertObjectEquals()` method. This commit: * Adds two traits with the same name. One to polyfill the method when not available in PHPUnit. The other to allow for `use`-ing the trait in PHPUnit versions in which the method is already natively available. * Adds an `InvalidComparisonMethodException` exception class. _PHPUnit natively throws a range of different exceptions._ _The polyfill included in this library throws one exception type - the `InvalidComparisonMethodException` - with a range of different messages._ * Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used. * Adds tests. As the polyfill contains logic to match the PHPUnit native implementation as closely as possible, while still being PHP and PHPUnit cross-version compatible, extensive unit tests have been added to ensure the behaviour of the polyfill matches that of the original function, with the exception of the _return type verification_. As return types were not available in PHP prior to PHP 7.0, the return type verification as done in the PHPUnit native implementation, has been replaced by a verification that the _returned value_ is of the required type. This provides the same safeguard as the PHPUnit native implementation, but in a PHP cross-version compatible manner. Note: the method uses `static::` to call the PHPUnit native functionality. This allows for existing method overloads in a child class of the PHPUnit native `TestCase` to be respected. Includes: * Adding information on the new polyfill to the README. * Adding the new polyfill to the existing `TestCases` classes. * Adding a few select exceptions to the PHPCS ruleset for the fixtures used in the tests. Refs: * sebastianbergmann/phpunit#4467 * sebastianbergmann/phpunit#4707 * sebastianbergmann/phpunit@1dba8c3 * sebastianbergmann/phpunit@6099c5e
…ethod PHPUnit 9.4.0 introduced the new `Assert::assertObjectEquals()` method. This commit: * Adds two traits with the same name. One to polyfill the method when not available in PHPUnit. The other to allow for `use`-ing the trait in PHPUnit versions in which the method is already natively available. * Adds an `InvalidComparisonMethodException` exception class. _PHPUnit natively throws a range of different exceptions._ _The polyfill included in this library throws one exception type - the `InvalidComparisonMethodException` - with a range of different messages._ * Logic to the custom autoloader which will load the correct trait depending on the PHPUnit version used. * Adds tests. As the polyfill contains logic to match the PHPUnit native implementation as closely as possible, while still being PHP and PHPUnit cross-version compatible, extensive unit tests have been added to ensure the behaviour of the polyfill matches that of the original function, with the exception of the _return type verification_. As return types were not available in PHP prior to PHP 7.0, the return type verification as done in the PHPUnit native implementation, has been replaced by a verification that the _returned value_ is of the required type. This provides the same safeguard as the PHPUnit native implementation, but in a PHP cross-version compatible manner. Note: the method uses `static::` to call the PHPUnit native functionality. This allows for existing method overloads in a child class of the PHPUnit native `TestCase` to be respected. Includes: * Adding information on the new polyfill to the README. * Adding the new polyfill to the existing `TestCases` classes. * Adding a few select exceptions to the PHPCS ruleset for the fixtures used in the tests. Refs: * sebastianbergmann/phpunit#4467 * sebastianbergmann/phpunit#4707 * sebastianbergmann/phpunit@1dba8c3 * sebastianbergmann/phpunit@6099c5e
Related to #4467 and 1dba8c3
I'm currently looking at polyfilling the
assertObjectEquals()
assertion for the PHPUnit Polyfills.As things stand, the assertion does a check on the
$method
to make sure that:bool
This prohibits the writing of PHP/PHPUnit cross-version compatible code using comparator methods, where PHP < 7.0 still has to be supported. (yeah, I know, don't get me started....).
The thing is, if I look at the implementation, that check should not have to be a blocker.
If the return type check is removed and replaced by a check that the output of
$other->{$this->method}($this->expected)
is of type boolean before checking that the value istrue
, this part of the assertion is still safeguarded, while not blocking cross-version code.Would a change to this effect be deemed acceptable ? If so, I'd be willing to create the PR for it.
The text was updated successfully, but these errors were encountered: