Skip to content

Commit

Permalink
Fix matching when arrays are identical
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Aug 18, 2017
1 parent cb0e412 commit ed9f3dc
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Spies/MatchArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ public function is_match( $actual ) {
return false;
}
$match_count = 0;
$is_associative = $this->is_associative( $actual );
foreach ( $this->expected_array as $key => $value ) {
// Compare associative arrays
if ( array_key_exists( $key, $actual ) && $actual[ $key ] === $value ) {
$match_count += 1;
}
// Compare indexed arrays
if ( ! $this->is_associative( $actual ) && in_array( $value, $actual ) ) {
$match_count += 1;
if ( $is_associative ) {
// Compare associative arrays
if ( array_key_exists( $key, $actual ) && $actual[ $key ] === $value ) {
$match_count += 1;
}
} else {
// Compare indexed arrays
if ( ! $this->is_associative( $actual ) && in_array( $value, $actual ) ) {
$match_count += 1;
}
}
}
return ( count( $this->expected_array ) === $match_count );
Expand Down

0 comments on commit ed9f3dc

Please sign in to comment.