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

Implemented step theResponseShouldContainPartialJson #49

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion Behat/CommonContexts/WebApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,31 @@ public function theResponseShouldContainJson(PyStringNode $jsonString)
}
}

/**
* @Given /^the response should contain partial json:$/
*/
public function theResponseShouldContainPartialJson(PyStringNode $jsonString)
{

$etalon = json_decode($this->replacePlaceHolder($jsonString->getRaw()), true);
$actual = json_decode($this->browser->getLastResponse()->getContent(), true);

if (null === $etalon) {
throw new \RuntimeException(
"Can not convert etalon to json:\n".$this->replacePlaceHolder($jsonString->getRaw())
);
}
Copy link
Member

Choose a reason for hiding this comment

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

you also need to check `è$actual`` to avoid a fatal error on the typehint


$etalonKeys = array_keys($etalon);
$actualKeys = array_keys($actual);
$keys = array_intersect($etalonKeys, $actualKeys);
Copy link
Member

Choose a reason for hiding this comment

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

ths looks wrong to me. All keys of the etalon should be checked. Your current implementation will not fail if the returned response is missing an expected key

Copy link
Author

Choose a reason for hiding this comment

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

But this is the goal: I don't want to check all the keys of the returned response, only the ones of the etalon. But OK the implementation is over complicated.

Copy link
Member

Choose a reason for hiding this comment

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

the issue is that you were not checking all keys of the etalon either. Because of this line, you were checking that keys being in both are the same, not that keys being in the etalon are present in the output with the right value.

The following case would have passed:

Then the response should contain partial json:
  """
  { "foo": "bar" }
  """

when the response contains

{ "invalid": "response" }


foreach ($keys as $key) {
assertArrayHasKey($key, $etalon);
Copy link
Member

Choose a reason for hiding this comment

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

this does not make sense. you should assert what the response contain, not what the etalon contains

Copy link
Author

Choose a reason for hiding this comment

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

You are right.

assertEquals($etalon[$key], $actual[$key]);
}
}

/**
* Prints last response body.
*
Expand Down Expand Up @@ -279,7 +304,7 @@ protected function addHeader($header)
{
$this->headers[] = $header;
}

/**
* Removes a header identified by $headerName
*
Expand Down