Skip to content

Commit

Permalink
[REST] seeResponseContainsJson doesn't crash when json response is no…
Browse files Browse the repository at this point in the history
…t array (#4217)

* [PhpBrowserRestTest] converted arrays to short syntax

* [REST] seeResponseContainsJson don't crash when json response is not array #4202
  • Loading branch information
Naktibalda authored and DavertMik committed May 20, 2017
1 parent 6808d28 commit 6249dd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/Codeception/Util/JsonArray.php
Expand Up @@ -35,6 +35,10 @@ public function __construct($jsonString)
json_last_error()
);
}

if (!is_array($this->jsonArray)) {
throw new \PHPUnit_Framework_AssertionFailedError('JSON response is not an array: ' . $jsonString);
}
}

public function toXml()
Expand Down
27 changes: 18 additions & 9 deletions tests/unit/Codeception/Module/PhpBrowserRestTest.php
Expand Up @@ -19,7 +19,7 @@ public function setUp()
{
$this->phpBrowser = new \Codeception\Module\PhpBrowser(make_container());
$url = 'http://localhost:8010';
$this->phpBrowser->_setConfig(array('url' => $url));
$this->phpBrowser->_setConfig(['url' => $url]);
$this->phpBrowser->_initialize();

$this->module = Stub::make('\Codeception\Module\REST');
Expand All @@ -42,7 +42,7 @@ public function testGet()
$this->module->sendGET('/rest/user/');
$this->module->seeResponseIsJson();
$this->module->seeResponseContains('davert');
$this->module->seeResponseContainsJson(array('name' => 'davert'));
$this->module->seeResponseContainsJson(['name' => 'davert']);
$this->module->seeResponseCodeIs(200);
$this->module->dontSeeResponseCodeIs(404);
}
Expand All @@ -55,9 +55,9 @@ public function testSendAbsoluteUrlGet()

public function testPost()
{
$this->module->sendPOST('/rest/user/', array('name' => 'john'));
$this->module->sendPOST('/rest/user/', ['name' => 'john']);
$this->module->seeResponseContains('john');
$this->module->seeResponseContainsJson(array('name' => 'john'));
$this->module->seeResponseContainsJson(['name' => 'john']);
}

public function testValidJson()
Expand Down Expand Up @@ -98,10 +98,10 @@ public function testSeeInJson()
'{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}, "labels": null}}'
);
$this->module->seeResponseIsJson();
$this->module->seeResponseContainsJson(array('name' => 'Davert'));
$this->module->seeResponseContainsJson(array('user' => array('name' => 'Davert')));
$this->module->seeResponseContainsJson(array('ticket' => array('title' => 'Bug should be fixed')));
$this->module->seeResponseContainsJson(array('ticket' => array('user' => array('name' => 'Davert'))));
$this->module->seeResponseContainsJson(['name' => 'Davert']);
$this->module->seeResponseContainsJson(['user' => ['name' => 'Davert']]);
$this->module->seeResponseContainsJson(['ticket' => ['title' => 'Bug should be fixed']]);
$this->module->seeResponseContainsJson(['ticket' => ['user' => ['name' => 'Davert']]]);
$this->module->seeResponseContainsJson(array('ticket' => array('labels' => null)));
}

Expand All @@ -116,7 +116,6 @@ public function testSeeInJsonCollection()
$this->module->seeResponseContainsJson(array('user' => 'John Doe', 'age' => 27));
}


public function testArrayJson()
{
$this->setStubResponse(
Expand All @@ -125,6 +124,16 @@ public function testArrayJson()
$this->module->seeResponseContainsJson(array('id' => 1));
}

/**
* @issue https://github.com/Codeception/Codeception/issues/4202
*/
public function testSeeResponseContainsJsonFailsGracefullyWhenJsonResultIsNotArray()
{
$this->shouldFail();
$this->setStubResponse(json_encode('no_status'));
$this->module->seeResponseContainsJson(array('id' => 1));
}

public function testDontSeeInJson()
{
$this->setStubResponse('{"ticket": {"title": "Bug should be fixed", "user": {"name": "Davert"}}}');
Expand Down

0 comments on commit 6249dd3

Please sign in to comment.