Skip to content

Commit

Permalink
Save original value in FalseyValue
Browse files Browse the repository at this point in the history
  • Loading branch information
sirbrillig committed Aug 9, 2017
1 parent eba7a71 commit 85e33d6
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
7 changes: 7 additions & 0 deletions src/Spies/FalseyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@

// A special case for falsey return values
class FalseyValue {
public function __construct( $value ) {
$this->value = $value;
}

public function get_value() {
return $this->value;
}
}
2 changes: 1 addition & 1 deletion src/Spies/GlobalSpies.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private static function replace_global_function( $function_name ) {

private static function filter_return_for( $return ) {
if ( $return instanceof FalseyValue ) {
return null;
return $return->get_value();
}
return $return;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Spies/Spy.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ public function that_returns( $value ) {
* @return Spy This Spy
*/
public function and_return( $value ) {
// Add a special case for falsey return values
// Add a special case for falsey return values so we can detect them
if ( ! $value ) {
$value = new FalseyValue();
$value = new FalseyValue( $value );
}
if ( isset( $this->with_arguments ) ) {
$this->conditional_returns[] = [ 'args' => $this->with_arguments, 'return' => $value ];
Expand Down Expand Up @@ -410,8 +410,8 @@ private function filter_return_for( $return, $args, $options = [] ) {
if ( is_callable( $return ) ) {
return call_user_func_array( $return, $args );
}
if ( $return instanceof FalseyValue && ! $options['return_falsey_objects'] ) {
return null;
if ( $return instanceof FalseyValue && empty( $options['return_falsey_objects'] ) ) {
return $return->get_value();
}
return $return;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/SpyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ public function test_stub_on_internal_existing_method_replaces_return_value_of_e
$this->assertEquals( 'boo', trigger_error( 'foo' ) );
}

public function test_stub_on_internal_existing_method_replaces_return_value_of_existing_method_if_empty() {
\Spies\mock_function( 'trigger_error' )->and_return( [] );
$this->assertEquals( [], trigger_error( 'foo' ) );
}

public function test_stub_on_internal_existing_method_replaces_return_value_of_existing_method_if_null() {
\Spies\mock_function( 'trigger_error' )->and_return( null );
$this->assertEquals( null, trigger_error( 'foo' ) );
Expand Down

0 comments on commit 85e33d6

Please sign in to comment.