From dbc3442dbe56960a83a431890322b7455619d8e0 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Mon, 19 Nov 2018 20:25:57 +0100 Subject: [PATCH] Every client should have its own Mock instance --- ClientFactory/MockFactory.php | 19 +------------- Resources/config/mock-client.xml | 7 +---- Tests/Unit/ClientFactory/MockFactoryTest.php | 6 ----- .../HttplugExtensionTest.php | 26 ++++++++++++++++++- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/ClientFactory/MockFactory.php b/ClientFactory/MockFactory.php index 25ca938b..0938bfff 100644 --- a/ClientFactory/MockFactory.php +++ b/ClientFactory/MockFactory.php @@ -9,19 +9,6 @@ */ class MockFactory implements ClientFactory { - /** - * @var Client - */ - private $client; - - /** - * @param Client $client - */ - public function setClient(Client $client) - { - $this->client = $client; - } - /** * {@inheritdoc} */ @@ -31,10 +18,6 @@ public function createClient(array $config = []) 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; + return new Client(); } } diff --git a/Resources/config/mock-client.xml b/Resources/config/mock-client.xml index 81d714b0..e2daa2b6 100644 --- a/Resources/config/mock-client.xml +++ b/Resources/config/mock-client.xml @@ -4,11 +4,6 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - - - - - + diff --git a/Tests/Unit/ClientFactory/MockFactoryTest.php b/Tests/Unit/ClientFactory/MockFactoryTest.php index 77b21984..969d9327 100644 --- a/Tests/Unit/ClientFactory/MockFactoryTest.php +++ b/Tests/Unit/ClientFactory/MockFactoryTest.php @@ -23,11 +23,5 @@ public function testCreateClient() $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 801e2d61..fd76ddb3 100644 --- a/Tests/Unit/DependencyInjection/HttplugExtensionTest.php +++ b/Tests/Unit/DependencyInjection/HttplugExtensionTest.php @@ -147,7 +147,31 @@ public function testClientPlugins() $this->assertContainerBuilderHasService($id); } $this->assertContainerBuilderHasServiceDefinitionWithArgument('httplug.client.acme', 1, $pluginReferences); - $this->assertContainerBuilderHasService('httplug.client.mock'); + } + + public function testMocking() + { + $this->load([ + 'clients' => [ + 'ace' => [ + 'factory' => 'httplug.factory.mock', + ], + 'acme' => [ + 'factory' => 'httplug.factory.mock', + ], + ], + ]); + + $this->assertContainerBuilderHasService('httplug.client.ace'); + $this->assertContainerBuilderHasService('httplug.client.ace.client'); + + $this->assertContainerBuilderHasService('httplug.client.acme'); + $this->assertContainerBuilderHasService('httplug.client.acme.client'); + + $this->assertNotSame( + $this->container->get('httplug.client.ace.client'), + $this->container->get('httplug.client.acme.client') + ); } /**