-
Notifications
You must be signed in to change notification settings - Fork 460
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 options to make all methods as stubs, returning null by default #446
Conversation
So we have the second part you desire as a global flag $foo = m::mock("Foo")
->shouldIgnoreMissing()
->disallowMockingNonExistentMethods(); |
Neither work as I expected (couldn't find class Foo {
public function bar() {
return 'bar';
}
} Using |
Sorry about the |
Ah, it seems the |
On my latest commit, I got that part covered. |
Needs rebasing so we're running with the new test setup. |
Rebased |
Please fix the cs issues as shown by StyleCI. |
You can download the diff by clicking the details link, and apply with using git if you want, or you can just manually apply the fixes. |
* | ||
* @return Mock | ||
*/ | ||
public function disallowMockingNonExistentMethods(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding methods to an interface is a breaking change btw. We'd need a 0.10 release for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to, there's no BC guarantee until 1.0 IMO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@GrahamCampbell I got your point.
The disallowMockingNonExistentMethods()
can be removed from the interface and become a unique method for Mockery\Mock
class.
But for Mock
class to be able to locally (per mock basis, without setting it from global configuration) disallow mocking non existent methods, the Mockery
class needs to have a way to read the local flag. Thus, adding mockingNonExistentMethodsAllowed()
to the MockInterface
is inevitable.
I'll try to look for a better way implementing this. Hopefully I can find one that doesn't need to change the interface :D
I think the |
When I see extra method calls, I start thinking we should simplify. Mockery::stub()? Should we be making this a first class citizen? |
I'm not really sure how you're gonna make Mockery::stub() works, is it the same like Mockery::mock()? |
@yeka Merely a thought. I'm not sure how possible or not it is without checking. |
Just a thought, how about making mock object as stubs by default? |
Do you mean like |
No, Given that my Current behavior: $foo = Mockery::mock('Foo');
$foo->bar(); This will result in Suggested default behavior: I don't know if many people make use of current behavior. But I'm just telling the idea anyway. |
IMO, that wouldn't be a mock any more it would be a spy, that's why we have I'm not keen on only being able to stub/mock existing methods, as it makes interface discovery more difficult for me, instead I prefer to use |
Understood. Btw, could you give some thought to my another pull request? |
Sorry, I should have said "once your PR gets merged", just 👍'd that PR, will let @padraic have his say but I'm ready to merge. |
Thanks, I'm a bit relaxed now :p |
I'm referring to this documentation of PHPUnit for creating mocked object that all methods are stubs, which return null by default: Unit Testing Tutorial (Do not call setMethods() & Passing an empty array)
This kind of feature doesn't seems currently available on Mockery.
One option that close to that is
shouldIgnoreMissing()
, but this option will always return null even when the method being called is not exists. This is not good when there's a typo in either unit test's class or actual class.The additional option of
makeStubs()
works alike, but will still throw error when a nonexistent method is called.