Experimental project

This is a sandbox project, which contains experimental code for developer use only.

When writing tests for the Drupal functionality, you might not want to have the Drupal site make actual calls to the remote api. Perhaps there isn't a testing version or perhaps its not something you have any control over so cannot remove any test data if it were created there.

This module provides a mock endpoint using the Drupal framework which can be communicated with during tests instead of the remote API. Functions exist which allow you to tell the mock API endpoint to expect certain requests and how to respond to them. A behat context is provided for use with the drupalextension testing framework. Your behaviour scenarios can define expected calls to an API and set the expected response.

During use, the module stores data into a text file. such as Expected
calls to the webservice. By default this file is _temporary://mock-api.txt_
but this can be set specifically in your settings.php file:

$conf['mock_api_expectation_file'] = '/tmp/mock-api.txt';

The module provides a single URL which should replace the usual base url
you use to access your real API at your Drupal site URL /mock-api-endpoint.
For example, if your Drupal site under test is http://mysite.local then
the mock API endpoint would be:

http://mysite.local/mock-api-endpoint

You would replace the URL to the real API with this on the test site.

You can then set expected calls to the api and specify the response
to that call.

For full details, check the README

Example of use within a FeatureContext

class FeatureContext extends DrupalContext implements SnippetAcceptingContext {
    
      /** @var MockAPIContext */
      private $mockApiContext;
      
      /** @BeforeScenario */
      public function gatherContexts(BeforeScenarioScope $scope) {
        $environment = $scope->getEnvironment();
        $this->mockApiContext = $environment->getContext('MockAPIContext');
      }

      /**
       * @When /^I login as a CRM contact with name "([^"]*)"$/
       */
      public function loginAsCRMContact($name) {
         // Register an expectation that a call to CRM will happen.
         $this->expectRequestUser('123', $name, $name . '@example.com');
         
         /** Some login code which triggers request for contact in CRM. */
         
         // Now check the call to CRM did actually happen.
         $this->mockApiContext->allExpectedRequestsWereReceivedByTheAPI();
      }
      
      /**
        * Register an expectation that Drupal will ask for a specific Contact from CRM.
        */
      public function expectRequestUser($id, $name, $email) {

        // Expect the following request.
        $request = (object) array(
          'args' => array('api', 'contact', $id),
          'method' => 'GET',
        );

        // Construct the correct response.
        $contact = (object) array(
          'id' => $id,
          'name' => $name,
          'email' => $email,
        );

        $result = new stdClass();
        $result->contacts = array($contact);

        // Respond with the following response.
        $response = (object) array(
          'headers' => array(
            'Content-Type' => 'application/json',
          ),
          'status' => 200,
          'body' => json_encode($result),
        );

        // Register this expectation.
        mock_api_set_test_api_response($request, $response);
      }
}

Project information

  • caution Maintenance fixes only
    Considered feature-complete by its maintainers.
  • Module categories: Developer Tools
  • Created by johnennew on , updated