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
Classes with __toString() should be tested like strings when appropriate #934
Comments
|
👍 Makes sense to me. |
|
I've thought about this before, but I'm still not sure if implicit type conversions are helpful or dangerous when testing. Consider that all the following examples match in PHP: var_dump(preg_match('/0/', new Exception()));
var_dump(preg_match('/1/', true));
var_dump(preg_match('/2/', 512));
var_dump(preg_match('/3/', 5.37));@sebastianbergmann, what do you think? |
This is and should be a match because casting an Exception to a string yields something like: This would be a really bad test because the regular expression is way too broad.
I'm not talking about changing anything to accept other things where it should accept a string. I'm only talking about making all string based assertions honor a classes __toString() method. |
|
@mtdowling, maybe I didn't do a very good job of explaining myself :P Those examples are meant to be contrived. I didn't intend to suggest that those are examples of good tests or that they shouldn't match for some reason. I understand why they match. I was trying to suggest that "PHPUnit should do x because PHP does x" is not always the best line of reasoning. PHP also accepts other scalars (integers, floats, booleans) in most methods that accept strings, but I don't think PHPUnit should. I understand that you're only suggesting that string-based assertions should use In the end, I'm not really for or against this. I asked for @sebastianbergmann's opinion because I'm kind of on the fence. Sorry if my original response was misleading, I'm just a bit pressed for time today :) |
|
Also on the fence here, leaning towards the "too much magic" side. |
|
Maybe @edorian has an opinion? |
|
Would be cool to support, but I am also mostly on the side of forcing unit tests to be more explicit when using those assertions, whether it be by directly calling Otherwise, the implicit |
|
Alright, I think it's time to close this. Thanks for the input everyone, but it doesn't seem like any of the core maintainers are in favor of this feature. |
PHPUnit currently does not work with things like:
When $class implements
__toString(). Assertions that work on strings should cast objects to a string if the object has a__toString()method. PHP does this when using objects in any method that accepts a string, so I think it makes sense that PHPUnit adopts the same behavior.The text was updated successfully, but these errors were encountered: