Skip to content

Every client should have its own Mock instance #286

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions ClientFactory/MockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,6 @@
*/
class MockFactory implements ClientFactory
{
/**
* @var Client
*/
private $client;

/**
* @param Client $client
*/
public function setClient(Client $client)
{
$this->client = $client;
}

/**
* {@inheritdoc}
*/
Expand All @@ -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();
}
}
7 changes: 1 addition & 6 deletions Resources/config/mock-client.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="httplug.client.mock" class="Http\Mock\Client" public="true" />
<service id="httplug.factory.mock" class="Http\HttplugBundle\ClientFactory\MockFactory" public="false">
<call method="setClient">
<argument type="service" id="httplug.client.mock" />
</call>
</service>
<service id="httplug.factory.mock" class="Http\HttplugBundle\ClientFactory\MockFactory" public="false" />
</services>
</container>
6 changes: 0 additions & 6 deletions Tests/Unit/ClientFactory/MockFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
26 changes: 25 additions & 1 deletion Tests/Unit/DependencyInjection/HttplugExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

/**
Expand Down