14
14
use Behat \Gherkin \Node \TableNode ;
15
15
use GuzzleHttp \ClientInterface ;
16
16
use GuzzleHttp \Exception \RequestException ;
17
+ use GuzzleHttp \Psr7 \Request ;
17
18
use PHPUnit_Framework_Assert as Assertions ;
19
+ use Psr \Http \Message \RequestInterface ;
20
+ use Psr \Http \Message \ResponseInterface ;
18
21
19
22
/**
20
23
* Provides web API description definitions.
@@ -39,12 +42,12 @@ class WebApiContext implements ApiClientAwareContext
39
42
private $ headers = array ();
40
43
41
44
/**
42
- * @var \GuzzleHttp\Message\RequestInterface
45
+ * @var \GuzzleHttp\Message\RequestInterface|RequestInterface
43
46
*/
44
47
private $ request ;
45
48
46
49
/**
47
- * @var \GuzzleHttp\Message\ResponseInterface
50
+ * @var \GuzzleHttp\Message\ResponseInterface|ResponseInterface
48
51
*/
49
52
private $ response ;
50
53
@@ -97,9 +100,14 @@ public function iSetHeaderWithValue($name, $value)
97
100
public function iSendARequest ($ method , $ url )
98
101
{
99
102
$ url = $ this ->prepareUrl ($ url );
100
- $ this ->request = $ this ->getClient ()->createRequest ($ method , $ url );
101
- if (!empty ($ this ->headers )) {
102
- $ this ->request ->addHeaders ($ this ->headers );
103
+
104
+ if (version_compare (ClientInterface::VERSION , '6.0 ' , '>= ' )) {
105
+ $ this ->request = new Request ($ method , $ url , $ this ->headers );
106
+ } else {
107
+ $ this ->request = $ this ->getClient ()->createRequest ($ method , $ url );
108
+ if (!empty ($ this ->headers )) {
109
+ $ this ->request ->addHeaders ($ this ->headers );
110
+ }
103
111
}
104
112
105
113
$ this ->sendRequest ();
@@ -126,9 +134,14 @@ public function iSendARequestWithValues($method, $url, TableNode $post)
126
134
$ bodyOption = array (
127
135
'body ' => json_encode ($ fields ),
128
136
);
129
- $ this ->request = $ this ->getClient ()->createRequest ($ method , $ url , $ bodyOption );
130
- if (!empty ($ this ->headers )) {
131
- $ this ->request ->addHeaders ($ this ->headers );
137
+
138
+ if (version_compare (ClientInterface::VERSION , '6.0 ' , '>= ' )) {
139
+ $ this ->request = new Request ($ method , $ url , $ this ->headers , $ bodyOption ['body ' ]);
140
+ } else {
141
+ $ this ->request = $ this ->getClient ()->createRequest ($ method , $ url , $ bodyOption );
142
+ if (!empty ($ this ->headers )) {
143
+ $ this ->request ->addHeaders ($ this ->headers );
144
+ }
132
145
}
133
146
134
147
$ this ->sendRequest ();
@@ -148,14 +161,19 @@ public function iSendARequestWithBody($method, $url, PyStringNode $string)
148
161
$ url = $ this ->prepareUrl ($ url );
149
162
$ string = $ this ->replacePlaceHolder (trim ($ string ));
150
163
151
- $ this ->request = $ this ->getClient ()->createRequest (
152
- $ method ,
153
- $ url ,
154
- array (
155
- 'headers ' => $ this ->getHeaders (),
156
- 'body ' => $ string ,
157
- )
158
- );
164
+ if (version_compare (ClientInterface::VERSION , '6.0 ' , '>= ' )) {
165
+ $ this ->request = new Request ($ method , $ url , $ this ->headers , $ string );
166
+ } else {
167
+ $ this ->request = $ this ->getClient ()->createRequest (
168
+ $ method ,
169
+ $ url ,
170
+ array (
171
+ 'headers ' => $ this ->getHeaders (),
172
+ 'body ' => $ string ,
173
+ )
174
+ );
175
+ }
176
+
159
177
$ this ->sendRequest ();
160
178
}
161
179
@@ -175,11 +193,16 @@ public function iSendARequestWithFormData($method, $url, PyStringNode $body)
175
193
176
194
$ fields = array ();
177
195
parse_str (implode ('& ' , explode ("\n" , $ body )), $ fields );
178
- $ this ->request = $ this ->getClient ()->createRequest ($ method , $ url );
179
- /** @var \GuzzleHttp\Post\PostBodyInterface $requestBody */
180
- $ requestBody = $ this ->request ->getBody ();
181
- foreach ($ fields as $ key => $ value ) {
182
- $ requestBody ->setField ($ key , $ value );
196
+
197
+ if (version_compare (ClientInterface::VERSION , '6.0 ' , '>= ' )) {
198
+ $ this ->request = new Request ($ method , $ url , ['Content-Type ' => 'application/x-www-form-urlencoded ' ], http_build_query ($ fields , null , '& ' ));
199
+ } else {
200
+ $ this ->request = $ this ->getClient ()->createRequest ($ method , $ url );
201
+ /** @var \GuzzleHttp\Post\PostBodyInterface $requestBody */
202
+ $ requestBody = $ this ->request ->getBody ();
203
+ foreach ($ fields as $ key => $ value ) {
204
+ $ requestBody ->setField ($ key , $ value );
205
+ }
183
206
}
184
207
185
208
$ this ->sendRequest ();
@@ -241,14 +264,20 @@ public function theResponseShouldNotContain($text)
241
264
public function theResponseShouldContainJson (PyStringNode $ jsonString )
242
265
{
243
266
$ etalon = json_decode ($ this ->replacePlaceHolder ($ jsonString ->getRaw ()), true );
244
- $ actual = $ this ->response ->json ( );
267
+ $ actual = json_decode ( $ this ->response ->getBody (), true );
245
268
246
269
if (null === $ etalon ) {
247
270
throw new \RuntimeException (
248
271
"Can not convert etalon to json: \n" . $ this ->replacePlaceHolder ($ jsonString ->getRaw ())
249
272
);
250
273
}
251
274
275
+ if (null === $ actual ) {
276
+ throw new \RuntimeException (
277
+ "Can not convert actual to json: \n" . $ this ->replacePlaceHolder ((string ) $ this ->response ->getBody ())
278
+ );
279
+ }
280
+
252
281
Assertions::assertGreaterThanOrEqual (count ($ etalon ), count ($ actual ));
253
282
foreach ($ etalon as $ key => $ needle ) {
254
283
Assertions::assertArrayHasKey ($ key , $ actual );
@@ -269,9 +298,9 @@ public function printResponse()
269
298
echo sprintf (
270
299
"%s %s => %d: \n%s " ,
271
300
$ request ->getMethod (),
272
- $ request-> getUrl (),
301
+ ( string ) ( $ request instanceof RequestInterface ? $ request -> getUri () : $ request -> getUrl () ),
273
302
$ response ->getStatusCode (),
274
- $ response ->getBody ()
303
+ ( string ) $ response ->getBody ()
275
304
);
276
305
}
277
306
0 commit comments