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 all commits
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
44 changes: 43 additions & 1 deletion Behat/CommonContexts/WebApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,48 @@ public function theResponseShouldContainJson(PyStringNode $jsonString)
}
}

/**
* Checks that response body contains JSON from PyString.
* Only the keys present in the etalon will be checked.
*
* @param PyStringNode $jsonString
*
* @Then /^(?: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


assertThat($actual, isType('array'));
$this->assertArrayPartiallyEquals($etalon, $actual);
}

/**
* Recursively checks that $actual contains the same key/values listed in $expected
* @param array $expected
* @param array $actual
*/
protected function assertArrayPartiallyEquals(array $expected, array $actual)
{
foreach ($expected as $key => $value) {
assertArrayHasKey($key, $actual);
if (is_array($value)) {
assertThat($actual[$key], isType('array'));
$this->assertArrayPartiallyEquals($expected[$key], $actual[$key]);
Copy link
Member

Choose a reason for hiding this comment

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

you need to assert that $actual[$key] is an array before calling the method to avoid a fatal error on the typehint

} else {
assertEquals($value, $actual[$key]);
}
}
}

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

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