Skip to content

Commit

Permalink
Merge pull request #10 from sirbrillig/fix/called-with-object
Browse files Browse the repository at this point in the history
Update do_args_match to do casted array comparisons of objects
  • Loading branch information
sirbrillig authored Feb 17, 2017
2 parents e0d8563 + d9c6b57 commit b3ee2e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/Spies/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function do_args_match( $a, $b ) {
return false;
}
$index = 0;
foreach( $a as $arg ) {
foreach ( $a as $arg ) {
if ( ! self::do_vals_match( $arg, $b[ $index ] ) ) {
return false;
}
Expand All @@ -28,6 +28,13 @@ private static function do_vals_match( $a, $b ) {
if ( $a === $b ) {
return true;
}
if ( is_object( $a ) || is_object( $b ) ) {
$array_a = is_object( $a ) ? (array) $a : $a;
$array_b = is_object( $b ) ? (array) $b : $b;
if ( $array_a === $array_b ) {
return true;
}
}
if ( $a instanceof \Spies\AnyValue || $b instanceof \Spies\AnyValue ) {
return true;
}
Expand Down
9 changes: 8 additions & 1 deletion tests/SpyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function test_spy_was_called_with_array_returns_true_if_the_spy_was_calle
public function test_spy_was_called_with_array_returns_false_if_the_spy_was_not_called_with_the_arguments_provided() {
$spy = \Spies\make_spy();
$spy( 'foo' );
$this->assertFalse( $spy->was_called_with( [ 'foo', 'bar', 'baz' ] ) );
$this->assertFalse( $spy->was_called_with_array( [ 'foo', 'bar', 'baz' ] ) );
}

public function test_spy_was_called_with_returns_true_if_the_spy_was_called_with_the_arguments_provided() {
Expand All @@ -111,6 +111,13 @@ public function test_spy_was_called_with_returns_true_if_the_spy_was_called_with
$this->assertTrue( $spy->was_called_with( 'foo', 'bar', 'baz' ) );
}

public function test_spy_was_called_with_returns_true_if_the_spy_was_called_with_the_object_arguments_provided() {
$spy = \Spies\make_spy();
$obj = (object) [ 'ID' => 5 ];
$spy( $obj );
$this->assertTrue( $spy->was_called_with( $obj ) );
}

public function test_spy_was_called_with_returns_false_if_the_spy_was_not_called_with_the_arguments_provided() {
$spy = \Spies\make_spy();
$spy( 'foo' );
Expand Down

0 comments on commit b3ee2e2

Please sign in to comment.