Skip to content

Commit ff62c55

Browse files
committed
Merge pull request Behat#35 from Soullivaneuh/guzzle-6
Guzzle 6 support
2 parents 5d8bccb + 0422549 commit ff62c55

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ php:
77
- 7.0
88
- hhvm
99

10+
matrix:
11+
fast_finish: true
12+
include:
13+
- php: 5.6
14+
env: GUZZLE_VERSION=4.*
15+
1016
before_install:
1117
# start a web server on port 8080, running in the background
1218
- bin/start_server.sh
1319

1420
before_script:
1521
- composer self-update
22+
- if [ "$GUZZLE_VERSION" != "" ]; then composer require "guzzlehttp/guzzle:${GUZZLE_VERSION}" --no-update; fi;
1623
- composer install --prefer-source
1724
- export LANG=C
1825
- export PATH=vendor/bin:$PATH

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"require": {
2020
"php": ">=5.4",
2121
"behat/behat": "~3.0",
22-
"guzzlehttp/guzzle": "4 - 5",
22+
"guzzlehttp/guzzle": "4 - 6",
2323
"phpunit/phpunit": "4 - 5"
2424
},
2525

src/Context/WebApiContext.php

+53-24
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
use Behat\Gherkin\Node\TableNode;
1515
use GuzzleHttp\ClientInterface;
1616
use GuzzleHttp\Exception\RequestException;
17+
use GuzzleHttp\Psr7\Request;
1718
use PHPUnit_Framework_Assert as Assertions;
19+
use Psr\Http\Message\RequestInterface;
20+
use Psr\Http\Message\ResponseInterface;
1821

1922
/**
2023
* Provides web API description definitions.
@@ -39,12 +42,12 @@ class WebApiContext implements ApiClientAwareContext
3942
private $headers = array();
4043

4144
/**
42-
* @var \GuzzleHttp\Message\RequestInterface
45+
* @var \GuzzleHttp\Message\RequestInterface|RequestInterface
4346
*/
4447
private $request;
4548

4649
/**
47-
* @var \GuzzleHttp\Message\ResponseInterface
50+
* @var \GuzzleHttp\Message\ResponseInterface|ResponseInterface
4851
*/
4952
private $response;
5053

@@ -97,9 +100,14 @@ public function iSetHeaderWithValue($name, $value)
97100
public function iSendARequest($method, $url)
98101
{
99102
$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+
}
103111
}
104112

105113
$this->sendRequest();
@@ -126,9 +134,14 @@ public function iSendARequestWithValues($method, $url, TableNode $post)
126134
$bodyOption = array(
127135
'body' => json_encode($fields),
128136
);
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+
}
132145
}
133146

134147
$this->sendRequest();
@@ -148,14 +161,19 @@ public function iSendARequestWithBody($method, $url, PyStringNode $string)
148161
$url = $this->prepareUrl($url);
149162
$string = $this->replacePlaceHolder(trim($string));
150163

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+
159177
$this->sendRequest();
160178
}
161179

@@ -175,11 +193,16 @@ public function iSendARequestWithFormData($method, $url, PyStringNode $body)
175193

176194
$fields = array();
177195
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+
}
183206
}
184207

185208
$this->sendRequest();
@@ -241,14 +264,20 @@ public function theResponseShouldNotContain($text)
241264
public function theResponseShouldContainJson(PyStringNode $jsonString)
242265
{
243266
$etalon = json_decode($this->replacePlaceHolder($jsonString->getRaw()), true);
244-
$actual = $this->response->json();
267+
$actual = json_decode($this->response->getBody(), true);
245268

246269
if (null === $etalon) {
247270
throw new \RuntimeException(
248271
"Can not convert etalon to json:\n" . $this->replacePlaceHolder($jsonString->getRaw())
249272
);
250273
}
251274

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+
252281
Assertions::assertGreaterThanOrEqual(count($etalon), count($actual));
253282
foreach ($etalon as $key => $needle) {
254283
Assertions::assertArrayHasKey($key, $actual);
@@ -269,9 +298,9 @@ public function printResponse()
269298
echo sprintf(
270299
"%s %s => %d:\n%s",
271300
$request->getMethod(),
272-
$request->getUrl(),
301+
(string) ($request instanceof RequestInterface ? $request->getUri() : $request->getUrl()),
273302
$response->getStatusCode(),
274-
$response->getBody()
303+
(string) $response->getBody()
275304
);
276305
}
277306

src/ServiceContainer/WebApiExtension.php

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Behat\Behat\Context\ServiceContainer\ContextExtension;
1414
use Behat\Testwork\ServiceContainer\Extension as ExtensionInterface;
1515
use Behat\Testwork\ServiceContainer\ExtensionManager;
16+
use GuzzleHttp\ClientInterface;
1617
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
1718
use Symfony\Component\DependencyInjection\ContainerBuilder;
1819
use Symfony\Component\DependencyInjection\Definition;
@@ -68,6 +69,18 @@ public function load(ContainerBuilder $container, array $config)
6869

6970
private function loadClient(ContainerBuilder $container, $config)
7071
{
72+
// Guzzle 6 BC bridge
73+
if (version_compare(ClientInterface::VERSION, '6.0', '>=')) {
74+
$config['base_uri'] = $config['base_url'];
75+
unset($config['bar_url']);
76+
77+
if (isset($config['defaults'])) {
78+
$defaults = $config['defaults'];
79+
unset($config['defaults']);
80+
$config = array_merge($config, $defaults);
81+
}
82+
}
83+
7184
$definition = new Definition('GuzzleHttp\Client', array($config));
7285
$container->setDefinition(self::CLIENT_ID, $definition);
7386
}

0 commit comments

Comments
 (0)