You are here

class TestRestClient in Salesforce Suite 5.0.x

Same name and namespace in other branches
  1. 8.4 src/Tests/TestRestClient.php \Drupal\salesforce\Tests\TestRestClient
  2. 8.3 src/Tests/TestRestClient.php \Drupal\salesforce\Tests\TestRestClient

Test Salesforce REST client.

Hierarchy

Expanded class hierarchy of TestRestClient

See also

tests/modules/salesforce_test_rest_client

2 files declare their use of TestRestClient
PullQueueTest.php in modules/salesforce_pull/tests/src/Functional/PullQueueTest.php
SalesforceTestRestClientServiceProvider.php in tests/modules/salesforce_test_rest_client/src/SalesforceTestRestClientServiceProvider.php

File

src/Tests/TestRestClient.php, line 19

Namespace

Drupal\salesforce\Tests
View source
class TestRestClient extends RestClient {
  const AUTH_ENDPOINT_URL = 'https://example.com/fake/auth/endpoint/for/testing';
  const AUTH_TOKEN_URL = 'https://example.com/fake/token/url/for/testing';

  /**
   * {@inheritdoc}
   */
  public function isInit() {
    return TRUE;
  }

  /**
   * Wrapper for test results.
   */
  public static function getContactQueryResults() {
    return json_decode(file_get_contents(__DIR__ . '/contactQuery.json'), JSON_OBJECT_AS_ARRAY);
  }

  /**
   * Mock a query result for test_mapping.
   *
   * @see modules/salesforce_mapping/tests/modules/salesforce_mapping_test/config/install/salesforce_mapping.salesforce_mapping.test_mapping.yml
   */
  public function query(SelectQueryInterface $query) {
    return new SelectQueryResult(self::getContactQueryResults());
  }

  /**
   * Short-circuit api calls.
   */
  public function apiCall($path, array $params = [], $method = 'GET', $returnObject = FALSE) {
  }

  /**
   * Hard-code a short list of objects for testing.
   */
  public function objects(array $conditions = [
    'updateable' => TRUE,
  ], $reset = FALSE) {
    return json_decode(file_get_contents(__DIR__ . '/objects.json'), JSON_OBJECT_AS_ARRAY);
  }

  /**
   * Hard-code the object descriptions for testing.
   */
  public function objectDescribe($name, $reset = FALSE) {
    $contents = file_get_contents(__DIR__ . '/objectDescribe.json');
    return new RestResponseDescribe(new RestResponse(new Response(200, [
      'Content-Type' => 'application/json;charset=UTF-8',
    ], $contents)));
  }

  /**
   * Hard-code record types for testing.
   */
  public function getRecordTypes($name = NULL, $reset = FALSE) {
    $json = json_decode(file_get_contents(__DIR__ . '/recordTypes.json'), JSON_OBJECT_AS_ARRAY);
    $result = new SelectQueryResult($json);
    $record_types = [];
    foreach ($result
      ->records() as $rt) {
      $record_types[$rt
        ->field('SobjectType')][$rt
        ->field('DeveloperName')] = $rt;
    }
    if ($name != NULL) {
      if (!isset($record_types[$name])) {
        throw new \Exception("No record types for {$name}");
      }
      return $record_types[$name];
    }
    return $record_types;
  }

  /**
   * Helper callback for OAuth handshake, and refreshToken()
   *
   * @param \GuzzleHttp\Psr7\Response $response
   *   Response object from refreshToken or authToken endpoints.
   *
   * @see SalesforceController::oauthCallback()
   * @see self::refreshToken()
   */
  public function handleAuthResponse(Response $response) {
  }

  /**
   * Get the fake OAuth endpoint.
   *
   * @return string
   *   REST OAuth Login URL.
   */
  public function getAuthEndpointUrl() {
    return self::AUTH_ENDPOINT_URL;
  }

  /**
   * Get the fake Auth token endpoint.
   *
   * @return string
   *   REST OAuth Token URL.
   */
  public function getAuthTokenUrl() {
    return self::AUTH_TOKEN_URL;
  }

  /**
   * Prevent an API call out here.
   */
  public function listResources() {
    return (object) [
      'resources' => [],
    ];
  }

  /**
   * {@inheritDoc}
   */
  public function objectCreate($name, array $params) {
    $random = new Random();
    return new SFID(strtoupper($random
      ->string(SFID::MAX_LENGTH)));
  }

  /**
   * {@inheritDoc}
   */
  public function objectUpsert($name, $key, $value, array $params) {
    $random = new Random();
    return new SFID(strtoupper($random
      ->string(SFID::MAX_LENGTH)));
  }

  /**
   * {@inheritDoc}
   */
  public function objectUpdate($name, $id, array $params) {

    // Object update does... NOTHING!
    return NULL;
  }

  /**
   * {@inheritDoc}
   */
  public function objectDelete($name, $id, $throw_exception = FALSE) {

    // Object delete does... NOTHING!
    return NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RestClient::$authConfig protected property Active auth provider config.
RestClient::$authManager protected property Auth provider manager.
RestClient::$authProvider protected property Active auth provider.
RestClient::$authToken protected property Active auth token.
RestClient::$cache protected property The cache service.
RestClient::$configFactory protected property Config factory service.
RestClient::$httpClient protected property GuzzleHttp client.
RestClient::$httpClientOptions protected property HTTP client options.
RestClient::$immutableConfig protected property Salesforce immutable config object. Useful for gets.
RestClient::$json protected property The JSON serializer service.
RestClient::$response public property Response object.
RestClient::$state protected property The state service.
RestClient::$url protected property Salesforce API URL.
RestClient::apiHttpRequest protected function Private helper to issue an SF API request.
RestClient::CACHE_LIFETIME constant
RestClient::getApiUsage public function Get the api usage, as returned in the most recent API request header. Overrides RestClientInterface::getApiUsage
RestClient::getDeleted public function Retrieves objects deleted within the given timeframe. Overrides RestClientInterface::getDeleted
RestClient::getErrorData protected function Extract normalized error information from a RequestException.
RestClient::getHttpClientOption public function Getter for a single, named HTTP client option. Overrides RestClientInterface::getHttpClientOption
RestClient::getHttpClientOptions public function Getter for HTTP client options. Overrides RestClientInterface::getHttpClientOptions
RestClient::getLongTermCacheLifetime public function Getter for long term cache lifetime. Overrides RestClientInterface::getLongTermCacheLifetime
RestClient::getObjectTypeName public function Utility function to determine object type for given SFID. Overrides RestClientInterface::getObjectTypeName
RestClient::getRecordTypeIdByDeveloperName public function Given a DeveloperName and SObject Name, return SFID of the RecordType. Overrides RestClientInterface::getRecordTypeIdByDeveloperName
RestClient::getRequestTime protected function Returns REQUEST_TIME.
RestClient::getShortTermCacheLifetime public function Getter for short term cache lifetime. Overrides RestClientInterface::getShortTermCacheLifetime
RestClient::getUpdated public function Return a list of SFIDs for the given object for the given timeframe. Overrides RestClientInterface::getUpdated
RestClient::getVersions public function Wrapper for "Versions" resource to list information about API releases. Overrides RestClientInterface::getVersions
RestClient::httpRequest protected function Make the HTTP request. Wrapper around drupal_http_request().
RestClient::httpRequestRaw public function Return raw response content from given URL. Overrides RestClientInterface::httpRequestRaw
RestClient::LONGTERM_CACHE_LIFETIME constant
RestClient::objectRead public function Return a fullly loaded Salesforce object. Overrides RestClientInterface::objectRead
RestClient::objectReadbyExternalId public function Return a full loaded Salesforce object from External ID. Overrides RestClientInterface::objectReadbyExternalId
RestClient::queryAll public function Same as ::query(), but also returns deleted or archived records. Overrides RestClientInterface::queryAll
RestClient::queryMore public function Given a select query result, fetch the next results set, if it exists. Overrides RestClientInterface::queryMore
RestClient::setHttpClientOption public function Set a single Guzzle HTTP client option. Overrides RestClientInterface::setHttpClientOption
RestClient::setHttpClientOptions public function Set options for Guzzle HTTP client. Overrides RestClientInterface::setHttpClientOptions
RestClient::updateApiUsage protected function Helper to extract API Usage info from response header and write to state.
RestClient::__construct public function Constructor which initializes the consumer.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TestRestClient::apiCall public function Short-circuit api calls. Overrides RestClient::apiCall
TestRestClient::AUTH_ENDPOINT_URL constant
TestRestClient::AUTH_TOKEN_URL constant
TestRestClient::getAuthEndpointUrl public function Get the fake OAuth endpoint.
TestRestClient::getAuthTokenUrl public function Get the fake Auth token endpoint.
TestRestClient::getContactQueryResults public static function Wrapper for test results.
TestRestClient::getRecordTypes public function Hard-code record types for testing. Overrides RestClient::getRecordTypes
TestRestClient::handleAuthResponse public function Helper callback for OAuth handshake, and refreshToken()
TestRestClient::isInit public function Check if the client is ready to perform API calls. Overrides RestClient::isInit
TestRestClient::listResources public function Prevent an API call out here. Overrides RestClient::listResources
TestRestClient::objectCreate public function Create a new object of the given type. Overrides RestClient::objectCreate
TestRestClient::objectDelete public function Delete a Salesforce object. Overrides RestClient::objectDelete
TestRestClient::objectDescribe public function Hard-code the object descriptions for testing. Overrides RestClient::objectDescribe
TestRestClient::objects public function Hard-code a short list of objects for testing. Overrides RestClient::objects
TestRestClient::objectUpdate public function Update an existing object. Overrides RestClient::objectUpdate
TestRestClient::objectUpsert public function Create new records or update existing records. Overrides RestClient::objectUpsert
TestRestClient::query public function Mock a query result for test_mapping. Overrides RestClient::query