Skip to content

Commit

Permalink
Merge pull request #16 from sirbrillig/fix/exact-match-array
Browse files Browse the repository at this point in the history
Fix `match_array` when arrays are identical
  • Loading branch information
sirbrillig authored Aug 18, 2017
2 parents bd0c1b2 + ed9f3dc commit 408d180
Show file tree
Hide file tree
Showing 4 changed files with 73 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
12 changes: 12 additions & 0 deletions tests/AssertionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,24 @@ public function test_assert_that_was_called_with_is_true_when_called_with_match_
$this->assertThat( $spy, $this->wasCalledWith( [ \Spies\match_array( [ 'foo' => 'bar' ] ) ] ) );
}

public function test_assert_that_was_called_with_is_true_when_called_with_match_array_as_argument_for_exact_match() {
$spy = \Spies\make_spy();
$spy( [ 'foo' => 'bar' ] );
$this->assertThat( $spy, $this->wasCalledWith( [ \Spies\match_array( [ 'foo' => 'bar' ] ) ] ) );
}

public function test_assert_that_was_called_with_is_true_when_called_with_match_array_and_indexed_array() {
$spy = \Spies\make_spy();
$spy( [ 'foo', 'bar', 'baz' ] );
$this->assertThat( $spy, $this->wasCalledWith( [ \Spies\match_array( [ 'bar' ] ) ] ) );
}

public function test_assert_that_was_called_with_is_true_when_called_with_match_array_and_indexed_array_for_exact_match() {
$spy = \Spies\make_spy();
$spy( [ 'foo', 'bar', 'baz' ] );
$this->assertThat( $spy, $this->wasCalledWith( [ \Spies\match_array( [ 'foo', 'bar', 'baz' ] ) ] ) );
}

public function test_assert_spy_was_not_called_with_is_true_when_not_called_with_args() {
$spy = \Spies\make_spy();
$spy( 'e', 'b', 'c' );
Expand Down
14 changes: 14 additions & 0 deletions tests/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,27 @@ public function test_with_match_array_is_met_if_the_spy_is_called_with_matching_
$expectation->verify();
}

public function test_with_match_array_is_met_if_the_spy_is_called_with_matching_key_values_for_exact_match() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_array( [ 'foo' => 'bar' ] ) );
$spy( 'foo', [ 'foo' => 'bar' ] );
$expectation->verify();
}

public function test_with_match_array_is_met_if_the_spy_is_called_with_matching_index_keys() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_array( [ 'bar', 'foo' ] ) );
$spy( 'foo', [ 'foo', 'bar', 'baz' ] );
$expectation->verify();
}

public function test_with_match_array_is_met_if_the_spy_is_called_with_matching_index_keys_for_exact_match() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_array( [ 'bar', 'foo' ] ) );
$spy( 'foo', [ 'bar', 'foo' ] );
$expectation->verify();
}

public function test_with_match_array_is_not_met_if_the_spy_is_called_with_no_matching_index_keys() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_array( [ 'beep' ] ) );
Expand Down
36 changes: 36 additions & 0 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,46 @@ public function test_do_arrays_match_returns_true_if_using_match_array_and_array
$this->assertTrue( \Spies\do_arrays_match( $array1, \Spies\match_array( $array2 ) ) );
}

public function test_do_arrays_match_returns_true_if_using_match_array_and_arrays_match_exactly() {
$array1 = [ 'b', 'c' ];
$array2 = [ 'b', 'c' ];
$this->assertTrue( \Spies\do_arrays_match( $array1, \Spies\match_array( $array2 ) ) );
}

public function test_do_arrays_match_returns_false_if_using_match_array_and_arrays_differ() {
$array1 = [ 'a', 'b', 'c' ];
$array2 = [ 'd', 'c' ];
$this->assertFalse( \Spies\do_arrays_match( $array1, \Spies\match_array( $array2 ) ) );
}

public function test_do_assoc_arrays_match_returns_true_if_arrays_are_the_same() {
$array1 = [ 'a' => 'x', 'b' => 'y', 'c' => 'z' ];
$array2 = [ 'a' => 'x', 'b' => 'y', 'c' => 'z' ];
$this->assertTrue( \Spies\do_arrays_match( $array1, $array2 ) );
}

public function test_do_assoc_arrays_match_returns_false_if_arrays_are_different() {
$array1 = [ 'a' => 'x', 'b' => 'y', 'c' => 'z' ];
$array2 = [ 'b' => 'y', 'c' => 'z' ];
$this->assertFalse( \Spies\do_arrays_match( $array1, $array2 ) );
}

public function test_do_assoc_arrays_match_returns_true_if_using_match_array_and_arrays_match() {
$array1 = [ 'a' => 'x', 'b' => 'y', 'c' => 'z' ];
$array2 = [ 'b' => 'y', 'c' => 'z' ];
$this->assertTrue( \Spies\do_arrays_match( $array1, \Spies\match_array( $array2 ) ) );
}

public function test_do_assoc_arrays_match_returns_true_if_using_match_array_and_arrays_match_exactly() {
$array1 = [ 'b' => 'y', 'c' => 'z' ];
$array2 = [ 'b' => 'y', 'c' => 'z' ];
$this->assertTrue( \Spies\do_arrays_match( $array1, \Spies\match_array( $array2 ) ) );
}

public function test_do_assoc_arrays_match_returns_false_if_using_match_array_and_arrays_differ() {
$array1 = [ 'a' => 'x', 'b' => 'y', 'c' => 'z' ];
$array2 = [ 'd' => 'y', 'c' => 'z' ];
$this->assertFalse( \Spies\do_arrays_match( $array1, \Spies\match_array( $array2 ) ) );
}
}

0 comments on commit 408d180

Please sign in to comment.