I'm trying to test a method that makes an HTTP request via Guzzle, so I can see if my code that filters that response is working. I have a mock json response in a json file that I want to use to test it. This is simply a saved version of an actual response.
I know you can mock methods e.g. this would override the get method to return a mock Guzzle Response. How do you mock a Guzzle response in Drupal 8?
$this->mockHttp->expects($this->any())
->method('get')
->willReturn($mockGuzzleResponseShouldGoHere);
The mock Guzzle response needs to have a mock method getBody so I can do something like this:
$this->mockHttp->expects($this->any())
->method('getBody')
->willReturn(__DIR__ . 'Mocks/membership-classes.json');
and make sure that getBody returns my mock json file.
Method to test that makes a Http Guzzle request
<?php
/**
* Call the GetMembershipClasses service.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
*/
public function getMembershipClasses() {
try {
// Load configuration for this module.
$response = $this->http_client->get(
$this->getBaseUrl() . '/2014/06/membershipClasses', [
'headers' => $this->getAnonymousHeaders(),
]);
$response = json_decode($response->getBody());
// Get to the meat of the response.
$response = $response->GetMembershipClassesResponseMessage->MembershipClasses->MembershipClass;
// Validate returned data.
$filtered = array();
foreach ($response as $membership_class) {
$membership_class->MembershipClassCode = $this->removeSpaces($membership_class->MembershipClassCode);
if ($membership_class->IsActive === TRUE) {
$filtered[] = $membership_class;
}
}
return new JsonResponse($filtered);
}
catch (RequestException $e) {
return $this->handleError($e);
}
}
And the test class
<?php
/**
* Class RouteControllerAnonymousTest.
*
*/
class AnonymousWebServicesTest extends UnitTestCase {
/**
* Mock of http_client service.
*
* @var \GuzzleHttp\Client
*/
protected $mockHttp;
/**
* Mock of state service.
*
* @var \Drupal\Core\State\State
*/
protected $mockState;
/**
* Mock of config.factory service.
*
* @var \Drupal\Core\Config\ConfigFactory
*/
protected $mockConfigFactory;
/**
* Set up method which is called before any tests are
* run. Mocks of the services that the class being tested rely on are
* created.
*
* @return void
*/
protected function setUp() {
parent::setUp();
$mockHttp = $this
->getMockBuilder('GuzzleHttp\Client')
->disableOriginalConstructor()
->setMethods(['get', 'getBody'])
->getMock();
$this->mockHttp = $mockHttp;
$mockState = $this
->getMockBuilder('Drupal\Core\State\State')
->disableOriginalConstructor()
->getMock();
$this->mockState = $mockState;
$mockConfigFactory = $this
->getMockBuilder('\Drupal\Core\Config\ConfigFactory')
->disableOriginalConstructor()
->getMock();
$this->mockConfigFactory = $mockConfigFactory;
}
/**
* Test the getMembershipClasses method.
*/
public function testGetMembershipClasses() {
// This needs to mock a Guzzle response from a get request
$this->mockHttp->expects($this->any())
->method('get')
->willReturn('The mock guzzle response should go here);
$this->mockHttp->expects($this->any())
->method('getBody')
->willReturn(__DIR__ . 'Mocks/membership-classes.json');
$anonymousWebServices = new AnonymousWebServices(
$this->mockHttp,
$this->mockState,
$this->mockConfigFactory
);
$membership_classes = $anonymousWebServices->getMembershipClasses();
var_dump($membership_classes);
// Insert some assert statement here
}
}