Skip to content

Commit

Permalink
Merge pull request #11 from sirbrillig/fix/called-with-deep-objects
Browse files Browse the repository at this point in the history
Save SpyCall object arguments by reference
  • Loading branch information
sirbrillig authored Feb 17, 2017
2 parents b3ee2e2 + 81e5091 commit aaf082e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
19 changes: 0 additions & 19 deletions src/Spies/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,9 @@ 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;
}
return false;
}

public static function array_clone( $array ) {
return array_map( function( $element ) {
return ( ( is_array( $element ) )
? Helpers::array_clone( $element )
: ( ( is_object( $element ) )
? clone $element
: $element
)
);
}, $array );
}
}
3 changes: 1 addition & 2 deletions src/Spies/Spy.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ private function set_arguments( $args ) {
*
* You should not need to call this directly.
*/
private function record_function_call( $orig_args ) {
$args = Helpers::array_clone( $orig_args );
private function record_function_call( $args ) {
$this->call_record[] = new SpyCall( $args );
}

Expand Down
21 changes: 19 additions & 2 deletions tests/SpyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ public function test_spy_was_called_with_returns_true_if_the_spy_was_called_with
$this->assertTrue( $spy->was_called_with( $obj ) );
}

public function test_spy_was_called_with_returns_true_if_the_spy_was_called_with_the_deep_object_arguments_provided() {
$spy = \Spies\make_spy();
$obj = [ (object) [ 'ID' => 5, 'names' => [ 'bob' ], 'colors' => [ 'red' => '#f00' ] ] ];
$spy( [ 'foo' => $obj[0] ] );
$this->assertTrue( $spy->was_called_with( [ 'foo' => $obj[0] ] ) );
}

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 Expand Up @@ -153,14 +160,24 @@ public function test_spy_was_called_when_returns_false_if_the_spy_was_called_whe
} ) );
}

public function test_spy_was_called_when_uses_a_copy_of_the_arguments_as_they_were_when_the_call_occurred() {
public function test_spy_was_called_when_tests_object_arguments_by_reference() {
$spy = \Spies\make_spy();
$obj = new \StdClass();
$obj->foo = 'original';
$spy( $obj );
$obj->foo = 'modified';
$this->assertTrue( $spy->was_called_when( function( $args ) {
return $args[0]->foo === 'original';
return $args[0]->foo === 'modified';
} ) );
}

public function test_spy_was_called_when_tests_array_arguments_by_value() {
$spy = \Spies\make_spy();
$obj = [ 'foo' => 'original' ];
$spy( $obj );
$obj['foo'] = 'modified';
$this->assertTrue( $spy->was_called_when( function( $args ) {
return $args[0]['foo'] === 'original';
} ) );
}

Expand Down

0 comments on commit aaf082e

Please sign in to comment.