diff --git a/library/Mockery/CompositeExpectation.php b/library/Mockery/CompositeExpectation.php index 52314322c..3b65cb926 100644 --- a/library/Mockery/CompositeExpectation.php +++ b/library/Mockery/CompositeExpectation.php @@ -59,6 +59,23 @@ public function andReturns(...$args) return call_user_func_array([$this, 'andReturn'], $args); } + /** + * Dump the args supplied by the method call. + * + * @return self + */ + public function andDump() + { + foreach ($this->_expectations as $expectation) { + $expectation->andDump(); + } + + // Return a new instance so the following modifiers do not affect the + // dumpable expectation. The new expectation listens for a string that + // is not a valid PHP method name. + return $this->getMock()->shouldReceive(' '); + } + /** * Intercept any expectation calls and direct against all expectations * diff --git a/library/Mockery/Exception/Dump.php b/library/Mockery/Exception/Dump.php new file mode 100644 index 000000000..a15fc03ed --- /dev/null +++ b/library/Mockery/Exception/Dump.php @@ -0,0 +1,7 @@ +andReturn(true); } + /** + * Dump the args supplied by the method call. + * + * @return self + */ + public function andDump() + { + $this->_closureQueue = [ + fn (...$args) => throw new Exception\Dump( + 'Called ' . \Mockery::formatArgs($this->_name, $args) + ), + ]; + + return clone $this; + } + /** * Set Exception class and arguments to that class to be thrown * diff --git a/tests/Mockery/ExpectationTest.php b/tests/Mockery/ExpectationTest.php index c688aec5f..6665c3ae9 100644 --- a/tests/Mockery/ExpectationTest.php +++ b/tests/Mockery/ExpectationTest.php @@ -260,6 +260,34 @@ public function testReturnsNullArgument() $this->assertNull($this->mock->foo(...$args)); } + public function testDumpsCalls() + { + $this->expectException(Mockery\Exception\Dump::class); + $this->expectExceptionMessage("Called foo('bar')"); + $this->mock->shouldReceive('foo')->andDump(); + $this->mock->foo('bar'); + } + + public function testDumperDoesNotBreakOtherListeners() + { + $this->mock->shouldReceive('foo')->andReturn('f'); + $this->mock->shouldReceive('bar')->andDump(); + $this->mock->shouldReceive('baz')->with('b')->andReturn('z'); + + $this->assertSame('f', $this->mock->foo()); + $this->assertSame('z', $this->mock->baz('b')); + + $this->expectException(Mockery\Exception\Dump::class); + $this->mock->bar(); + } + + public function testDumperIgnoresMatchers() + { + $this->expectException(Mockery\Exception\Dump::class); + $this->mock->shouldReceive('foo')->andDump()->with('baz'); + $this->mock->foo('bar'); + } + public function testExceptionOnInvalidArgumentIndexValue() { $this->expectException(\InvalidArgumentException::class);