Skip to content

Commit 33d28d6

Browse files
authored
Merge pull request #27 from c960657/client-factory
Allow overriding all Guzzle configuration settings
2 parents 1290340 + 9f3f28b commit 33d28d6

File tree

2 files changed

+117
-18
lines changed

2 files changed

+117
-18
lines changed

src/Guzzle/ClientFactory.php

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ClientFactory implements LoggerAwareInterface {
2323
/**
2424
* @since 2.1.0
2525
*
26-
* @param array $config with possible keys:
26+
* @param array $config All configurtion settings supported by Guzzle, and these:
2727
* middleware => array of extra middleware to pass to guzzle
2828
* user-agent => string default user agent to use for requests
2929
*/
@@ -48,29 +48,36 @@ public function getClient() {
4848
* @return Client
4949
*/
5050
private function newClient() {
51-
$middlewareFactory = new MiddlewareFactory();
52-
$middlewareFactory->setLogger( $this->logger );
51+
$this->config += array(
52+
'cookies' => true,
53+
'headers' => array(),
54+
'middleware' => array(),
55+
);
5356

54-
$handlerStack = HandlerStack::create( new CurlHandler() );
55-
$handlerStack->push( $middlewareFactory->retry() );
57+
if( !array_key_exists( 'User-Agent', $this->config['headers'] ) ) {
58+
if( array_key_exists( 'user-agent', $this->config ) ) {
59+
$this->config['headers']['User-Agent'] = $this->config['user-agent'];
60+
} else {
61+
$this->config['headers']['User-Agent'] = 'Addwiki - mediawiki-api-base';
62+
}
63+
}
64+
unset( $this->config['user-agent'] );
5665

57-
if( array_key_exists( 'user-agent', $this->config ) ) {
58-
$ua = $this->config['user-agent'];
59-
} else {
60-
$ua = 'Addwiki - mediawiki-api-base';
66+
if( !array_key_exists( 'handler', $this->config ) ) {
67+
$this->config['handler'] = HandlerStack::create( new CurlHandler() );
6168
}
6269

63-
if( array_key_exists( 'middleware', $this->config ) ) {
64-
foreach( $this->config['middleware'] as $middleware ) {
65-
$handlerStack->push( $middleware );
66-
}
70+
$middlewareFactory = new MiddlewareFactory();
71+
$middlewareFactory->setLogger( $this->logger );
72+
73+
$this->config['middleware'][] = $middlewareFactory->retry();
74+
75+
foreach( $this->config['middleware'] as $name => $middleware ) {
76+
$this->config['handler']->push( $middleware );
6777
}
78+
unset( $this->config['middleware'] );
6879

69-
return new Client( array(
70-
'cookies' => true,
71-
'handler' => $handlerStack,
72-
'headers' => array( 'User-Agent' => $ua ),
73-
) );
80+
return new Client( $this->config );
7481
}
7582

7683
/**
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Mediawiki\Api\Test\Unit\Guzzle;
4+
5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\HandlerStack;
7+
use Mediawiki\Api\Guzzle\ClientFactory;
8+
use Psr\Http\Message\RequestInterface;
9+
10+
/**
11+
* @author Christian Schmidt
12+
*
13+
* @covers Mediawiki\Api\Guzzle\ClientFactory
14+
*/
15+
class ClientFactoryTest extends \PHPUnit_Framework_TestCase {
16+
17+
public function testNoConfig() {
18+
$clientFactory = new ClientFactory();
19+
20+
$client = $clientFactory->getClient();
21+
22+
$this->assertSame( $client, $clientFactory->getClient() );
23+
24+
$config = $client->getConfig();
25+
$this->assertEquals( $config['headers']['User-Agent'], 'Addwiki - mediawiki-api-base' );
26+
27+
$this->assertFalse( empty( $config['cookies'] ) );
28+
}
29+
30+
public function testUserAgent() {
31+
$clientFactory = new ClientFactory( array( 'user-agent' => 'Foobar' ) );
32+
33+
$client = $clientFactory->getClient();
34+
35+
$this->assertNull( $client->getConfig('user-agent') );
36+
37+
$config = $client->getConfig();
38+
$this->assertEquals( $config['headers']['User-Agent'], 'Foobar' );
39+
}
40+
41+
public function testHeaders() {
42+
$clientFactory = new ClientFactory( array(
43+
'headers' => array(
44+
'User-Agent' => 'Foobar',
45+
'X-Foo' => 'Bar',
46+
)
47+
) );
48+
49+
$client = $clientFactory->getClient();
50+
51+
$headers = $client->getConfig('headers');
52+
$this->assertCount( 2, $headers );
53+
$this->assertEquals( $headers['User-Agent'], 'Foobar' );
54+
$this->assertEquals( $headers['X-Foo'], 'Bar' );
55+
}
56+
57+
public function testHandler() {
58+
$handler = HandlerStack::create();
59+
60+
$clientFactory = new ClientFactory( array( 'handler' => $handler ) );
61+
62+
$client = $clientFactory->getClient();
63+
64+
$this->assertSame( $handler, $client->getConfig('handler') );
65+
}
66+
67+
public function testMiddleware() {
68+
$invoked = false;
69+
$middleware = function() use (&$invoked) {
70+
return function() use (&$invoked) {
71+
$invoked = true;
72+
};
73+
};
74+
75+
$clientFactory = new ClientFactory( array( 'middleware' => array( $middleware ) ) );
76+
77+
$client = $clientFactory->getClient();
78+
79+
$this->assertNull( $client->getConfig('middleware') );
80+
81+
$request = $this->getMockBuilder( RequestInterface::class )->getMock();
82+
83+
$handler = $client->getConfig( 'handler' );
84+
$handler->remove( 'http_errors' );
85+
$handler->remove( 'allow_redirects' );
86+
$handler->remove( 'cookies' );
87+
$handler->remove( 'prepare_body' );
88+
$handler($request, array());
89+
90+
$this->assertTrue( $invoked );
91+
}
92+
}

0 commit comments

Comments
 (0)