-
Notifications
You must be signed in to change notification settings - Fork 50
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
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.
if i remember correctly, the intention of this was that you could prepare a mock and set it in the factory, or get the mock from the factory and set things on it. that way you can manage what the mock should be doing even if the code you are testing is using the factory.
i can see what you want to do and with properly built code that uses DI, it would work fine. however, when you have such code, why use the factory at all? then you can define as many clients as you want as mock clients whithout using the factory at all.
i would rather not change this, as the side effects can be significant.
I just tried this: # app/config/httplug.yaml
httplug:
clients:
my_service:
factory: 'httplug.factory.guzzle5'
config:
defaults:
connect_timeout: 3
timeout: 3 # app/config/httplug_test.yaml
services:
httplug.clients.my_service.mock:
class: Http\Mock\Client
httplug:
clients:
my_service:
service: 'httplug.clients.my_service.mock' Now it gives this error:
I think this is because it first loads services:
httplug.clients.my_service.mock:
class: Http\Mock\Client
httplug:
clients:
my_service:
factory: 'httplug.factory.guzzle5'
service: 'httplug.clients.my_service.mock'
config:
defaults:
connect_timeout: 3
timeout: 3 So now the only way for me to solve this, is by removing the factory stuff from |
Doing it like this seems to work, but still very ugly: services:
httplug.clients.my_service.mock:
class: Http\Mock\Client
httplug.clients.my_service.mock_factory:
class: Http\HttplugBundle\ClientFactory\MockFactory
calls:
- ['setClient', ['@httplug.clients.my_service.mock']]
httplug.clients.my_service_read.mock:
class: Http\Mock\Client
httplug.clients.my_service_read.mock_factory:
class: Http\HttplugBundle\ClientFactory\MockFactory
calls:
- ['setClient', ['@httplug.clients.my_service_read.mock']]
httplug:
clients:
my_service:
factory: 'httplug.clients.my_service.mock_factory'
public: true
my_service_read:
factory: 'httplug.clients.my_service_read.mock_factory'
public: true |
can you configure the "real" services in httplug_prod.yaml only, and the mock ones in httplug_test.yaml? |
You mean: // httplug_prod.yaml
http:
clients:
my_service:
factory: 'guzzleblala'
// options here // httplug_dev.yaml
http:
clients:
my_service:
factory: 'guzzleblala'
// options here // httplug_test.yaml
http:
clients:
my_service:
factory: 'mock'
// options here ? |
hm, yeah i guess. maybe you can make httplug_shared.yaml to include that from httplug_dev and _prod to not duplicate there? |
Feels really complicated. Maybe I can make a PR to introduce a |
yeah, its not elegant. i think its better to have a separate class and service rather than make the existing "configurable" for both scenarios. i think the pull request makes sense, please do one. |
I'm trying hard to come up with a name for this Mock Factory. But the only thing that is right, is |
hm, what about adding a method to the existing factory , called |
What's in this PR?
This PR makes the MockFactory a real factory.
Why?
When you have multiple configured clients, and want to mock them in test mode, you can change the factory method to
httplug.factory.mock
. That's awesome. But they all use the same instance ofHttp\Mock\Client
which is problematic.If you have 1 service that calls 2 http clients after each othere, there is no way of properly configuring things.
This PR makes sure every client gets its own mock instance that you can access by calling
httplug.client.acme.client
(see #287)Checklist