Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dump helper #1221

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions library/Mockery/CompositeExpectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions library/Mockery/Exception/Dump.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Mockery\Exception;

class Dump extends \Mockery\Exception
{
}
16 changes: 16 additions & 0 deletions library/Mockery/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,22 @@ public function andReturnTrue()
return $this->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
*
Expand Down
28 changes: 28 additions & 0 deletions tests/Mockery/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down