Skip to content

Commit 7564416

Browse files
GaryPEGEOTdbu
authored andcommitted
Add mock client factory for test purpose. (#242)
1 parent f034b16 commit 7564416

File tree

6 files changed

+95
-0
lines changed

6 files changed

+95
-0
lines changed

ClientFactory/MockFactory.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Http\HttplugBundle\ClientFactory;
4+
5+
use Http\Mock\Client;
6+
7+
/**
8+
* @author Gary PEGEOT <garypegeot@gmail.com>
9+
*/
10+
class MockFactory implements ClientFactory
11+
{
12+
/**
13+
* @var Client
14+
*/
15+
private $client;
16+
17+
/**
18+
* @param Client $client
19+
*/
20+
public function setClient(Client $client)
21+
{
22+
$this->client = $client;
23+
}
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function createClient(array $config = [])
29+
{
30+
if (!class_exists(Client::class)) {
31+
throw new \LogicException('To use the mock adapter you need to install the "php-http/mock-client" package.');
32+
}
33+
34+
if (!$this->client) {
35+
$this->client = new Client();
36+
}
37+
38+
return $this->client;
39+
}
40+
}

DependencyInjection/HttplugExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Http\Message\Authentication\BasicAuth;
1313
use Http\Message\Authentication\Bearer;
1414
use Http\Message\Authentication\Wsse;
15+
use Http\Mock\Client as MockClient;
1516
use Psr\Http\Message\UriInterface;
1617
use Symfony\Component\Config\FileLocator;
1718
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -40,6 +41,9 @@ public function load(array $configs, ContainerBuilder $container)
4041

4142
$loader->load('services.xml');
4243
$loader->load('plugins.xml');
44+
if (\class_exists(MockClient::class)) {
45+
$loader->load('mock-client.xml');
46+
}
4347

4448
// Register default services
4549
foreach ($config['classes'] as $service => $class) {

Resources/config/mock-client.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
5+
6+
<services>
7+
<service id="httplug.client.mock" class="Http\Mock\Client" public="true" />
8+
<service id="httplug.factory.mock" class="Http\HttplugBundle\ClientFactory\MockFactory" public="false">
9+
<call method="setClient">
10+
<argument type="service" id="httplug.client.mock" />
11+
</call>
12+
</service>
13+
</services>
14+
</container>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* This file is part of the HttplugBundle package.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace Http\HttplugBundle\Tests\Unit\ClientFactory;
10+
11+
use Http\HttplugBundle\ClientFactory\MockFactory;
12+
use Http\Mock\Client;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* @author Gary PEGEOT <garypegeot@gmail.com>
17+
*/
18+
class MockFactoryTest extends TestCase
19+
{
20+
public function testCreateClient()
21+
{
22+
$factory = new MockFactory();
23+
$client = $factory->createClient();
24+
25+
$this->assertInstanceOf(Client::class, $client);
26+
27+
$client = new Client();
28+
29+
$factory->setClient($client);
30+
31+
$this->assertEquals($client, $factory->createClient());
32+
}
33+
}

Tests/Unit/DependencyInjection/HttplugExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public function testClientPlugins()
140140
$this->assertContainerBuilderHasService($id);
141141
}
142142
$this->assertContainerBuilderHasServiceDefinitionWithArgument('httplug.client.acme', 1, $pluginReferences);
143+
$this->assertContainerBuilderHasService('httplug.client.mock');
143144
}
144145

145146
/**

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
"conflict": {
6161
"php-http/guzzle6-adapter": "<1.1"
6262
},
63+
"suggest": {
64+
"php-http/mock-client": "Add this to your require-dev section to mock HTTP responses easily"
65+
},
6366
"autoload": {
6467
"psr-4": {
6568
"Http\\HttplugBundle\\": ""

0 commit comments

Comments
 (0)