From 2641a95a6ee43a44c6be2b70332bfc75ef1a6e06 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Wed, 3 Jan 2018 17:10:33 +0100 Subject: [PATCH 1/6] Add mock client factory for test purpose. --- ClientFactory/MockFactory.php | 38 +++++++++++++++++++ DependencyInjection/HttplugExtension.php | 10 +++++ Resources/config/services.xml | 1 + Tests/Unit/ClientFactory/MockFactoryTest.php | 33 ++++++++++++++++ .../HttplugExtensionTest.php | 1 + 5 files changed, 83 insertions(+) create mode 100644 ClientFactory/MockFactory.php create mode 100644 Tests/Unit/ClientFactory/MockFactoryTest.php diff --git a/ClientFactory/MockFactory.php b/ClientFactory/MockFactory.php new file mode 100644 index 00000000..719e6347 --- /dev/null +++ b/ClientFactory/MockFactory.php @@ -0,0 +1,38 @@ + + */ +class MockFactory implements ClientFactory +{ + /** + * @var Client + */ + private $client; + + /** + * @param Client $client + */ + public function setClient(Client $client) + { + $this->client = $client; + } + + /** + * {@inheritdoc} + */ + public function createClient(array $config = []) + { + if (!class_exists('Http\Mock\Client')) { + throw new \LogicException('To use the mock adapter you need to install the "php-http/mock-client" package.'); + } + + return $this->client ?: new Client(); + } +} diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index e8afe492..f3523f1c 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -106,6 +106,16 @@ private function configureClients(ContainerBuilder $container, array $config) $container->setAlias('httplug.client.default', 'httplug.client.'.$first); } } + + $mockClass = 'Http\Mock\Client'; + $mockServiceId = 'httplug.client.mock'; + + if (\class_exists($mockClass) && !$container->has($mockServiceId)) { + $container->register($mockServiceId, $mockClass)->setPublic($container->getParameter('kernel.debug')); + + $container->findDefinition('httplug.factory.mock') + ->addMethodCall('setClient', [new Reference($mockServiceId)]); + } } /** diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 194921b8..7f9cc86d 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -68,5 +68,6 @@ + diff --git a/Tests/Unit/ClientFactory/MockFactoryTest.php b/Tests/Unit/ClientFactory/MockFactoryTest.php new file mode 100644 index 00000000..77b21984 --- /dev/null +++ b/Tests/Unit/ClientFactory/MockFactoryTest.php @@ -0,0 +1,33 @@ + + */ +class MockFactoryTest extends TestCase +{ + public function testCreateClient() + { + $factory = new MockFactory(); + $client = $factory->createClient(); + + $this->assertInstanceOf(Client::class, $client); + + $client = new Client(); + + $factory->setClient($client); + + $this->assertEquals($client, $factory->createClient()); + } +} diff --git a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php index 686dae7c..384609ee 100644 --- a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -134,6 +134,7 @@ public function testClientPlugins() $this->assertContainerBuilderHasService($id); } $this->assertContainerBuilderHasServiceDefinitionWithArgument('httplug.client.acme', 1, $pluginReferences); + $this->assertContainerBuilderHasService('httplug.client.mock'); } /** From 3144ea8666b07af6e23eb6d4f78896baf55c277d Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Wed, 3 Jan 2018 17:45:13 +0100 Subject: [PATCH 2/6] Style fix --- ClientFactory/MockFactory.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/ClientFactory/MockFactory.php b/ClientFactory/MockFactory.php index 719e6347..45961758 100644 --- a/ClientFactory/MockFactory.php +++ b/ClientFactory/MockFactory.php @@ -5,8 +5,6 @@ use Http\Mock\Client; /** - * Class Http\HttplugBundle\ClientFactory\MockFactory - * * @author Gary PEGEOT */ class MockFactory implements ClientFactory From 13cbc2e3d407962bc1e22635b29a4c76a7a50802 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Thu, 4 Jan 2018 10:30:21 +0100 Subject: [PATCH 3/6] Create client if it doesn't exist. --- ClientFactory/MockFactory.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/ClientFactory/MockFactory.php b/ClientFactory/MockFactory.php index 45961758..25ca938b 100644 --- a/ClientFactory/MockFactory.php +++ b/ClientFactory/MockFactory.php @@ -27,10 +27,14 @@ public function setClient(Client $client) */ public function createClient(array $config = []) { - if (!class_exists('Http\Mock\Client')) { + if (!class_exists(Client::class)) { throw new \LogicException('To use the mock adapter you need to install the "php-http/mock-client" package.'); } - return $this->client ?: new Client(); + if (!$this->client) { + $this->client = new Client(); + } + + return $this->client; } } From 140e96acee66fc3ea2d176b7b473021d6105c9f6 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Thu, 4 Jan 2018 10:42:29 +0100 Subject: [PATCH 4/6] Remove mock factory definition when library is missing. --- DependencyInjection/HttplugExtension.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index f3523f1c..c3cde98c 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -12,6 +12,7 @@ use Http\Message\Authentication\BasicAuth; use Http\Message\Authentication\Bearer; use Http\Message\Authentication\Wsse; +use Http\Mock\Client as MockClient; use Psr\Http\Message\UriInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ChildDefinition; @@ -107,11 +108,14 @@ private function configureClients(ContainerBuilder $container, array $config) } } - $mockClass = 'Http\Mock\Client'; $mockServiceId = 'httplug.client.mock'; - if (\class_exists($mockClass) && !$container->has($mockServiceId)) { - $container->register($mockServiceId, $mockClass)->setPublic($container->getParameter('kernel.debug')); + if (!\class_exists(MockClient::class)) { + $container->removeDefinition('httplug.factory.mock'); + } + + if ($container->has('httplug.factory.mock') && !$container->has($mockServiceId)) { + $container->register($mockServiceId, MockClient::class)->setPublic($container->getParameter('kernel.debug')); $container->findDefinition('httplug.factory.mock') ->addMethodCall('setClient', [new Reference($mockServiceId)]); From dd1cb9a5febcff8e1cbb103f8aaed73d7c9522f0 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Thu, 4 Jan 2018 10:43:12 +0100 Subject: [PATCH 5/6] Add suggest with mock-client lib. --- composer.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/composer.json b/composer.json index 636ef6a2..7c38a6b3 100644 --- a/composer.json +++ b/composer.json @@ -60,6 +60,9 @@ "conflict": { "php-http/guzzle6-adapter": "<1.1" }, + "suggest": { + "php-http/mock-client": "Add this to your require-dev section to mock HTTP responses easily" + }, "autoload": { "psr-4": { "Http\\HttplugBundle\\": "" From c2f3ac67a262b4adba1747f56e39dfbcca8285bb Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Mon, 8 Jan 2018 11:21:15 +0100 Subject: [PATCH 6/6] Move service definition to a dedicated file. --- DependencyInjection/HttplugExtension.php | 16 +++------------- Resources/config/mock-client.xml | 14 ++++++++++++++ Resources/config/services.xml | 1 - 3 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 Resources/config/mock-client.xml diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index c3cde98c..266506e5 100644 --- a/DependencyInjection/HttplugExtension.php +++ b/DependencyInjection/HttplugExtension.php @@ -41,6 +41,9 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('services.xml'); $loader->load('plugins.xml'); + if (\class_exists(MockClient::class)) { + $loader->load('mock-client.xml'); + } // Register default services foreach ($config['classes'] as $service => $class) { @@ -107,19 +110,6 @@ private function configureClients(ContainerBuilder $container, array $config) $container->setAlias('httplug.client.default', 'httplug.client.'.$first); } } - - $mockServiceId = 'httplug.client.mock'; - - if (!\class_exists(MockClient::class)) { - $container->removeDefinition('httplug.factory.mock'); - } - - if ($container->has('httplug.factory.mock') && !$container->has($mockServiceId)) { - $container->register($mockServiceId, MockClient::class)->setPublic($container->getParameter('kernel.debug')); - - $container->findDefinition('httplug.factory.mock') - ->addMethodCall('setClient', [new Reference($mockServiceId)]); - } } /** diff --git a/Resources/config/mock-client.xml b/Resources/config/mock-client.xml new file mode 100644 index 00000000..81d714b0 --- /dev/null +++ b/Resources/config/mock-client.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 7f9cc86d..194921b8 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -68,6 +68,5 @@ -