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

Add dump helper #1221

wants to merge 9 commits into from

Conversation

tontonsb
Copy link
Contributor

@tontonsb tontonsb commented Mar 5, 2023

This PR proposes a new expectation method to help in debugging expectations. When your expectation doesn't match and you want to understand what was actually called, call ->andDump() on your expectation like this:

$connection
    ->shouldReceive('update')->andDump()
    ->with('arg1', 'arg2')
    ->once();

The dump method will throw an exception showing the called method, args and the stack trace.

The rest of the chain will have no effect as andDump() returns a clone of the expectation. Thus unlike with dumping in an andReturn or putting a dumping matcher inside the with(), this does not require commenting out or modifying your non-matching with and other method calls.

@tontonsb tontonsb marked this pull request as draft March 5, 2023 16:02
@tontonsb tontonsb marked this pull request as ready for review March 5, 2023 16:37
@tontonsb tontonsb mentioned this pull request Mar 5, 2023
@davedevelopment
Copy link
Collaborator

I like the concept, not sure on the implementation/api though.

As I understand, it will only work if the andDump is called before the arguments specification etc?

$connection
    ->shouldReceive('update')
    ->with('arg1', 'arg2')
    ->once()->andDump(); // probably wouldn't do anything if the expectation was not matched?

There's no doubt this is something Mockery needs, a clear way of determining why your expectation wasn't met and has come up in the past, with things like #1033 and #936.

Perhaps, this could be a something to start with and something we ship quickly, with the later view of something much better.

Could we think on the naming a little? I'm not against andDump, but wonder if we could be more specific. When I first read andDump, I assumed it would dump the arguments to output, but then carry on anyway, so I might see all of the calls within the test. I can't really think of anything better to name it, without it getting lengthy, but here are a couple of ideas in case anyone else can think of something:

$connection->shouldReceive('update')->thenDumpAndThrow();
$connection->shouldReceive('update')->thenThrowDebugException();

@tontonsb
Copy link
Contributor Author

tontonsb commented Mar 7, 2023

As I understand, it will only work if the andDump is called before the arguments specification etc?

Yes, it's somewhat of a "let's see how the chain behaves this far". You might also do ->with(\Mockery::on($matcherCallback))->andDump() to see the call that has been matched.

When I first read andDump, I assumed it would dump the arguments to output, but then carry on anyway

I actually tried doing things like that, but all of my attempts seemed to affect too many parts and seeing that #1000 was reverted I decided to go with the simplest approach so it wouldn't break anything.

in case anyone else can think of something

Yeah, a short and descriptive name would be nice :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants