Skip to content

Improved: Now you can pass * at theResponseShouldContainJson() #46

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"php": ">=5.4",
"behat/behat": "~3.0",
"guzzlehttp/guzzle": "4 - 5",
"intellectsoft-uk/assert": "*@dev",
"phpunit/phpunit": "~4.0"
},

Expand All @@ -28,6 +29,11 @@
"silex/silex": "~1"
},

"repositories": [{
"type": "vcs",
"url": "[email protected]:intellectsoft-uk/assert.git"
}],

"autoload": {
"psr-4": {
"Behat\\WebApiExtension\\": "src/"
Expand Down
37 changes: 14 additions & 23 deletions src/Context/WebApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use PHPUnit_Framework_Assert as Assertions;
use Isdev\Assert\Assertion as JsonAssert;

/**
* Provides web API description definitions.
Expand All @@ -26,29 +27,29 @@ class WebApiContext implements ApiClientAwareContext
/**
* @var string
*/
private $authorization;
protected $authorization;

/**
* @var ClientInterface
*/
private $client;
protected $client;

/**
* @var array
*/
private $headers = array();
protected $headers = array();

/**
* @var \GuzzleHttp\Message\RequestInterface
*/
private $request;
protected $request;

/**
* @var \GuzzleHttp\Message\ResponseInterface
*/
private $response;
protected $response;

private $placeHolders = array();
protected $placeHolders = array();

/**
* {@inheritdoc}
Expand Down Expand Up @@ -124,7 +125,7 @@ public function iSendARequestWithValues($method, $url, TableNode $post)
}

$bodyOption = array(
'body' => json_encode($fields),
'body' => json_encode($fields),
);
$this->request = $this->getClient()->createRequest($method, $url, $bodyOption);
if (!empty($this->headers)) {
Expand Down Expand Up @@ -240,20 +241,10 @@ public function theResponseShouldNotContain($text)
*/
public function theResponseShouldContainJson(PyStringNode $jsonString)
{
$etalon = json_decode($this->replacePlaceHolder($jsonString->getRaw()), true);
$actual = $this->response->json();
$etalon = $this->replacePlaceHolder($jsonString->getRaw());
$actual = $this->response->getBody();

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

Assertions::assertGreaterThanOrEqual(count($etalon), count($actual));
foreach ($etalon as $key => $needle) {
Assertions::assertArrayHasKey($key, $actual);
Assertions::assertEquals($etalon[$key], $actual[$key]);
}
JsonAssert::jsonStringMatchJsonString($etalon, $actual);
}

/**
Expand Down Expand Up @@ -282,7 +273,7 @@ public function printResponse()
*
* @return string
*/
private function prepareUrl($url)
protected function prepareUrl($url)
{
return ltrim($this->replacePlaceHolder($url), '/');
}
Expand Down Expand Up @@ -358,7 +349,7 @@ protected function removeHeader($headerName)
}
}

private function sendRequest()
protected function sendRequest()
{
try {
$this->response = $this->getClient()->send($this->request);
Expand All @@ -371,7 +362,7 @@ private function sendRequest()
}
}

private function getClient()
protected function getClient()
{
if (null === $this->client) {
throw new \RuntimeException('Client has not been set in WebApiContext');
Expand Down