Skip to content

Commit

Permalink
Merge pull request #26 from sirbrillig/add/readable-special-matchers
Browse files Browse the repository at this point in the history
Add readable special matchers
  • Loading branch information
sirbrillig authored Oct 30, 2017
2 parents 0ce7e28 + 91af685 commit 099c0c8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Spies/AnyValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@

class AnyValue {
public $value = 'ANYTHING';

public function __toString() {
return 'AnyValue';
}
}
8 changes: 7 additions & 1 deletion src/Spies/ArgumentFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public function __toString() {
}

private function get_args_as_array() {
return implode( ', ', array_map( 'json_encode', $this->args ) );
$stringify_argument = function ( $argument ) {
if ( method_exists( $argument, '__toString' ) ) {
return $argument->__toString();
}
return json_encode( $argument );
};
return implode( ', ', array_map( $stringify_argument, $this->args ) );
}
}
4 changes: 4 additions & 0 deletions src/Spies/MatchArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@ private function is_associative( $arr ) {
}
return array_keys( $arr ) !== range( 0, count( $arr ) - 1 );
}

public function __toString() {
return 'MatchArray(' . json_encode( $this->expected_array ) . ')';
}
}

6 changes: 4 additions & 2 deletions src/Spies/MatchPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public function is_match( $actual ) {
}
return false;
}
}


public function __toString() {
return "MatchPattern('{$this->expected_pattern}')";
}
}
21 changes: 21 additions & 0 deletions tests/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ public function test__with__and__match_pattern__is_not_met_if_the_spy_is_called_
$this->assertFalse( $expectation->met_expectations() );
}

public function test__match_pattern__reports_fail_message_on_fail() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_pattern( '/Bart/i' ) );
$spy( 'foo', 'slartiblargfast' );
$this->assertContains( 'Failed asserting that a spy is called with arguments: ( "foo", MatchPattern(\'/Bart/i\') )', $expectation->get_fail_message() );
}

public function test__with__and__match_array__is_met_if_the_spy_is_called_with_matching_key_values() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_array( [ 'foo' => 'bar', 'flim' => 'flam' ] ) );
Expand Down Expand Up @@ -335,13 +342,27 @@ public function test__with__and__match_array__is_not_met_if_the_spy_is_called_wi
$this->assertFalse( $expectation->met_expectations() );
}

public function test__match_array__reports_fail_message_on_fail() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\match_array( [ 'flim' => 'flam' ] ) );
$spy( 'foo', [ 'bar' => 'baz', 'foo' => 'bar' ] );
$this->assertContains( 'Failed asserting that a spy is called with arguments: ( "foo", MatchArray({"flim":"flam"}) )', $expectation->get_fail_message() );
}

public function test__with__and__any__is_met_if_the_spy_is_called_with_any_arguments() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\any() );
$spy( 'foo', 'bar' );
$this->assertTrue( $expectation->met_expectations() );
}

public function test__any__reports_fail_message_on_fail() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', \Spies\any() );
$spy( 'bar' );
$this->assertContains( 'Failed asserting that a spy is called with arguments: ( "foo", AnyValue )', $expectation->get_fail_message() );
}

public function test__with__is_not_met_if_the_spy_is_called_with_no_arguments() {
$spy = \Spies\make_spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'foo', 'bar' );
Expand Down

0 comments on commit 099c0c8

Please sign in to comment.