-
Notifications
You must be signed in to change notification settings - Fork 17
Add conditional responses to mock client #27
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
Conversation
c71565f
to
d1c5d2e
Compare
As a first pass at this, does this seem like a good direction? I've not written any documentation yet, and a couple of the automated checks are failing but:
|
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.
cool, this looks good to me. can you please rebase on current master?
src/Client.php
Outdated
$callable = $result['callable']; | ||
|
||
/** | ||
* @var RequestMatcher $matcher |
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.
i think the phpfixer will be happy when you do @var $matcher RequestMatcher
(had to look that up in one of our projects :-) )
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.
I just tried that, it didn't seem to like that either.
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.
I think I've got the linter working now with single annotations directly above the variables.
src/Client.php
Outdated
|
||
/** | ||
* @var RequestMatcher $matcher | ||
* @var ResponseInterface|Exception $result |
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.
but i think this should be removed as there is no $result. also, the callable must not return an exception but throw it instead, or we violate the contract.
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.
The $result
comes from foreach ($this->conditionalResults as $result) {
, and though we are returning the result of the callable, the callable itself will actually throw the exception rather than returning it.
https://github.com/php-http/mock-client/pull/27/files#diff-ff73e042e738204c6da009e2ed19f783R130
Feel free to suggest another way of doing this if you'd prefer it.
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.
ah, then the annotation should be above the foreach. but you use result as an array, so i think its not a ResponseInterface|Exception.
can you put the matcher type annotation above the line where you assign $matcher?
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.
I've done that, and also removed the ResponseInterface|Exception one because you're right - that was an array.
I've changed that to @var callable
directly above the variable.
hi @adamquaile do you have time to wrap this up? |
Hi, yeah I should be able to get to this in the next few days, I think.
Thanks!
…On Sun, Jan 27, 2019, 13:50 David Buchmann ***@***.*** wrote:
hi @adamquaile <https://github.com/adamquaile> do you have time to wrap
this up?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#27 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAERKUrZjTrBqDS7n_oAuLFM0DfF3bpjks5vHa6QgaJpZM4YMSvx>
.
|
Following discussions at php-http#3 this adds a on method taking a RequestMatcher and then either a Response, an Exception or a callable. This allows for mock clients to not depend on the order the responses/exceptions are mocked. It also allows for responses to depend on some content in the request, avoiding duplication in some cases.
d1c5d2e
to
a23b1be
Compare
@dbu I've rebased from master, and left a couple of replies to your comments. It's not quite mergeable yet, but let me know your thoughts. Thanks. |
@dbu I've managed to resolve the code style issues, so I think it's ready now, except for the discussion on the |
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.
thanks a lot! this looks good to me.
can you please do the 2 todo in the merge request: add a changelog entry (only the what, not the how to use) and do a pull request to explain this feature in https://github.com/php-http/documentation/blob/master/clients/mock-client.rst ?
thanks a lot for this contribution! i like that we got 2 new features with this, not only the conditionals but also the callbacks. i tweaked some nitpicks and squashed the commits, then merged manually. i will tag a release now. could you please add a bit of documentation for this as well? i created php-http/documentation#255 to keep track of that. |
Great, thanks for the merge! This'll be a great help in a couple of things I'm working on. |
What's in this PR?
Following discussions at #3 this adds
a on method taking a RequestMatcher and then either a Response, an Exception or a
callable.
This allows for mock clients to not depend on the order the responses/exceptions
are mocked.
It also allows for responses to depend on some content in the request, avoiding
duplication in some cases.
Example Usage
Aside from the usage discussed in the linked issue, the callable allows for repeated similar responses to be mocked at once, like this:
Checklist