From e1915945db4143fb6102ad0497f771c5823dfa88 Mon Sep 17 00:00:00 2001 From: Fabien Bourigault Date: Wed, 19 Apr 2017 13:41:24 +0200 Subject: [PATCH] fix ProfileClient creation with callable factory --- Collector/ProfileClientFactory.php | 2 +- .../Collector/ProfileClientFactoryTest.php | 56 +++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 Tests/Unit/Collector/ProfileClientFactoryTest.php diff --git a/Collector/ProfileClientFactory.php b/Collector/ProfileClientFactory.php index b9422094..2fe4b2a3 100644 --- a/Collector/ProfileClientFactory.php +++ b/Collector/ProfileClientFactory.php @@ -51,7 +51,7 @@ public function __construct($factory, Collector $collector, Formatter $formatter */ public function createClient(array $config = []) { - $client = is_callable($this->factory) ? $this->factory($config) : $this->factory->createClient($config); + $client = is_callable($this->factory) ? call_user_func($this->factory, $config) : $this->factory->createClient($config); if (!($client instanceof HttpClient && $client instanceof HttpAsyncClient)) { $client = new FlexibleHttpClient($client); diff --git a/Tests/Unit/Collector/ProfileClientFactoryTest.php b/Tests/Unit/Collector/ProfileClientFactoryTest.php new file mode 100644 index 00000000..7c5186ae --- /dev/null +++ b/Tests/Unit/Collector/ProfileClientFactoryTest.php @@ -0,0 +1,56 @@ +collector = $this->getMockBuilder(Collector::class)->disableOriginalConstructor()->getMock(); + $this->formatter = $this->getMockBuilder(Formatter::class)->disableOriginalConstructor()->getMock(); + $this->client = $this->getMockBuilder(HttpClient::class)->getMock(); + } + + public function testCreateClientFromClientFactory() + { + $factory = $this->getMockBuilder(ClientFactory::class)->getMock(); + $factory->method('createClient')->willReturn($this->client); + + $subject = new ProfileClientFactory($factory, $this->collector, $this->formatter); + + $this->assertInstanceOf(ProfileClient::class, $subject->createClient()); + } + + public function testCreateClientFromCallable() + { + $factory = function ($config) { + return $this->client; + }; + + $subject = new ProfileClientFactory($factory, $this->collector, $this->formatter); + + $this->assertInstanceOf(ProfileClient::class, $subject->createClient()); + } +}