1

Environment: Symfony2, Guzzle

Problem: I want to write functional tests and replace the guzzle client used in the tested class with a mock client class sometimes.

In detail:

  • class BookFetcher uses class ApiConnector which uses Guzzle to retrieve the data from somewhere else
  • I want to write some tests which actually use the original ApiConnector to make sure that nothing has been changed at the other side
  • as this is slow I want to mock the ApiConnector in all other tests using a mock class
  • At the moment I replace the ApiConnector class with a MockClass in config_test.yml (parameters section). This affects ALL tests (no more contract testing).
  • I need a way to replace the ApiConnector class with my mock class sometimes
herrjeh42
  • 2,782
  • 4
  • 35
  • 47
  • I always create a service that collaborate with Guzzle. In functional environment, ... I just mock collaborator. In this case, Guzzle. – sensorario Nov 25 '15 at 11:38

1 Answers1

0

You can create an instance of ApiConnector using new keyword, inject mock Guzzle and use it for testing.

Another option you have is you can mock ApiConnector while mocking only some of it methods (maybe the ones doing the actual connections?) using phpUnit's setMethods.

Community
  • 1
  • 1
Konstantin Pereiaslov
  • 1,786
  • 1
  • 18
  • 26