From ad39792f947647984d6512043181eec79b834309 Mon Sep 17 00:00:00 2001 From: Markus Bachmann Date: Wed, 13 Jan 2016 15:23:01 +0100 Subject: [PATCH 1/5] Add getter/setter to the plugin client --- spec/PluginClientSpec.php | 15 +++++++++++++++ src/PluginClient.php | 30 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/spec/PluginClientSpec.php b/spec/PluginClientSpec.php index 28f3b6f..06a7cbc 100644 --- a/spec/PluginClientSpec.php +++ b/spec/PluginClientSpec.php @@ -69,4 +69,19 @@ function it_throws_loop_exception(HttpClient $client, RequestInterface $request) $this->shouldThrow('Http\Client\Plugin\Exception\LoopException')->duringSendRequest($request); } + + function it_adds_a_plugin(Plugin $plugin) + { + $this->addPlugin($plugin); + + $this->getPlugins()->shouldReturn([$plugin]); + } + + function it_sets_new_plugins() + { + $plugins = [new LoopPlugin]; + $this->setPlugins($plugins); + + $this->getPlugins()->shouldReturn($plugins); + } } diff --git a/src/PluginClient.php b/src/PluginClient.php index 2fed906..3edc1b4 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -61,6 +61,36 @@ public function __construct($client, array $plugins = [], array $options = []) $this->options = $this->configure($options); } + /** + * Adds a plugin to the client. + * + * @param Plugin $plugin + */ + public function addPlugin(Plugin $plugin) + { + $this->plugins[] = $plugin; + } + + /** + * Get all active plugins. + * + * @return Plugin[] + */ + public function getPlugins() + { + return $this->plugins; + } + + /** + * Assign plugins to the client. + * + * @param Plugin[] $plugins + */ + public function setPlugins(array $plugins = []) + { + $this->plugins = $plugins; + } + /** * {@inheritdoc} */ From 16c25b58c6c2cbaa2cf8e02d9b14da80d8721ad8 Mon Sep 17 00:00:00 2001 From: Markus Bachmann Date: Wed, 13 Jan 2016 15:50:31 +0100 Subject: [PATCH 2/5] Use a mock object instead of the LoopPlugin --- spec/PluginClientSpec.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/PluginClientSpec.php b/spec/PluginClientSpec.php index 06a7cbc..7e596a4 100644 --- a/spec/PluginClientSpec.php +++ b/spec/PluginClientSpec.php @@ -77,9 +77,9 @@ function it_adds_a_plugin(Plugin $plugin) $this->getPlugins()->shouldReturn([$plugin]); } - function it_sets_new_plugins() + function it_sets_new_plugins(Plugin $plugin) { - $plugins = [new LoopPlugin]; + $plugins = [$plugin]; $this->setPlugins($plugins); $this->getPlugins()->shouldReturn($plugins); From 9558ed786097fdcdda656efc4b728e65d6d2dae1 Mon Sep 17 00:00:00 2001 From: Markus Bachmann Date: Wed, 13 Jan 2016 15:50:50 +0100 Subject: [PATCH 3/5] Add validation for PluginClient::setPlugins method --- src/PluginClient.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PluginClient.php b/src/PluginClient.php index 3edc1b4..7c11e5f 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -57,7 +57,7 @@ public function __construct($client, array $plugins = [], array $options = []) throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient'); } - $this->plugins = $plugins; + $this->plugins = $this->setPlugins($plugins); $this->options = $this->configure($options); } @@ -88,7 +88,9 @@ public function getPlugins() */ public function setPlugins(array $plugins = []) { - $this->plugins = $plugins; + foreach ($plugins as $plugin) { + $this->addPlugin($plugin); + } } /** From a70d550914e7e81819f076af782bce2b6a35496b Mon Sep 17 00:00:00 2001 From: Markus Bachmann Date: Wed, 13 Jan 2016 16:11:14 +0100 Subject: [PATCH 4/5] Provide better docblock for PluginClient::addPlugin --- src/PluginClient.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PluginClient.php b/src/PluginClient.php index 7c11e5f..bf0783e 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -62,7 +62,7 @@ public function __construct($client, array $plugins = [], array $options = []) } /** - * Adds a plugin to the client. + * Append a plugin to the end of the queue. * * @param Plugin $plugin */ From 5208c06bcfb250b8380a71abd5c3544afdea8d6a Mon Sep 17 00:00:00 2001 From: Markus Bachmann Date: Wed, 13 Jan 2016 16:32:22 +0100 Subject: [PATCH 5/5] Fix tests --- src/PluginClient.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PluginClient.php b/src/PluginClient.php index bf0783e..5e4ef3f 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -31,7 +31,7 @@ final class PluginClient implements HttpClient, HttpAsyncClient * * @var Plugin[] */ - private $plugins; + private $plugins = []; /** * A list of options. @@ -57,7 +57,7 @@ public function __construct($client, array $plugins = [], array $options = []) throw new \RuntimeException('Client must be an instance of Http\\Client\\HttpClient or Http\\Client\\HttpAsyncClient'); } - $this->plugins = $this->setPlugins($plugins); + $this->setPlugins($plugins); $this->options = $this->configure($options); }