Skip to content

Commit

Permalink
Merge pull request #642 from guzzle/fix-inject-emitter
Browse files Browse the repository at this point in the history
Added an emitter option to the Client
  • Loading branch information
mtdowling committed Apr 17, 2014
2 parents aae940a + 33ad112 commit c3a7917
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docs/clients.rst
Expand Up @@ -56,6 +56,11 @@ defaults
default headers (e.g., User-Agent), default query string parameters, SSL
configurations, and any other supported request options.

emitter
Specifies an event emitter (``GuzzleHttp\Event\EmitterInterface``) instance
to be used by the client to emit request events. This option is useful if
you need to inject an emitter with listeners/subscribers already attached.

Here's an example of creating a client with various options, including using
a mock adapter that just returns the result of a callable function and a
base URL that is a URI template with parameters.
Expand Down
4 changes: 4 additions & 0 deletions src/Client.php
Expand Up @@ -69,12 +69,16 @@ class Client implements ClientInterface
* - parallel_adapter: Adapter used to transfer requests in parallel
* - message_factory: Factory used to create request and response object
* - defaults: Default request options to apply to each request
* - emitter: Event emitter used for request events
*/
public function __construct(array $config = [])
{
$this->configureBaseUrl($config);
$this->configureDefaults($config);
$this->configureAdapter($config);
if (isset($config['emitter'])) {
$this->emitter = $config['emitter'];
}
}

/**
Expand Down
13 changes: 13 additions & 0 deletions tests/ClientTest.php
Expand Up @@ -103,6 +103,19 @@ public function testCanSpecifyMessageFactory()
$client->get();
}

public function testCanSpecifyEmitter()
{
$emitter = $this->getMockBuilder('GuzzleHttp\Event\EmitterInterface')
->setMethods(['listeners'])
->getMockForAbstractClass();
$emitter->expects($this->once())
->method('listeners')
->will($this->returnValue('foo'));

$client = new Client(['emitter' => $emitter]);
$this->assertEquals('foo', $client->getEmitter()->listeners());
}

public function testAddsDefaultUserAgentHeaderWithDefaultOptions()
{
$client = new Client(['defaults' => ['allow_redirects' => false]]);
Expand Down

0 comments on commit c3a7917

Please sign in to comment.