Skip to content

Commit

Permalink
Possibility to overwrite classnames with service container parameters
Browse files Browse the repository at this point in the history
added unittests
  • Loading branch information
Sebastian Blum committed Feb 2, 2016
1 parent 7efaca8 commit 346ea13
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 25 deletions.
40 changes: 18 additions & 22 deletions DependencyInjection/GuzzleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function load(array $configs, ContainerBuilder $container) {
$argument = array_merge($options['options'], $argument);
}

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

// set service name based on client name
Expand All @@ -85,16 +85,16 @@ public function load(array $configs, ContainerBuilder $container) {
protected function createHandler(ContainerBuilder $container, $name, array $config) {

$logServiceName = sprintf('guzzle_bundle.middleware.log.%s', $name);
$log = $this->createLogMiddleware($container);
$log = $this->createLogMiddleware();
$container->setDefinition($logServiceName, $log);

$headerServiceName = sprintf('guzzle_bundle.middleware.request_header.%s', $name);
$requestHeader = $this->createRequestHeaderMiddleware($container, $config['headers']);
$requestHeader = $this->createRequestHeaderMiddleware($config['headers']);
$container->setDefinition($headerServiceName, $requestHeader);

// Event Dispatching service
$eventServiceName = sprintf('guzzle_bundle.middleware.event_dispatch.%s', $name);
$eventService = $this->createEventMiddleware($container, $name);
$eventService = $this->createEventMiddleware($name);
$container->setDefinition($eventServiceName, $eventService);

$headerExpression = new Expression(sprintf("service('%s').attach()", $headerServiceName));
Expand All @@ -118,7 +118,7 @@ protected function createHandler(ContainerBuilder $container, $name, array $conf
$username = $wsseConfig['username'];
$password = $wsseConfig['password'];

$wsse = $this->createWsseMiddleware($container, $username, $password);
$wsse = $this->createWsseMiddleware($username, $password);
$wsseServiceName = sprintf('guzzle_bundle.middleware.wsse.%s', $name);

$container->setDefinition($wsseServiceName, $wsse);
Expand Down Expand Up @@ -149,7 +149,7 @@ protected function createHandler(ContainerBuilder $container, $name, array $conf
*/
protected function createLogger(ContainerBuilder $container) {

$logger = new Definition($container->getParameter('guzzle_bundle.logger.class'));
$logger = new Definition('%guzzle_bundle.logger.class%');

$container->setDefinition('guzzle_bundle.logger', $logger);

Expand All @@ -162,13 +162,11 @@ protected function createLogger(ContainerBuilder $container) {
* @author Florian Preusner
* @since 2015-07
*
* @param ContainerBuilder $container
*
* @return Definition
*/
protected function createLogMiddleware(ContainerBuilder $container) {
protected function createLogMiddleware() {

$log = new Definition($container->getParameter('guzzle_bundle.middleware.log.class'));
$log = new Definition('%guzzle_bundle.middleware.log.class%');
$log->addArgument(new Reference('guzzle_bundle.logger'));
$log->addArgument(new Reference('guzzle_bundle.formatter'));

Expand All @@ -181,14 +179,13 @@ protected function createLogMiddleware(ContainerBuilder $container) {
* @author Florian Preusner
* @since 2015-07
*
* @param ContainerBuilder $container
* @param array $headers
* @param array $headers
*
* @return Definition
*/
protected function createRequestHeaderMiddleware(ContainerBuilder $container, array $headers) {
protected function createRequestHeaderMiddleware(array $headers) {

$requestHeader = new Definition($container->getParameter('guzzle_bundle.middleware.request_header.class'));
$requestHeader = new Definition('%guzzle_bundle.middleware.request_header.class%');
$requestHeader->addArgument($this->cleanUpHeaders($headers));

return $requestHeader;
Expand All @@ -200,12 +197,12 @@ protected function createRequestHeaderMiddleware(ContainerBuilder $container, ar
* @author Chris Warner
* @since 2015-09
*
* @param ContainerBuilder $container
* @param string name
*
* @return Definition
*/
protected function createEventMiddleware(ContainerBuilder $container, $name) {
$eventMiddleWare = new Definition($container->getParameter('guzzle_bundle.middleware.event_dispatcher.class'));
protected function createEventMiddleware($name) {
$eventMiddleWare = new Definition('%guzzle_bundle.middleware.event_dispatcher.class%');
$eventMiddleWare->addArgument(new Reference('event_dispatcher'));
$eventMiddleWare->addArgument($name);

Expand All @@ -218,15 +215,14 @@ protected function createEventMiddleware(ContainerBuilder $container, $name) {
* @author Florian Preusner
* @since 2015-07
*
* @param ContainerBuilder $container
* @param string $username
* @param string $password
* @param string $username
* @param string $password
*
* @return Definition
*/
protected function createWsseMiddleware(ContainerBuilder $container, $username, $password) {
protected function createWsseMiddleware($username, $password) {

$wsse = new Definition($container->getParameter('guzzle_bundle.middleware.wsse.class'));
$wsse = new Definition('%guzzle_bundle.middleware.wsse.class%');
$wsse->setArguments([$username, $password]);

return $wsse;
Expand Down
22 changes: 22 additions & 0 deletions Tests/DependencyInjection/Fixtures/FakeWsseAuthMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace EightPoints\Bundle\GuzzleBundle\Tests\DependencyInjection\Fixtures;


/**
* Class FakeWsseAuthMiddleware
*
* @package EightPoints\Bundle\GuzzleBundle\Tests\DependencyInjection\Fixtures
* @author Sebastian Blum
*
* @since 2016-01
*/
class FakeWsseAuthMiddleware
{
public function attach()
{
return function (callable $handler) {

};
}
}
82 changes: 79 additions & 3 deletions Tests/DependencyInjection/GuzzleExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,97 @@

namespace EightPoints\Bundle\GuzzleBundle\Tests\DependencyInjection;

use EightPoints\Bundle\GuzzleBundle\DependencyInjection\GuzzleExtension;
use EightPoints\Bundle\GuzzleBundle\DependencyInjection\GuzzleExtension;
use EightPoints\Bundle\GuzzleBundle\Tests\DependencyInjection\Fixtures\FakeClient;
use EightPoints\Bundle\GuzzleBundle\Tests\DependencyInjection\Fixtures\FakeWsseAuthMiddleware;
use EightPoints\Guzzle\WsseAuthMiddleware;
use GuzzleHttp\Psr7\Uri;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Class GuzzleExtensionTest
*
* @package EightPoints\Bundle\GuzzleBundle\Tests\DependencyInjection
* @author Florian Preusner
* @author Sebastian Blum
*
* @version 2.1
* @since 2015-05
*/
class GuzzleExtensionTest extends \PHPUnit_Framework_TestCase {

public function test() {
public function testGuzzleExtension()
{
$container = $this->createContainer();
$extension = new GuzzleExtension();
$extension->load($this->getConfigs(), $container);

$this->markTestSkipped('implement me');
// test Client
$this->assertTrue($container->hasDefinition('guzzle.client.test_api'));
$testApi = $container->get('guzzle.client.test_api');
$this->assertInstanceOf('GuzzleHttp\Client', $testApi);
$this->assertEquals(new Uri('//api.domain.tld/path'), $testApi->getConfig('base_uri'));

// test Services
$this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.log.test_api'));
$this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.request_header.test_api'));
$this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.event_dispatch.test_api'));

// test WSSE Plugin
$this->assertTrue($container->hasDefinition('guzzle_bundle.middleware.wsse.test_api'));
$wsse = $container->get('guzzle_bundle.middleware.wsse.test_api');
$this->assertInstanceOf(WsseAuthMiddleware::class, $wsse);
$this->assertSame('my-user', $wsse->getUsername());
$this->assertSame('my-pass', $wsse->getPassword());
}

public function testOverwriteClasses()
{
$container = $this->createContainer();
$extension = new GuzzleExtension();
$extension->load($this->getConfigs(), $container);

$container->setParameter('guzzle.http_client.class', FakeClient::class);
$container->setParameter('guzzle_bundle.middleware.wsse.class', FakeWsseAuthMiddleware::class);

$client = $container->get('guzzle.client.test_api', FakeClient::class);
$this->assertInstanceOf(FakeClient::class, $client);

$wsse = $container->get('guzzle_bundle.middleware.wsse.test_api');
$this->assertInstanceOf(FakeWsseAuthMiddleware::class, $wsse);
}

/**
* @return ContainerBuilder
*/
private function createContainer()
{
$container = new ContainerBuilder();
$container->setParameter('kernel.debug', true);
$container->set('event_dispatcher', $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));

return $container;
}

/**
* @return array
*/
private function getConfigs()
{
return $configs = [
[
'clients' => [
'test_api' => [
'base_url' => '//api.domain.tld/path',
'plugin' => [
'wsse' => [
'username' => 'my-user',
'password' => 'my-pass',
],
],
],
],
],
];
}
} // end: GuzzleExtensionTest
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.0",
"eightpoints/guzzle-wsse-middleware": "~3.0",
"symfony/dependency-injection": "~2.3|~3.0",
"symfony/expression-language": "~2.3|~3.0",
"symfony/event-dispatcher": "~2.3|~3.0",
"symfony/http-kernel": "~2.3|~3.0",
"psr/log": "~1.0"
},
Expand Down

0 comments on commit 346ea13

Please sign in to comment.