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

[6.x] Support Partial Mocks on Facades #30754

Merged
merged 1 commit into from Dec 4, 2019
Merged

Conversation

@hettiger
Copy link
Contributor

hettiger commented Dec 4, 2019

Suppose we have a custom Facade called SomeService and want to mock just a single method.
This situation is usually leading to an error as follows:

SomeService::shouldReceive('foo')->once();
SomeService::foo();
SomeService::bar(); // Mockery\Exception\BadMethodCallException : Received Mockery_2_App_SomeService::bar(), but no expectations were specified 

My patch adds support for partial mocks in two ways:

SomeService::shouldReceive('foo')->once();
SomeService::partialMock();
SomeService::foo(); // this call is mocked
SomeService::bar(); // this call is deferred to it's parent
SomeService::partialMock()->shouldReceive('foo')->once();
SomeService::foo(); // this call is mocked
SomeService::bar(); // this call is deferred to it's parent

It is already possible to do something very similar relying on undocumented behavior:

SomeService::shouldReceive('foo')->once();
SomeService::makePartial();
SomeService::foo(); // this call is mocked
SomeService::bar(); // this call is deferred to it's parent

However my IDE is yelling at me and I strongly prefer a supported option if possible.
Also the above workaround is not as flexible:

SomeService::makePartial()->shouldReceive('foo')->once(); // Error : Call to undefined method SomeService::makePartial()
SomeService::foo();
SomeService::bar();
@GrahamCampbell GrahamCampbell changed the title Support Partial Mocks on Facades [6.x] Support Partial Mocks on Facades Dec 4, 2019
@taylorotwell taylorotwell merged commit d69ac3d into laravel:6.x Dec 4, 2019
2 checks passed
2 checks passed
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.