-
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
Conversation
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.
great idea, thanks!
you could add the mock client to the composer.json suggest section and propose to add it in require-dev of the users project.
ClientFactory/MockFactory.php
Outdated
*/ | ||
public function createClient(array $config = []) | ||
{ | ||
if (!class_exists('Http\Mock\Client')) { |
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.
@@ -106,6 +106,16 @@ private function configureClients(ContainerBuilder $container, array $config) | |||
$container->setAlias('httplug.client.default', 'httplug.client.'.$first); | |||
} | |||
} | |||
|
|||
$mockClass = 'Http\Mock\Client'; |
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.
lets use the ::class pseudoconstant here. and make the use statement say use Http\Mock\Client as MockClient;
to make things more readable.
ClientFactory/MockFactory.php
Outdated
throw new \LogicException('To use the mock adapter you need to install the "php-http/mock-client" package.'); | ||
} | ||
|
||
return $this->client ?: new Client(); |
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.
this will create a new client on each call, if none has been registered. should we instead register the newly created client if none was set?
Resources/config/services.xml
Outdated
@@ -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" /> |
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.
i think this service should be removed from the container when the mock client is not available. that way, users notice problems at container build time instead of at runtime.
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.
thanks, looks good to me!
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.
Great! I wrote a comment to improve the readability.
$mockServiceId = 'httplug.client.mock'; | ||
|
||
if (!\class_exists(MockClient::class)) { | ||
$container->removeDefinition('httplug.factory.mock'); |
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.
Instead of removing a definition when the class does not exist, could you move the relevant service definition to a dedicated file and load this file if the class is available?
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.
looks good to me, thanks!
What's in this PR?
Add a mock client factory to ease functional testing.
Example Usage
Configuration:
Usage: