-
-
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
Usage of asserts is incorrect? #1914
Comments
👯 |
|
I understand the reasons above (and I find them reasonable), but maybe there can be a slight change? I think that it is certainly useful to allow reuse of asserts and doing it by static calls is perfectly fine in this case, but maybe there could be different behavior directly on TestCase? I mean instead of extending That means that the API would stay the same - obviously other than the static change, but maybe that is not a big BC break, because everywhere is written to use |
...and I almost went to PhpStorm bug tracker to submit a bug, that $this->assert.. IDE suggestions don't offer anything, but the code still works. I thought I was going insane... |
I thinks is great that the methods are defined as static when they are not using Please also notice: In other languages like C# its event a hint that if some method don't access instance data (don't use Calling a non-method method statically generates on PHP 5 E_STRICT & on PHP 7 E_DEPRECATED. But not the other way around. Call a static method non-statically is fine. |
What is the reasoning behind the statement?
Its what I do, but why is it considered the 'right' choice? |
I think the reason for static assertions is:
|
I can see value in the assertions being declared statically, this probably allows for better code reuse. But I cannot see the value in calling them via If anything it just hide the fact that they are static and reusability. |
I still fail to see the motivation behind keeping things as they are.
Great then.
Fair enough. But still, we're15 years later, 6 major versions of PHPUnit have been released since then, slowly forgetting this heritage doesn't look like a big deal.
Now that's a good point.
This is where I get lost. You're giving all your reasons to keep the methods static, yet you advise users to call them dynamically, with no explanation whatsoever. Why on earth should we call them dynamically? |
* [TEST] use ::class instead of classname string Reason - when the class is renamed or removed, it will be detected much faster. It helps with the static analysis of the code (also used by IDEs) * [TEST] use assertSame() instead of assertEquals() assertEquals() means == assertSame() is === Can cause tests not detecting issues, because different instances of same class with same data are equal, but not the same. * [TEST] convert array() to [] syntax * [TEST] $this->assert* should be used See sebastianbergmann/phpunit#1914 (comment)
* [TEST] use ::class instead of classname string Reason - when the class is renamed or removed, it will be detected much faster. It helps with the static analysis of the code (also used by IDEs) * [TEST] use assertSame() instead of assertEquals() assertEquals() means == assertSame() is === Can cause tests not detecting issues, because different instances of same class with same data are equal, but not the same. * [TEST] convert array() to [] syntax * [TEST] $this->assert* should be used See sebastianbergmann/phpunit#1914 (comment)
* [TEST] use ::class instead of classname string Reason - when the class is renamed or removed, it will be detected much faster. It helps with the static analysis of the code (also used by IDEs) * [TEST] use assertSame() instead of assertEquals() assertEquals() means == assertSame() is === Can cause tests not detecting issues, because different instances of same class with same data are equal, but not the same. * [TEST] convert array() to [] syntax * [TEST] $this->assert* should be used See sebastianbergmann/phpunit#1914 (comment)
I feel with you. |
No it's not! It's OO. There is no rule, that you can't call static methods within your instance context. |
Static anlyzers, IDEs, everything is warning and complaining about this. Why is this then an issue for those to report if it was totally "acceptable"? Sure, technically it is fine, but conceptually? |
Fix the tools! |
What's the point of fixing tools if we can fix the documentation? |
I would advocate to call methods with respect to their declarations as well. For all who want's to automate choosing his way of usage of assertions, PHP CS Fixer can do it for you with |
I have thoroughly analyzed PHPUnit's Or, PHPUnit needs to have a third class that does something like // Expose static assert methods for people who need them.
class Assert
{
public static function assertEquals($expected, $actual, $memo='')
{
return (new AssertLogic())->assertEquals($expected, $actual, $memo);
}
} Then extend |
For anyone that is interested we have since moved to using The only downside is that you have to add use for |
I like this idea 👍 But to better comply with PHPUnit standards, I won't be calling (new class extends Assert {})->assert*(); ... </joke_mode> |
It's not a secret that all asserts in PHPUnit are in fact static methods, but everyone are using them as they are not, even official documentation.
I think I understand the reasons, why they were made static in first place, probably it is related to performance (btw, do other reasons exists for that?).
But in my own opinion, if they are static, we should use them as we use other static methods to make our code cleaner.
This means using:
instead of:
Can anyone give me a good reason why we shouldn't?
The text was updated successfully, but these errors were encountered: