You are here

class MockHandlerStack in Apigee Edge 8

The mock handler stack.

The Mock handler stack overrides the Mock handler so we can add a response factory as well.

Hierarchy

  • class \Drupal\apigee_mock_api_client\MockHandlerStack extends \Apigee\MockClient\GuzzleHttp\MockHandler

Expanded class hierarchy of MockHandlerStack

1 string reference to 'MockHandlerStack'
apigee_mock_api_client.services.yml in tests/modules/apigee_mock_api_client/apigee_mock_api_client.services.yml
tests/modules/apigee_mock_api_client/apigee_mock_api_client.services.yml
1 service uses MockHandlerStack
apigee_mock_api_client.mock_http_handler_stack in tests/modules/apigee_mock_api_client/apigee_mock_api_client.services.yml
Drupal\apigee_mock_api_client\MockHandlerStack

File

tests/modules/apigee_mock_api_client/src/MockHandlerStack.php, line 36

Namespace

Drupal\apigee_mock_api_client
View source
class MockHandlerStack extends MockHandler {

  /**
   * Responses that have been loaded from the response file.
   *
   * @var array
   */
  protected $responses = [];

  /**
   * The response factory.
   *
   * @var \Apigee\MockClient\ResponseFactoryInterface
   */
  protected $responseFactory;

  /**
   * The twig environment used in the response generator.
   *
   * @var \Twig_Environment
   */
  protected $twig;

  /**
   * Override the mock handler constructor.
   *
   * @param \Apigee\MockClient\MockStorageInterface $storage
   *   Mock storage.
   * @param \Apigee\MockClient\ResponseFactoryInterface $response_factory
   *   The response factory.
   * @param \Twig_Environment $twig
   *   The twig environment used in the response generator.
   */
  public function __construct(MockStorageInterface $storage, ResponseFactoryInterface $response_factory, \Twig_Environment $twig) {
    parent::__construct($storage);
    $this->responseFactory = $response_factory;
    $this->twig = $twig;
  }

  /**
   * Queue a response that is in the catalog.
   *
   * Dynamic values can be passed and
   * will be replaced in the response.
   *
   * @param string|array $response_ids
   *   The name of the response template to queue (without file extension)
   *   e.g. `get-developer` or `get_developer` @see /tests/response-templates.
   *
   * @return $this
   */
  public function queueMockResponse($response_ids) {
    $org_name = \Drupal::service('apigee_edge.sdk_connector')
      ->getOrganization();
    if (empty($this->responses)) {

      // Get the module path for this module.
      $module_path = \Drupal::moduleHandler()
        ->getModule('apigee_mock_api_client')
        ->getPath();
      $this->responses = Yaml::parseFile($module_path . '/response_catalog.yml')['responses'];
    }

    // Loop through responses and add each one.
    foreach ((array) $response_ids as $index => $item) {

      // The catalog id should either be the item itself or the keys if an
      // associative array has been passed.
      $id = !is_array($item) ? $item : $index;

      // Body text can have elements replaced in it for certain values.
      $context = is_array($item) ? $item : [];
      $context['org_name'] = isset($context['org_name']) ? $context['org_name'] : $org_name;

      // Add the default headers if headers aren't defined in the response
      // catalog.
      $headers = isset($this->responses[$id]['headers']) ? $this->responses[$id]['headers'] : [
        'content-type' => 'application/json;charset=utf-8',
      ];

      // Set the default status code.
      $status_code = !empty($this->responses[$id]['status_code']) ? $this->responses[$id]['status_code'] : 200;
      $status_code = !empty($context['status_code']) ? $context['status_code'] : $status_code;
      if ($this->twig
        ->getLoader()
        ->exists($id)) {
        $this
          ->addResponse($this->responseFactory
          ->generateResponse(new TwigSource($id, $context, $status_code, $headers)));
      }
      else {
        $this
          ->addResponse(new Response($status_code, $headers, ''));
      }
    }
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MockHandlerStack::$responseFactory protected property The response factory.
MockHandlerStack::$responses protected property Responses that have been loaded from the response file.
MockHandlerStack::$twig protected property The twig environment used in the response generator.
MockHandlerStack::queueMockResponse public function Queue a response that is in the catalog.
MockHandlerStack::__construct public function Override the mock handler constructor.