Documentation

RemoteTestFunction
in package
uses HttpTestTrait, MagicGetterSetterTrait

RemoteTestFunctions may be used by tests to define (inline) code that will run on a remote server.

Tags
code

// Register a function. Send a request and parse the response as PHP data. // Responses MUST be JSON-serializable data. $getBaseUrl = RemoteTestFunction::register('getBaseUrl', fn() => CIVICRM_UF_BASEURL); $data = $getBaseUrl->execute();

code

// Register a function. Send HTTP call and inspect response. $getBaseUrl = RemoteTestFunction::register('getBaseUrl', fn() => CIVICRM_UF_BASEURL); $response = $getBaseUrl->httpRequest(); assertEquals(200, $response->getStatusCode());

endCode
endCode

TODO: Continue kicking-around method signatures. How best to distinguish functions which are meant to return JSON data and functions which are meant as mini page-controllers?

Table of Contents

Properties

$httpHistory  : array<string|int, mixed>
List of HTTP requests that have been made by this test.
$requestChannel  : string
$requestMethod  : string
How to submit the request. One of: "POST", "GET", "LOCAL"
$responseDecoder  : callable|null
$responseType  : string
$class  : string
Name of the test-class which defined the RTF.
$codeFile  : string
Path to a file with the minimal/extracted version of the RTF.
$id  : string
$indexFile  : string
Path to a file with local metadata about the RTF.
$name  : string
Logical name of the remote executable.

Methods

__call()  : static|mixed
Magic function to provide getters/setters.
_run()  : mixed
byId()  : RemoteTestFunction|null
byName()  : RemoteTestFunction|null
Lookup the implementation of a remote-test-function.
execute()  : mixed
Execute the method.
getRequestChannel()  : string
getRequestMethod()  : string
getResponseDecoder()  : callable|null
getResponseType()  : string
httpRequest()  : RequestInterface
Generate an HTTP request template.
register()  : RemoteTestFunction
Declare a remote-test-function.
setRequestChannel()  : $this
setRequestMethod()  : $this
setResponseDecoder()  : $this
setResponseType()  : $this
assertBodyRegexp()  : mixed
Assert that the response body matches a regular-expression.
assertContentType()  : $this
assertNotBodyRegexp()  : mixed
Assert that the response body DOES NOT match a regular-expression.
assertPageNotShown()  : void
Assert that the response did NOT produce a normal page-view.
assertStatusCode()  : $this
callApi4AjaxError()  : mixed
callApi4AjaxSuccess()  : mixed
createDecoder()  : callable
createGuzzle()  : Client
Create an HTTP client suitable for simulating AJAX requests.
formatFailure()  : false|string
Given that an HTTP request has yielded a failed response, format a blurb to summarize the details of the request+response.
getMagicProperties()  : array<string|int, mixed>
Get a list of class properties for which magic methods are supported.
resolveResponse()  : ResponseInterface
__construct()  : mixed
assertTestEnvironment()  : void
createClient()  : Client|ClientInterface
getCodePath()  : string
getIndexPath()  : string
The folder which stores information about test functions.
render()  : string
save()  : void
writeFile()  : void

Properties

$httpHistory

List of HTTP requests that have been made by this test.

protected array<string|int, mixed> $httpHistory = []

$requestChannel

protected string $requestChannel = 'HTTP'

Either 'HTTP' or 'LOCAL'

$requestMethod

How to submit the request. One of: "POST", "GET", "LOCAL"

protected string $requestMethod = 'POST'

$codeFile

Path to a file with the minimal/extracted version of the RTF.

private string $codeFile

$indexFile

Path to a file with local metadata about the RTF.

private string $indexFile

$name

Logical name of the remote executable.

private string $name

This is often eponymous with the test-function but it may vary.

Methods

__call()

Magic function to provide getters/setters.

public __call(string $method, array<string|int, mixed> $arguments) : static|mixed
Parameters
$method : string
$arguments : array<string|int, mixed>
Tags
throws
CRM_Core_Exception
Return values
static|mixed

execute()

Execute the method.

public execute([array<string|int, mixed> $args = [] ]) : mixed

This is intended for use with RTF's that simply return JSON data. If your RTF intends to emit some other response, then use httpRequest() and Guzzle Client.

Parameters
$args : array<string|int, mixed> = []

Data to pass through to the test function.

getResponseDecoder()

public getResponseDecoder() : callable|null
Return values
callable|null

httpRequest()

Generate an HTTP request template.

public httpRequest([array<string|int, mixed> $args = [] ][, string $method = 'POST' ]) : RequestInterface

If you send this HTTP request, it will run the remote test-function.

Parameters
$args : array<string|int, mixed> = []

Data to pass through to the test function.

$method : string = 'POST'
Return values
RequestInterface

register()

Declare a remote-test-function.

public static register(string $class, string $name, Closure $closure) : RemoteTestFunction

This is used by a test-class to prepare the server to execute some code.

Parameters
$class : string

Who is creating this function.

$name : string

Name of the function. (Each test-function should be unique within its test-class.)

$closure : Closure

Logic to execute. Should be a Closure. Must not have any use() properties. The result should be a JSON-friendly array-tree. Alternatively, it may emit a custom HTTP response via \CRM_Utils_System::sendResponse($result);

Return values
RemoteTestFunction

setRequestChannel()

public setRequestChannel([string $channel = ]) : $this
Parameters
$channel : string =
Return values
$this

setRequestMethod()

public setRequestMethod([string $method = ]) : $this
Parameters
$method : string =
Return values
$this

setResponseDecoder()

public setResponseDecoder([callable $decoder = ]) : $this
Parameters
$decoder : callable =
Return values
$this

setResponseType()

public setResponseType([string $type = ]) : $this
Parameters
$type : string =
Return values
$this

assertBodyRegexp()

Assert that the response body matches a regular-expression.

protected assertBodyRegexp(string $regexp[, ResponseInterface $response = NULL ][, string $message = NULL ]) : mixed
Parameters
$regexp : string
$response : ResponseInterface = NULL
$message : string = NULL

assertContentType()

protected assertContentType(mixed $expectType[, ResponseInterface|null $response = NULL ]) : $this
Parameters
$expectType : mixed
$response : ResponseInterface|null = NULL

If NULL, then it uses the last response.

Return values
$this

assertNotBodyRegexp()

Assert that the response body DOES NOT match a regular-expression.

protected assertNotBodyRegexp(string $regexp[, ResponseInterface $response = NULL ][, string $message = NULL ]) : mixed
Parameters
$regexp : string
$response : ResponseInterface = NULL
$message : string = NULL

assertPageNotShown()

Assert that the response did NOT produce a normal page-view.

protected assertPageNotShown([mixed $response = NULL ]) : void

This is basically assertStatusCode(404), except that the local configuration (CMS/setings/exts/yaddayadda) may change how the error manifests.

Parameters
$response : mixed = NULL

assertStatusCode()

protected assertStatusCode(mixed $expectCode[, ResponseInterface|null $response = NULL ]) : $this
Parameters
$expectCode : mixed
$response : ResponseInterface|null = NULL

If NULL, then it uses the last response.

Return values
$this

callApi4AjaxError()

protected callApi4AjaxError(string $entity, string $action[, array<string|int, mixed> $params = [] ]) : mixed
Parameters
$entity : string
$action : string
$params : array<string|int, mixed> = []

callApi4AjaxSuccess()

protected callApi4AjaxSuccess(string $entity, string $action[, array<string|int, mixed> $params = [] ]) : mixed
Parameters
$entity : string
$action : string
$params : array<string|int, mixed> = []

formatFailure()

Given that an HTTP request has yielded a failed response, format a blurb to summarize the details of the request+response.

protected formatFailure(ResponseInterface $response) : false|string
Parameters
$response : ResponseInterface
Return values
false|string

getMagicProperties()

Get a list of class properties for which magic methods are supported.

protected static getMagicProperties() : array<string|int, mixed>
Return values
array<string|int, mixed>

List of supported properties, keyed by property name. Array(string $propertyName => bool $true).

resolveResponse()

protected resolveResponse(ResponseInterface|null $response) : ResponseInterface
Parameters
$response : ResponseInterface|null
Return values
ResponseInterface

__construct()

private __construct(string $class, string $name, string $id, string $indexFile, string $codeFile) : mixed
Parameters
$class : string
$name : string
$id : string
$indexFile : string
$codeFile : string

createClient()

private createClient() : Client|ClientInterface
Return values
Client|ClientInterface

getCodePath()

private static getCodePath(string $className, string $name) : string
Parameters
$className : string
$name : string
Return values
string

getIndexPath()

The folder which stores information about test functions.

private static getIndexPath(string $id) : string
Parameters
$id : string
Return values
string

render()

private render(ReflectionClosure $refl) : string
Parameters
$refl : ReflectionClosure
Return values
string

save()

private save(ReflectionClosure $refl) : void
Parameters
$refl : ReflectionClosure

writeFile()

private writeFile(string $path, string $content) : void
Parameters
$path : string
$content : string

        
On this page

Search results