From be94225af68a4ca5bc44bab1e4e13a4c8a5b54e4 Mon Sep 17 00:00:00 2001 From: Igor Mukhin Date: Mon, 30 Nov 2015 02:34:57 +0200 Subject: [PATCH 1/2] Improved: Now you can pass * at theResponseShouldContainJson() --- composer.json | 6 ++++++ src/Context/WebApiContext.php | 19 +++++-------------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 8ff7a15..878cd2c 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "php": ">=5.4", "behat/behat": "~3.0", "guzzlehttp/guzzle": "4 - 5", + "intellectsoft-uk/assert": "*@dev", "phpunit/phpunit": "~4.0" }, @@ -28,6 +29,11 @@ "silex/silex": "~1" }, + "repositories": [{ + "type": "vcs", + "url": "git@github.com:intellectsoft-uk/assert.git" + }], + "autoload": { "psr-4": { "Behat\\WebApiExtension\\": "src/" diff --git a/src/Context/WebApiContext.php b/src/Context/WebApiContext.php index f21b24b..095f066 100644 --- a/src/Context/WebApiContext.php +++ b/src/Context/WebApiContext.php @@ -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. @@ -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)) { @@ -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); } /** From 696a94be0c941fab29a66cf15e11a3f0accdcf03 Mon Sep 17 00:00:00 2001 From: Igor Mukhin Date: Tue, 31 Jan 2017 00:18:45 +0200 Subject: [PATCH 2/2] Changed: Methods and variables visibility - they are protected now to have ability to override --- src/Context/WebApiContext.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Context/WebApiContext.php b/src/Context/WebApiContext.php index 095f066..b5ed83f 100644 --- a/src/Context/WebApiContext.php +++ b/src/Context/WebApiContext.php @@ -27,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} @@ -273,7 +273,7 @@ public function printResponse() * * @return string */ - private function prepareUrl($url) + protected function prepareUrl($url) { return ltrim($this->replacePlaceHolder($url), '/'); } @@ -349,7 +349,7 @@ protected function removeHeader($headerName) } } - private function sendRequest() + protected function sendRequest() { try { $this->response = $this->getClient()->send($this->request); @@ -362,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');