Skip to content

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

Closed
wants to merge 7 commits into from

Conversation

adamquaile
Copy link
Contributor

@adamquaile adamquaile commented Nov 2, 2018

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets discussed in #3
Documentation Not written yet
License MIT

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:

$mock->on(
  new RequestMatcher('#widgets/[0-9]+#'),
  function(RequestInterface $request) {
    return $this->messageFactory->createResponse()->withBodyBasedOffIdInURL();
  }
)

Checklist

  • Updated CHANGELOG.md to describe BC breaks / deprecations | new feature | bugfix
  • Documentation pull request created (if not simply a bugfix)

@adamquaile
Copy link
Contributor Author

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:

  • scrutinizer just says "failed" without a reason
  • styleci suggests something I wouldn't agree with
  • Travis is failing on HHVM, I'm not familiar with this to think why that would be

Copy link
Contributor

@dbu dbu left a 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
Copy link
Contributor

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 :-) )

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 just tried that, it didn't seem to like that either.

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 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
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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?

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'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.

@dbu
Copy link
Contributor

dbu commented Jan 27, 2019

hi @adamquaile do you have time to wrap this up?

@adamquaile
Copy link
Contributor Author

adamquaile commented Jan 27, 2019 via email

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.
@adamquaile
Copy link
Contributor Author

@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.

@adamquaile
Copy link
Contributor Author

@dbu I've managed to resolve the code style issues, so I think it's ready now, except for the discussion on the switch. Sorry it's taken a while to get to.

Copy link
Contributor

@dbu dbu left a 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 ?

@dbu
Copy link
Contributor

dbu commented Feb 21, 2019

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.

@adamquaile
Copy link
Contributor Author

Great, thanks for the merge! This'll be a great help in a couple of things I'm working on.

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.

2 participants