-
-
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
Support indexed arrays in ArraySubset #3161
Support indexed arrays in ArraySubset #3161
Conversation
Codecov Report
@@ Coverage Diff @@
## master #3161 +/- ##
===========================================
+ Coverage 81.32% 81.4% +0.07%
- Complexity 3384 3406 +22
===========================================
Files 137 137
Lines 8966 9003 +37
===========================================
+ Hits 7292 7329 +37
Misses 1674 1674
Continue to review full report at Codecov.
|
2e33879
to
7cc365e
Compare
I prefer private methods over anonymous functions. |
If this is a bug fix and the respective bug is in If this is an enhancement it will go into |
Thanks for the quick reply! I will refactor this using private methods. Regarding or not whether this is a bug fix, I think this can be debatable. I personally think of this as a bug since the current documentation does not mention that it is only designed to work with associative arrays:
When I read this documentation, then I would expect the following to return
However after I started working on this I read in #2069 (comment) that @marcioAlmada was only targeting associative arrays in the original implementation. So I guess from his point of view this is a new feature. |
I think it should go into |
OK! To the best of my knowledge there should not be a B/C break. I took care not to break the existing implementation for associative arrays, and have not touched the original tests, they are still passing. If people are using the current implementation for indexed arrays then there might be some unexpected failures appearing, but this will actually be because bugs in their code are now being correctly detected. For example, this will previously have returned
Is it the policy of PHPUnit to never cause any existing test to change output, even if the behaviour relied on is objectively broken? |
eb14b71
to
9cd8a7d
Compare
9cd8a7d
to
e9ea3d0
Compare
Thanks! |
} | ||
|
||
if ($this->isAssociative($array)) { | ||
\ksort($array); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is weird. Are you sure this shouldn't be asort
? The sort
just below sorts by value and this sorts by key. asort
would be more analogous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The goal is to be able to compare the two arrays, and so both ksort()
and asort()
do the trick for this use case. You do have a point that it would be more consistent using asort()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, same arrays sort to the same regardless you sort them by keys or values but for numeric arrays you wanted [1,2]
and [2,1]
to be the same.
This was a breaking change. Broke the existing Laravel framework tests. |
@taylorotwell Please be open a ticket for this breaking change (and be more specific than "Broke the existing Laravel framework tests"). Thanks! |
@taylorotwell thanks for the report. Can you check if the tests still fail if you run them using PR #3186? In that PR support is added for checking subsets that contain duplicate values. This might be the cause of the failures. |
This fixes #3101 and earlier efforts #2781 and #2069.
I have added support for indexed arrays, and it is now possible to correctly check multidimensional arrays. It allows to match indexed arrays that are out of order, so for example if you have an array
[0, 1, 2, 3]
you can assert that[3, 1]
is a valid subset.Because of this out-of-order matching there is a known limitation, the code loops over the data set and checks every data point and returns on the first match. This means it will generate a false positive if multiple identical values are present in the subset. For example if an array
[0, 1, 2, 3]
is given and you assert the subset[0, 1, 1, 2]
then it will returnTRUE
even though1
is present only once in the data set. I wasn't sure if the added complexity for supporting this edge case is worth it.This is my first contribution to PHPUnit and I am not sure on a few points: