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

andReturnArgument #1001

Merged
merged 10 commits into from
Oct 9, 2019
Merged

andReturnArgument #1001

merged 10 commits into from
Oct 9, 2019

Conversation

downsider
Copy link
Contributor

Added functionality to return an argument passed to a method expectation

@downsider
Copy link
Contributor Author

Resolves #992

if (isset($args[$index])) {
return $args[$index];
}
throw new Exception("Cannot return an argument value. No argument exists for the index $index");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not 100% sure if throwing an exception here is correct.

What do people think is the right behaviour when the argument index is out-of-bounds for a particular method call?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's a reasonable starting point. Could throw a OutOfBoundsException to be more explicit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense

throw new \InvalidArgumentException("Invalid argument index supplied. Index must be a positive integer.");
}
$closure = function (...$args) use ($index) {
if (isset($args[$index])) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably want array_key_exists here, the argument could legitimately be null. Test case to cover would be good too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, I'll update both

* @param int $index
* @return self
*/
public function andReturnArgument($index)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not 100% sure what would be best, but in other public methods, we use withArgs, rather than withArguments.

So would we better with andReturnArg or is Arg too ambiguous?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think andReturnArg is fine; I'm happy to keep things consistent.

@davedevelopment
Copy link
Collaborator

Sorry for the delay! Missed the notification on this one.

@downsider
Copy link
Contributor Author

No worries, thanks for the review

@downsider
Copy link
Contributor Author

@davedevelopment I've updated this MR according to your feedback

@davedevelopment davedevelopment merged commit d9d4fd0 into mockery:master Oct 9, 2019
@davedevelopment
Copy link
Collaborator

Thank you, great stuff

@robertbasic robertbasic mentioned this pull request Oct 24, 2019
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

3 participants