-
-
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
assertArraySubset() bad documented or doesn't work as expected #2069
Comments
|
This is a question for @marcioAlmada who implemented this feature. |
It should be considered that some people are probably depending in their tests on this behaviour, so maybe it's not wise to change it after all. Maybe it just should be a little better documented. |
Hi, I guess this function should have a flag to not consider index position. For explicit associative array where you name the key this won't be a problem. However for dynamic index array this is indeed a problem to consider the order.
1 to 4 will pass successfully regardless of the order as expected with this assertion function name. But when it comes to dynamically assigned index
As said above: 1 - works A quick fix is to loop over the expected array subset for each key and run assertContains to the actual array to dismiss the index criteria. However you'll have issue with mixed array, partially explicit key definition and dynamic attribution. So, either you restrict usage of this function to the only array with explicit associate key, or this function should have a flag to mention "This is an associative array" or "This is a non associative array". The mixed case will remain unsupported though. |
Hi! Somehow I missed the notifications on this thread. Indeed this assertion was only discussed in the context of "associative arrays". I believe that for a pair of non associative arrays the assertion should be represented by an intersection followed by a sorted comparison: diff --git a/src/Framework/Constraint/ArraySubset.php b/src/Framework/Constraint/ArraySubset.php
index c3cb095..d3569b6 100644
--- a/src/Framework/Constraint/ArraySubset.php
+++ b/src/Framework/Constraint/ArraySubset.php
@@ -49,12 +49,26 @@ public function __construct($subset, $strict = false)
*/
protected function matches($other)
{
- $patched = array_replace_recursive($other, $this->subset);
+ if (! $this->isArrayAssociative($this->subset)) {
+ $patched = array_intersect($other, $this->subset);
- if ($this->strict) {
- return $other === $patched;
- } else {
- return $other == $patched;
+ sort($patched);
+ sort($this->subset);
+
+ if ($this->strict) {
+ return $this->subset === $patched;
+ } else {
+ return $this->subset == $patched;
+ }
+ } else {
+
+ $patched = array_replace_recursive($other, $this->subset);
+
+ if ($this->strict) {
+ return $other === $patched;
+ } else {
+ return $other == $patched;
+ }
}
}
@@ -82,4 +96,9 @@ protected function failureDescription($other)
{
return 'an array ' . $this->toString();
}
+
+ private function isArrayAssociative(array $subject)
+ {
+ return count(array_filter(array_keys($subject), 'is_string')) > 0;
+ }
}
diff --git a/tests/Framework/AssertTest.php b/tests/Framework/AssertTest.php
index 984346c..b63fe2d 100644
--- a/tests/Framework/AssertTest.php
+++ b/tests/Framework/AssertTest.php
@@ -182,6 +182,10 @@ public function testAssertArraySubset()
$this->assertArraySubset(['a' => 'item a', 'c' => ['a2' => 'item a2']], $array);
$this->assertArraySubset(['a' => 'item a', 'd' => ['a2' => ['b3' => 'item b3']]], $array);
+ $this->assertArraySubset(['a', 'b', 'c'], ['a', 'b', 'c', 'd']);
+ $this->assertArraySubset(['b', 'c'], ['a', 'b', 'c', 'd']);
+ $this->assertArraySubset(['b', 'a'], ['a', 'b', 'c', 'd' => 3, 'x']);
+
try {
$this->assertArraySubset(['a' => 'bad value'], $array);
} catch (PHPUnit_Framework_AssertionFailedError $e) { This patch could be a starting point, I'll be in transit and wasn't able to PR |
Thank you for looking into this, @marcioAlmada. |
Will this patch be applyed in 5.7 version? |
@marcioAlmada Is #2069 (comment) a bugfix? Can you send a pull request? |
Came across this issue today. Will the patch be implemented at some point @marcioAlmada ? |
I am still confused about @marcioAlmada Is #2069 (comment) backward compatible? Can you please send a pull request with that patch? Thanks! |
No feedback, closing. |
Can you please specify in the documentation that |
Hi,
please consider 3 calls:
1 - works
2 - doesn't work. Why? Isn't ['b','c'] a subset of ['a', 'b', 'c', 'd']?
3 - doesn't work as well. Elements must be in apriopriate order?
Should it work this way?
Can information about this be added to docs? (3 little examples above should do)?
It's sort of confusing right now :)
The text was updated successfully, but these errors were encountered: