Closed (fixed)
Project:
Drupal core
Version:
9.2.x-dev
Component:
phpunit
Priority:
Normal
Category:
Task
Assigned:
Unassigned
Reporter:
Created:
2 Jul 2020 at 11:38 UTC
Updated:
5 Jul 2021 at 11:34 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
mondrakeComment #3
mondrakeComment #4
pavnish commented@catch and @mondrake Shall you please review this patch .All the usage of "assertSame" with counts replaced by the custom function
"AssertSameCount".
Thanks
Pavnish
Comment #5
mondrakeThanks for starting this!
IMHO it's good to have that method in a trait, but that trait should be more 'general', and imported in the base test classes - UnitTestBase, KernelTestBase, BrowserTestBase, etc.
We need test coverage for the new method itself.
An elegant solution would be to implement a PHPUnit Constraint for the check, à la #3134671: Introduce a PHPUnit Constraint to check existence of a response header , instead of falling to
assertSame.Comment #6
pavnish commented@mondrake
Thanks for your valuable feedback and suggestion.
I have ported the patch along with the your suggestion please review this patch.
Please suggest if any changes required it would be appreciated.
Thanks
Pavnish
Comment #7
mondrakeThanks.
Let's move this under
Drupal/TestToolsand tentatively call itAssertionTrait. This way we can add more assert methods in the trait when the need comes.We need to look at how PHPUnit assert methods are written, ussing Constraint and Assert classes.
Drupal\Tests\WebAssert::responseHeaderExistscould be a reference point. Also,Drupal\FunctionalTests\WebAssertTestshows how to test a custom assertion by intercepting the failure exception.assertNotSameCount.Comment #8
pavnish commented@mondrake
Thanks for the feedback and suggestions.
Point #7.1 and #7.2 addressed in this patch please review.
#7.3 :I think we should not work on assertNotSameCount method.I have tried to found the occurrence of assertNotSame with count Ex.
assertNotSame(count($foo), count($bar)) but i did not get any result.
Comment #11
tr commentedDo we really need to invent a new assertion method? What about assertSameSize(), which already exists?
Comment #12
mondrake#11 interesting! This assertions is there, but totally un-documented in PHPUnit manual. It happened already once to me: https://github.com/sebastianbergmann/phpunit-documentation-english/issue...
Comment #13
longwave@TR nice find, opened https://github.com/sebastianbergmann/phpunit-documentation-english/issue...
Comment #14
mondrakeShall we rescope this issue to use
::assertSameSize()?Comment #15
tr commentedHere's a patch to use
assertSameSize()when comparing thecount()of two arrays (or Countable or Traversable objects).I removed the "Needs tests" tag because we're not adding new code anymore, just replacing the assertions.
Comment #17
tr commentedI made an editing error in patch #15, resulting in a syntax error, and I also found one more assert that could be changed to use assertSameSize(). Here is a new patch and interdiff.
Comment #18
tr commentedPlease ignore #17, not my best work. That "one more assert that could be changed" wasn't in a test, so it broke everything.
Here's another patch, which is the same as #15 but with the syntax error fixed.
Comment #19
mondrakeThere's a few more that could benefit readibility by being converted.
Comment #20
jungleassertSameSize() exists since phpunit 3.6.0RC1, per the commit
It's ok back-porting down to 9.1.x or even 8.x, not 100% sure if there are more to do, so far so good, RTBC+1. Thanks!
Comment #21
longwaveFound two more that I think can be converted by searching for
assert.*count.*count, a lot of false positives to sift through though!Comment #22
mondrakeOn this
Comment #23
mondrakeAddressed #21, thanks
Comment #24
longwaveThere are perhaps more that we could convert, but they are going to be hard to find. This at least gives hints to others that read our tests that this method is available, so RTBC from me.
Comment #26
catchCommitted/pushed to 9.3.x and cherry-picked to 9.2.x, thanks!