Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 72cf6e4

Browse files
committed
feat: improved behat tests
1 parent ae75b12 commit 72cf6e4

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

‎features/book.feature‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
Feature: Book
22
Crud (REST) to /v1/books
33

4-
Scenario: GET /v1/books5
4+
Scenario: Library is empty: GET /v1/books
55
When I run GET on "v1/books"
66
Then the status code is 200
77
And the body is json
88
And the count of "data" is 0
99

10+
Scenario: Insert a book: POST /v1/books
11+
When I run POST on "v1/books" with json
12+
"""
13+
{
14+
"data": {
15+
"title": "Symfony is awesome",
16+
"pages": 981
17+
}
18+
}
19+
"""
20+
Then the status code is 200
21+
And the body is json
22+
And the count of "data" is 1
23+

‎tests/Behat/RestContext.php‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
use GuzzleHttp\Client;
1010
use GuzzleHttp\Exception\RequestException;
1111
use GuzzleHttp\Exception\TransferException;
12+
use InvalidArgumentException;
1213
use Peekmo\JsonPath\JsonStore;
1314
use Psr\Http\Message\ResponseInterface;
1415
use Symfony\Component\HttpFoundation\Request;
1516
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\HttpKernel\KernelInterface;
18+
use Behat\Gherkin\Node\PyStringNode;
1719

1820
/**
1921
* This context class contains the definitions of the steps used by the demo
@@ -43,12 +45,12 @@ public function __construct(KernelInterface $kernel, string $base_url)
4345
// throw new \Exception("nenne: ".$base_url);
4446
}
4547
/**
46-
* @When I run GET on :arg1
48+
* @When I run GET on :url
4749
*/
48-
public function iRunGetOn($arg1)
50+
public function iRunGet($url)
4951
{
5052
try {
51-
$this->_response = $this->_client->get($arg1);
53+
$this->_response = $this->_client->get($url);
5254

5355
} catch (RequestException $e) {
5456

@@ -63,6 +65,36 @@ public function iRunGetOn($arg1)
6365
}
6466
}
6567

68+
/**
69+
* @When I run POST on :url with json
70+
*/
71+
public function iRunPostWithJson($url, PyStringNode $string)
72+
{
73+
try {
74+
75+
$postParamArray = json_decode($string->getRaw(), true);
76+
if (json_last_error() != JSON_ERROR_NONE) {
77+
print $string->getRaw();
78+
throw new InvalidArgumentException("JSON not well formed: [" . json_last_error(). "]:" . json_last_error_msg());
79+
}
80+
$headers = [
81+
'json' => $postParamArray
82+
];
83+
84+
$response = $this->_client->post($url, $headers);
85+
$this->_response = $response;
86+
} catch (RequestException $e) { //4xx //5xx network
87+
if ($e->hasResponse()) {
88+
$this->_response = $e->getResponse();
89+
} else {
90+
throw $e;
91+
}
92+
} catch (\Exception $e) {
93+
throw $e;
94+
}
95+
}
96+
97+
6698
/**
6799
* @Then the status code is :arg1
68100
*/
@@ -118,8 +150,8 @@ public function theCountOfIs($path, $value)
118150
// $body=$this->_response->getBody()->getContents();
119151
// print_r($body);
120152
throw new \Exception("the body does not contain: $path");
121-
} elseif(count($res[0]) != $value) {
122-
throw new \Exception("$path is not ".count($res[0]).", ". print_r($res[0], true));
153+
} elseif(count($res) != $value && !(count($res)==1 && empty($res[0]) && $value==0)) {
154+
throw new \Exception("$path is not $value, but ".count($res).": ". print_r($res, true));
123155
}
124156
}
125157

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /