Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Simplified usage of will() #144

Closed
wants to merge 2 commits into from
Closed

Simplified usage of will() #144

wants to merge 2 commits into from

Conversation

fabian
Copy link
Contributor

@fabian fabian commented Nov 8, 2013

Enable use of will('something') instead of will($this->returnValue('something'))

Enable use of will('something') instead of will($this->returnValue('something'))
@makasim
Copy link
Contributor

makasim commented Nov 8, 2013

great addition. It would make will method usage convention compatible with with method one.

<?php
$mock->with($this->equalsTo($foo));
$mock->will($this->returnValue($foo));

// and

$mock->with($foo);
$mock->will($foo);

👍

@whatthejeff
Copy link
Collaborator

I've actually thought about this a lot in the past. What I think makes more sense is to replace $mock->will($this->returnValue($foo)); with $mock->willReturn($foo); since the point of this interface is to be more-or-less readable/fluent.

@whatthejeff
Copy link
Collaborator

I'd also like to see ->expects($this->any()) made optional so we could do something like:

$mock->method('doSomething')->willReturn('something');

@zwilias
Copy link
Contributor

zwilias commented Nov 14, 2013

I like @whatthejeff's proposal, makes a lot of sense to me.

@ciaranmcnulty
Copy link
Contributor

I've opened a separate PR (#146) for @whatthejeff's second proposal about making expects() optional - it makes very good sense to me.

@ciaranmcnulty
Copy link
Contributor

On this subject it would be nice to allow both will('foo') and willReturn('foo') as aliases.

The former fits with the existing pattern for with(), the latter is more verbose and clear. It would be nice to give authors the option.

@@ -59,8 +59,8 @@
* Stubs the matching method with the stub object $stub. Any invocations of
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a comment about what happens with a scalar

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added below.

@whatthejeff
Copy link
Collaborator

These might be nice too:

$stub->method('doSomething')->willReturnMap(...);
$stub->method('doSomething')->willReturnArgument(...);
$stub->method('doSomething')->willReturnCallback(...);
$stub->method('doSomething')->willReturnSelf(...);
$stub->method('doSomething')->willReturnOnConsecutiveCalls(...);
$stub->method('doSomething')->willThrowException(...);

@ciaranmcnulty
Copy link
Contributor

I like @whatthejeff's suggestions, don't think they block merging this though? Then either @fabian or someone else could do a separate PR for the longer versions.

@sebastianbergmann
Copy link
Owner

I prefer the syntax proposed by @whatthejeff.

@ciaranmcnulty
Copy link
Contributor

@fabian Do you want to implement these suggestions? Otherwise I can?

@fabian
Copy link
Contributor Author

fabian commented Dec 12, 2013

Yes, already started, didn't find the time to finish though: https://github.com/fabian/phpunit-mock-objects/commits/willReturn

Would love to get some feedback before I create a PR.

@fabian fabian mentioned this pull request Dec 12, 2013
@whatthejeff
Copy link
Collaborator

Closing in favor of #147.

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

Successfully merging this pull request may close these issues.

6 participants