diff --git a/ClientFactory/MockFactory.php b/ClientFactory/MockFactory.php new file mode 100644 index 00000000..25ca938b --- /dev/null +++ b/ClientFactory/MockFactory.php @@ -0,0 +1,40 @@ + + */ +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(Client::class)) { + throw new \LogicException('To use the mock adapter you need to install the "php-http/mock-client" package.'); + } + + if (!$this->client) { + $this->client = new Client(); + } + + return $this->client; + } +} diff --git a/DependencyInjection/HttplugExtension.php b/DependencyInjection/HttplugExtension.php index e8afe492..266506e5 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; @@ -40,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) { 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/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'); } /** 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\\": ""