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

Query param matcher API #31

Merged
merged 4 commits into from May 20, 2016
Merged

Query param matcher API #31

merged 4 commits into from May 20, 2016

Conversation

lstrojny
Copy link
Contributor

@lstrojny lstrojny commented Apr 27, 2016

Should allow to check for the existence of query params. Q: should there be an API to do a full match?

@michaelherold
Copy link

I know that a "partial match" would be handy because it could cut down on some typing. But the basic grammar is enough to start using it.

@lstrojny
Copy link
Contributor Author

Can you elaborate?

@michaelherold
Copy link

I'm primarily a Ruby developer. In RSpec, one of the Ruby testing frameworks, you can "partially match" a hash (in PHP, an associative array) with something like this:

require "rspec/autorun"

class Demo
  def initialize(arg)
    @arg = arg
  end
end

RSpec.describe "hash_including" do
  it "partially matches hashes" do
    hash = {:key => "value", :key2 => "value2"}

    expect(Demo).to receive(:new).with hash_including(:key => "value")

    Demo.new(hash)
  end
end

This spec checks to make sure that Demo.new is called with a hash including the key :key and the value "value". If it's called with any hash with that key/value combination, the spec will pass.

I was picturing something similar for checking query parameters in this context. Something like:

$this->http->mock
    ->when()
        ->methodIs("GET")
        ->pathIs("/my/test/path")
        ->queryContains(array("param1" => "test1", "param2" => "test2"))
    ->then()->body("{}")->statusCode(200)
    ->end();

So you could call to /my/test/path?param1=test1&param2=test2&param3=test3 and it would still respond with this mock.

I see this being useful when you're making a client that always sets a particular parameter like access_token=mybearertoken. However, this is really just sugar on what you already have, since this can be expressed equivalently with:

$this->http->mock
    ->when()
        ->methodIs("GET")
        ->pathIs("/my/test/path")
        ->queryParamIs("param1", "test1")
        ->queryParamIs("param2", "test2")
    ->then()->body("{}")->statusCode(200)
    ->end();

Does that make sense?

@lstrojny
Copy link
Contributor Author

lstrojny commented Apr 28, 2016

Understood. So we could add a queryParamsAre(array …) and queryParamseExist(array …) methods. What do you think?

@lstrojny
Copy link
Contributor Author

Ping! Can you have a look again, if that’s what you need?

@lstrojny lstrojny force-pushed the feature/query-param-matcher branch from 7b5a539 to 2532b6d Compare May 19, 2016 21:35
@lstrojny lstrojny force-pushed the feature/query-param-matcher branch from 9dd8df8 to a6cb7fa Compare May 19, 2016 21:37
@michaelherold
Copy link

This looks fantastic! Thanks for taking the time to add it. 👍

@lstrojny lstrojny merged commit d986aff into master May 20, 2016
@lstrojny lstrojny deleted the feature/query-param-matcher branch May 20, 2016 18:59
@lstrojny lstrojny changed the title [WIP] Query param matcher API Query param matcher API May 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants