Skip to content

Commit

Permalink
Merge pull request #17 from asprega/master
Browse files Browse the repository at this point in the history
Added support for Guzzle client options + configuration test
  • Loading branch information
Florian Preusner committed Aug 9, 2015
2 parents 10a7837 + f085214 commit 05052bc
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 3 deletions.
47 changes: 47 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ private function createClientsNode() {
$builder = new TreeBuilder();
$node = $builder->root('clients');

// Filtering function to cast scalar values to boolean
$boolFilter = function ($value) {
return (bool)$value;
};

$node->useAttributeAsKey('name')
->prototype('array')
->children()
Expand All @@ -87,6 +92,48 @@ private function createClientsNode() {
->end()
->end()

->arrayNode('options')
->children()
->scalarNode('cert')->end()
->scalarNode('connect_timeout')->end()
->booleanNode('debug')
->beforeNormalization()
->ifString()->then($boolFilter)
->end()
->end()
->booleanNode('decode_content')
->beforeNormalization()
->ifString()->then($boolFilter)
->end()
->end()
->scalarNode('delay')->end()
->booleanNode('http_errors')
->beforeNormalization()
->ifString()->then($boolFilter)
->end()
->end()
->scalarNode('expect')->end()
->scalarNode('ssl_key')->end()
->booleanNode('stream')
->beforeNormalization()
->ifString()->then($boolFilter)
->end()
->end()
->booleanNode('synchronous')
->beforeNormalization()
->ifString()->then($boolFilter)
->end()
->end()
->scalarNode('timeout')->end()
->booleanNode('verify')
->beforeNormalization()
->ifString()->then($boolFilter)
->end()
->end()
->scalarNode('version')->end()
->end()
->end()

->arrayNode('plugin')
->addDefaultsIfNotSet()
->children()
Expand Down
5 changes: 5 additions & 0 deletions DependencyInjection/GuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function load(array $configs, ContainerBuilder $container) {
'handler' => $this->createHandler($container, $name, $options)
];

// If present, add default options to the constructor argument for the Guzzle client
if (array_key_exists('options', $options)) {
$argument = array_merge($options['options'], $argument);
}

$client = new Definition($container->getParameter('guzzle.http_client.class'));
$client->addArgument($argument);

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ guzzle:
headers:
Accept: "application/json"

# guzzle client options (full description here: http://guzzle.readthedocs.org/en/latest/request-options.html)
# NOTE: "headers" option is not accepted here as it is provided as described above.
options:
timeout: 30

# plugin settings
plugin:
wsse:
Expand Down
42 changes: 40 additions & 2 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EightPoints\Bundle\GuzzleBundle\Tests\DependencyInjection;

use EightPoints\Bundle\GuzzleBundle\DependencyInjection\Configuration;
use Symfony\Component\Config\Definition\Processor;

/**
* Class ConfigurationTest
Expand All @@ -15,8 +16,45 @@
*/
class ConfigurationTest extends \PHPUnit_Framework_TestCase {

public function test() {
public function testSingleClientConfigWithOptions()
{
$config = [
'guzzle' => [
'clients' => [
'test_client' => [
'base_url' => 'http://baseurl/path',
'headers' => [
'Accept' => 'application/json'
],
'options' => [
'cert' => 'path/to/cert',
'connect_timeout' => 5,
'debug' => false,
'decode_content' => true,
'delay' => 1,
'http_errors' => false,
'expect' => true,
'ssl_key' => 'key',
'stream' => true,
'synchronous' => true,
'timeout' => 30,
'verify' => true,
'version' => '1.1'
],
'plugin' => [
'wsse' => [
'username' => 'user',
'password' => 'pass'
]
]
]
]
]
];

$this->markTestSkipped('implement me');
$processor = new Processor();
$processedConfig = $processor->processConfiguration(new Configuration(true), $config);

$this->assertEquals(array_merge($config['guzzle'], [ 'logging' => false ]), $processedConfig);
}
} // end: ConfigurationTest
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@
"require": {
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.0",
"eightpoints/guzzle-wsse-middleware": "~3.0"
"eightpoints/guzzle-wsse-middleware": "~3.0",
"symfony/http-kernel": "~2.3",
"psr/log": "~1.0"
},
"require-dev": {
"phpunit/phpunit": "*",
"symfony/config": "*"
},
"target-dir": "EightPoints/Bundle/GuzzleBundle",
"autoload": {
Expand Down

0 comments on commit 05052bc

Please sign in to comment.