Documentation

LocalHttpClient
in package
implements ClientInterface

(Experimental) Send HTTP-style requests directly to CRM_Core_Invoke (PSR-18 ClientInterface).

This allows you to process many requests within the same PHP process, which can be useful for headless unit-testing.

$c = new LocalHttpClient(['reboot' => FALSE]); $response = $c->sendRequest(new Request('GET', '/civicrm/foo?reset=1&bar=100')); $response = $c->sendRequest(new Request('GET', '/civicrm/whiz?reset=1&bang=200'));

In theory, this could be the basis for headless HTTP testing with client-libraries like Guzzle, Mink, or BrowserKit.

WHY: CiviCRM predates the PSR HTTP OOP conventions -- many things are built with $_GET, $_REQUEST, etc. To simulate an HTTP request to these, we swap-in and swap-out values for $_GET, $_REQUEST, etc. Consequently, there is some limited isolation between the parent/requester and child/requestee.

NOTE: You can improve the isolation more with reboot=>TRUE. This will swap (and reinitialize) the CiviCRM runtime-config and service-container. However, there is no comprehensive option to swap all static properties (other classes), so some data may still leak between requester+requestee.

NOTE: This is primarily intended for use in headless testing (CIVICRM_UF=UnitTests). It may or may not be quirky with real UFs.

Tags
link
https://www.php-fig.org/psr/psr-18/

Table of Contents

Interfaces

ClientInterface

Properties

$htmlHeader  : bool
Whether to generate the HTML <HEAD>er
$inherit  : array<string|int, mixed>
List of scopes which should be inherited/extended within the subrequest.
$scopes  : array<string|int, mixed>
List of scopes which should be backed-up, (re)populated, (re)set for the duration of the subrequest.

Methods

__construct()  : mixed
getAllValues()  : array<string|int, mixed>
sendRequest()  : ResponseInterface
initScopes()  : mixed
initValues_GET()  : array<string|int, mixed>
Map data from the request to $_GET.
initValues_POST()  : array<string|int, mixed>
Map data from the request to $_POST.
initValues_REQUEST()  : array<string|int, mixed>
Map data from the request to $_REQUEST.
initValues_SERVER()  : array<string|int, mixed>
invoke()  : string|null
restoreAllValues()  : void

Properties

$htmlHeader

Whether to generate the HTML <HEAD>er

protected bool $htmlHeader

$inherit

List of scopes which should be inherited/extended within the subrequest.

protected array<string|int, mixed> $inherit

Ex: ['_COOKIE', '_SERVER']

$scopes

List of scopes which should be backed-up, (re)populated, (re)set for the duration of the subrequest.

protected array<string|int, mixed> $scopes

Ex: ['_GET' => new SuperGlobal('_GET')]

Methods

__construct()

public __construct([array<string|int, mixed> $options = [] ]) : mixed
Parameters
$options : array<string|int, mixed> = []
  • reboot (bool): TRUE if you want to re-bootstrap CiviCRM (config/container) on each request Default: FALSE
  • htmlHeader (bool): TRUE if you want the generated page to include the full HTML header This may become standard (non-optional). It's opt-out to help debug/work-around some early quirks when first using LocalHttpClient in CI.
  • globals (string[]): List of (super)globals that should be backed-up, populated, used, and restored. Default: ['_GET', '_POST', '_COOKIE', '_FILES', '_SERVER', '_REQUEST']
  • inherit (string[]): When populating these (super)globals, build on top of the existing values. Default: ['_COOKIE', '_SERVER']

getAllValues()

public getAllValues() : array<string|int, mixed>
Return values
array<string|int, mixed>

sendRequest()

public sendRequest(RequestInterface $request) : ResponseInterface
Parameters
$request : RequestInterface
Return values
ResponseInterface

initScopes()

protected initScopes(RequestInterface $request) : mixed
Parameters
$request : RequestInterface

initValues_GET()

Map data from the request to $_GET.

protected initValues_GET(RequestInterface $request) : array<string|int, mixed>
Parameters
$request : RequestInterface
Return values
array<string|int, mixed>

initValues_POST()

Map data from the request to $_POST.

protected initValues_POST(RequestInterface $request) : array<string|int, mixed>
Parameters
$request : RequestInterface
Return values
array<string|int, mixed>

initValues_REQUEST()

Map data from the request to $_REQUEST.

protected initValues_REQUEST(RequestInterface $request) : array<string|int, mixed>
Parameters
$request : RequestInterface
Return values
array<string|int, mixed>

initValues_SERVER()

protected initValues_SERVER(RequestInterface $request) : array<string|int, mixed>
Parameters
$request : RequestInterface
Return values
array<string|int, mixed>

invoke()

protected invoke(string $route) : string|null
Parameters
$route : string
Return values
string|null

restoreAllValues()

protected restoreAllValues(array<string|int, mixed> &$backup) : void
Parameters
$backup : array<string|int, mixed>

        
On this page

Search results