Skip to content

Commit

Permalink
Merge pull request #1 from xurumelous/master
Browse files Browse the repository at this point in the history
Updated Guzzle to version 6.
  • Loading branch information
fernandocarletti committed Jun 21, 2015
2 parents eb39e37 + 9d27f24 commit 5aa4f35
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
composer.lock
vendor
.idea
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

before_install:
Expand All @@ -23,3 +23,8 @@ after_failure:
- cat server.log
- sudo cat /var/log/nginx/error.log
- sudo cat /var/log/nginx/access.log

matrix:
allow_failures:
- php: 7.0
- php: hhvm
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": ">=5.4",
"behat/behat": "~3.0",
"guzzlehttp/guzzle": "4 - 5",
"guzzlehttp/guzzle": "~6",
"phpunit/phpunit": "~4.0"
},
"require-dev": {
Expand Down
2 changes: 1 addition & 1 deletion features/testapp.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Test app verification
progress: ~
extensions:
Behat\WebApiExtension:
base_url: http://localhost:8080/
base_uri: http://localhost:8080/
suites:
default:
Expand Down
50 changes: 20 additions & 30 deletions src/Context/WebApiContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use PHPUnit_Framework_Assert as Assertions;
use GuzzleHttp\Psr7\Request;
use Psr\Http\Message\ResponseInterface;

/**
* Provides web API description definitions.
Expand All @@ -39,14 +41,14 @@ class WebApiContext implements ApiClientAwareContext
protected $headers = array();

/**
* @var \GuzzleHttp\Message\RequestInterface
* @var ResponseInterface
*/
protected $request;
protected $response;

/**
* @var \GuzzleHttp\Message\ResponseInterface
* @var Request
*/
protected $response;
protected $request;

protected $placeHolders = array();

Expand Down Expand Up @@ -97,10 +99,7 @@ public function iSetHeaderWithValue($name, $value)
public function iSendARequest($method, $url)
{
$url = $this->prepareUrl($url);
$this->request = $this->getClient()->createRequest($method, $url);
if (!empty($this->headers)) {
$this->request->addHeaders($this->headers);
}
$this->request = new Request($method, $url, $this->getHeaders());

$this->sendRequest();
}
Expand All @@ -123,13 +122,7 @@ public function iSendARequestWithValues($method, $url, TableNode $post)
$fields[$key] = $this->replacePlaceHolder($val);
}

$bodyOption = array(
'body' => json_encode($fields),
);
$this->request = $this->getClient()->createRequest($method, $url, $bodyOption);
if (!empty($this->headers)) {
$this->request->addHeaders($this->headers);
}
$this->request = new Request($method, $url, $this->getHeaders(), json_encode($fields));

$this->sendRequest();
}
Expand All @@ -147,15 +140,8 @@ public function iSendARequestWithBody($method, $url, PyStringNode $string)
{
$url = $this->prepareUrl($url);
$string = $this->replacePlaceHolder(trim($string));
$this->request = new Request($method, $url, $this->getHeaders(), $string);

$this->request = $this->getClient()->createRequest(
$method,
$url,
array(
'headers' => $this->getHeaders(),
'body' => $string,
)
);
$this->sendRequest();
}

Expand All @@ -174,14 +160,18 @@ public function iSendARequestWithFormData($method, $url, PyStringNode $body)
$body = $this->replacePlaceHolder(trim($body));

$fields = array();
$requestFields = [];
parse_str(implode('&', explode("\n", $body)), $fields);
$this->request = $this->getClient()->createRequest($method, $url);
/** @var \GuzzleHttp\Post\PostBodyInterface $requestBody */
$requestBody = $this->request->getBody();

foreach ($fields as $key => $value) {
$requestBody->setField($key, $value);
$requestFields[] = sprintf('%s=%s', urlencode($key), urlencode($value));
}

$requestBody = implode('&', $requestFields);

$headers = array_merge($this->getHeaders(), ['Content-Type' => 'application/x-www-form-urlencoded']);
$this->request = new Request($method, $url, $headers, $requestBody);

$this->sendRequest();
}

Expand Down Expand Up @@ -241,7 +231,7 @@ public function theResponseShouldNotContain($text)
public function theResponseShouldContainJson(PyStringNode $jsonString)
{
$etalon = json_decode($this->replacePlaceHolder($jsonString->getRaw()), true);
$actual = $this->response->json();
$actual = json_decode($this->response->getBody(), true);

if (null === $etalon) {
throw new \RuntimeException(
Expand All @@ -268,8 +258,8 @@ public function printResponse()

echo sprintf(
"%s %s => %d:\n%s",
$request->getMethod(),
$request->getUrl(),
$this->request->getMethod(),
$this->request->getUri(),
$response->getStatusCode(),
$response->getBody()
);
Expand Down
2 changes: 1 addition & 1 deletion src/ServiceContainer/WebApiExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function configure(ArrayNodeDefinition $builder)
$builder
->addDefaultsIfNotSet()
->children()
->scalarNode('base_url')
->scalarNode('base_uri')
->defaultValue('http://localhost')
->end()
->end()
Expand Down

0 comments on commit 5aa4f35

Please sign in to comment.