-
Notifications
You must be signed in to change notification settings - Fork 50
Add mock client factory for test purpose. #242
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
Changes from 2 commits
2641a95
3144ea8
13cbc2e
140e96a
dd1cb9a
c2f3ac6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| namespace Http\HttplugBundle\ClientFactory; | ||
|
|
||
| use Http\Mock\Client; | ||
|
|
||
| /** | ||
| * @author Gary PEGEOT <[email protected]> | ||
| */ | ||
| 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(); | ||
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)]); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,5 +68,6 @@ | |
| <service id="httplug.factory.socket" class="Http\HttplugBundle\ClientFactory\SocketFactory" public="false"> | ||
| <argument type="service" id="httplug.message_factory"/> | ||
| </service> | ||
| <service id="httplug.factory.mock" class="Http\HttplugBundle\ClientFactory\MockFactory" public="false" /> | ||
|
||
| </services> | ||
| </container> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
| /** | ||
| * This file is part of the HttplugBundle package. | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| namespace Http\HttplugBundle\Tests\Unit\ClientFactory; | ||
|
|
||
| use Http\HttplugBundle\ClientFactory\MockFactory; | ||
| use Http\Mock\Client; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * @author Gary PEGEOT <[email protected]> | ||
| */ | ||
| 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()); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use the ::class pseudoconstant. we are on php 5.5 or newer.