You are here

abstract class MockApi in Auth0 Single Sign On 8.2

Class MockApi

@package Auth0\Tests

Hierarchy

Expanded class hierarchy of MockApi

2 files declare their use of MockApi
MockAuthenticationApi.php in vendor/auth0/auth0-php/tests/API/Authentication/MockAuthenticationApi.php
MockManagementApi.php in vendor/auth0/auth0-php/tests/API/Management/MockManagementApi.php

File

vendor/auth0/auth0-php/tests/MockApi.php, line 17

Namespace

Auth0\Tests
View source
abstract class MockApi {

  /**
   * Guzzle request history container for mock API.
   *
   * @var array
   */
  protected $requestHistory = [];

  /**
   * History index to use.
   *
   * @var integer
   */
  protected $historyIndex = 0;

  /**
   * Management API object.
   *
   * @var Management|Authentication|JWKFetcher
   */
  protected $client;

  /**
   * MockApi constructor.
   *
   * @param array $responses Array of GuzzleHttp\Psr7\Response objects.
   * @param array $config Additional optional configuration needed for mocked class.
   */
  public function __construct(array $responses = [], array $config = []) {
    $guzzleOptions = [];
    if (count($responses)) {
      $mock = new MockHandler($responses);
      $handler = HandlerStack::create($mock);
      $handler
        ->push(Middleware::history($this->requestHistory));
      $guzzleOptions['handler'] = $handler;
    }
    $this
      ->setClient($guzzleOptions, $config);
  }

  /**
   * @param array $guzzleOptions
   * @param array $config
   *
   * @return mixed
   */
  public abstract function setClient(array $guzzleOptions, array $config = []);

  /**
   * Return the endpoint being used.
   *
   * @return Management|Authentication|JWKFetcher
   */
  public function call() {
    $this->historyIndex++;
    return $this->client;
  }

  /**
   * Get the URL from a mocked request.
   *
   * @param integer $parse_component Component for parse_url, null to return complete URL.
   *
   * @return string
   */
  public function getHistoryUrl($parse_component = null) {
    $request = $this
      ->getHistory();
    $request_url = $request
      ->getUri()
      ->__toString();
    return is_null($parse_component) ? $request_url : parse_url($request_url, $parse_component);
  }

  /**
   * Get the URL query from a mocked request.
   *
   * @return string
   */
  public function getHistoryQuery() {
    return $this
      ->getHistoryUrl(PHP_URL_QUERY);
  }

  /**
   * Get the HTTP method from a mocked request.
   *
   * @return string
   */
  public function getHistoryMethod() {
    return $this
      ->getHistory()
      ->getMethod();
  }

  /**
   * Get the body from a mocked request.
   *
   * @return \stdClass|array
   */
  public function getHistoryBody() {
    $body = $this
      ->getHistory()
      ->getBody();
    return json_decode($body, true);
  }

  /**
   * Get the form body from a mocked request.
   *
   * @return string
   */
  public function getHistoryBodyAsString() {
    return $this
      ->getHistory()
      ->getBody()
      ->getContents();
  }

  /**
   * Get the headers from a mocked request.
   *
   * @return array
   */
  public function getHistoryHeaders() {
    return $this
      ->getHistory()
      ->getHeaders();
  }

  /**
   * Get a Guzzle history record from an array populated by Middleware::history().
   *
   * @return Request
   */
  protected function getHistory() {
    $requestHistoryIndex = $this->historyIndex - 1;
    return $this->requestHistory[$requestHistoryIndex]['request'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MockApi::$client protected property Management API object. 2
MockApi::$historyIndex protected property History index to use.
MockApi::$requestHistory protected property Guzzle request history container for mock API.
MockApi::call public function Return the endpoint being used.
MockApi::getHistory protected function Get a Guzzle history record from an array populated by Middleware::history().
MockApi::getHistoryBody public function Get the body from a mocked request.
MockApi::getHistoryBodyAsString public function Get the form body from a mocked request.
MockApi::getHistoryHeaders public function Get the headers from a mocked request.
MockApi::getHistoryMethod public function Get the HTTP method from a mocked request.
MockApi::getHistoryQuery public function Get the URL query from a mocked request.
MockApi::getHistoryUrl public function Get the URL from a mocked request.
MockApi::setClient abstract public function 2
MockApi::__construct public function MockApi constructor.