9
9
use GuzzleHttp \Client ;
10
10
use GuzzleHttp \Exception \RequestException ;
11
11
use GuzzleHttp \Exception \TransferException ;
12
+ use InvalidArgumentException ;
12
13
use Peekmo \JsonPath \JsonStore ;
13
14
use Psr \Http \Message \ResponseInterface ;
14
15
use Symfony \Component \HttpFoundation \Request ;
15
16
use Symfony \Component \HttpFoundation \Response ;
16
17
use Symfony \Component \HttpKernel \KernelInterface ;
18
+ use Behat \Gherkin \Node \PyStringNode ;
17
19
18
20
/**
19
21
* 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)
43
45
// throw new \Exception("nenne: ".$base_url);
44
46
}
45
47
/**
46
- * @When I run GET on :arg1
48
+ * @When I run GET on :url
47
49
*/
48
- public function iRunGetOn ( $ arg1 )
50
+ public function iRunGet ( $ url )
49
51
{
50
52
try {
51
- $ this ->_response = $ this ->_client ->get ($ arg1 );
53
+ $ this ->_response = $ this ->_client ->get ($ url );
52
54
53
55
} catch (RequestException $ e ) {
54
56
@@ -63,6 +65,36 @@ public function iRunGetOn($arg1)
63
65
}
64
66
}
65
67
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
+
66
98
/**
67
99
* @Then the status code is :arg1
68
100
*/
@@ -118,8 +150,8 @@ public function theCountOfIs($path, $value)
118
150
// $body=$this->_response->getBody()->getContents();
119
151
// print_r($body);
120
152
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 ));
123
155
}
124
156
}
125
157
0 commit comments