From cb0e412237ed3f2a2c1f4f6bfe3b370af3095991 Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Fri, 18 Aug 2017 15:53:48 -0400 Subject: [PATCH 1/2] Tests: add exact match tests for match_array --- tests/AssertionTest.php | 12 ++++++++++++ tests/ExpectationTest.php | 14 ++++++++++++++ tests/HelpersTest.php | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/tests/AssertionTest.php b/tests/AssertionTest.php index 6863d5f..57f607b 100644 --- a/tests/AssertionTest.php +++ b/tests/AssertionTest.php @@ -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' ); diff --git a/tests/ExpectationTest.php b/tests/ExpectationTest.php index 6014841..6b0dea3 100644 --- a/tests/ExpectationTest.php +++ b/tests/ExpectationTest.php @@ -214,6 +214,13 @@ 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' ] ) ); @@ -221,6 +228,13 @@ 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_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' ] ) ); diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index d47933e..017e6f8 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -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 ) ) ); + } } From ed9f3dca6e391fffb2ab0133f8ffb4782064212e Mon Sep 17 00:00:00 2001 From: Payton Swick Date: Fri, 18 Aug 2017 16:00:12 -0400 Subject: [PATCH 2/2] Fix matching when arrays are identical --- src/Spies/MatchArray.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Spies/MatchArray.php b/src/Spies/MatchArray.php index 7649f75..188e14e 100644 --- a/src/Spies/MatchArray.php +++ b/src/Spies/MatchArray.php @@ -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 );