diff --git a/src/Spies/AnyValue.php b/src/Spies/AnyValue.php index 0855822..5f8d279 100644 --- a/src/Spies/AnyValue.php +++ b/src/Spies/AnyValue.php @@ -3,4 +3,8 @@ class AnyValue { public $value = 'ANYTHING'; + + public function __toString() { + return 'AnyValue'; + } } diff --git a/src/Spies/ArgumentFormatter.php b/src/Spies/ArgumentFormatter.php index 591b093..f40ca36 100644 --- a/src/Spies/ArgumentFormatter.php +++ b/src/Spies/ArgumentFormatter.php @@ -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 ) ); } } diff --git a/src/Spies/MatchArray.php b/src/Spies/MatchArray.php index 188e14e..c106d89 100644 --- a/src/Spies/MatchArray.php +++ b/src/Spies/MatchArray.php @@ -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 ) . ')'; + } } diff --git a/src/Spies/MatchPattern.php b/src/Spies/MatchPattern.php index ef8bd80..b1423c6 100644 --- a/src/Spies/MatchPattern.php +++ b/src/Spies/MatchPattern.php @@ -16,6 +16,8 @@ public function is_match( $actual ) { } return false; } -} - + public function __toString() { + return "MatchPattern('{$this->expected_pattern}')"; + } +} diff --git a/tests/ExpectationTest.php b/tests/ExpectationTest.php index f28987c..0212a36 100644 --- a/tests/ExpectationTest.php +++ b/tests/ExpectationTest.php @@ -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' ] ) ); @@ -335,6 +342,13 @@ 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() ); @@ -342,6 +356,13 @@ public function test__with__and__any__is_met_if_the_spy_is_called_with_any_argum $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' );