RemoteTestFunction
in package
uses
HttpTestTrait, MagicGetterSetterTrait
RemoteTestFunctions may be used by tests to define (inline) code that will run on a remote server.
Tags
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'
$responseDecoder
protected
callable|null
$responseDecoder
$responseType
protected
string
$responseType
= 'application/json'
$class
Name of the test-class which defined the RTF.
private
string
$class
$codeFile
Path to a file with the minimal/extracted version of the RTF.
private
string
$codeFile
$id
private
string
$id
$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
Return values
static|mixed_run()
public
_run([mixed $args = [] ]) : mixed
Parameters
- $args : mixed = []
byId()
public
static byId(string $id) : RemoteTestFunction|null
Parameters
- $id : string
Return values
RemoteTestFunction|nullbyName()
Lookup the implementation of a remote-test-function.
public
static byName(string $class, string $name) : RemoteTestFunction|null
Parameters
- $class : string
- $name : string
Return values
RemoteTestFunction|nullexecute()
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.
getRequestChannel()
public
getRequestChannel() : string
Return values
stringgetRequestMethod()
public
getRequestMethod() : string
Return values
stringgetResponseDecoder()
public
getResponseDecoder() : callable|null
Return values
callable|nullgetResponseType()
public
getResponseType() : string
Return values
stringhttpRequest()
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
RequestInterfaceregister()
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
RemoteTestFunctionsetRequestChannel()
public
setRequestChannel([string $channel = ]) : $this
Parameters
- $channel : string =
Return values
$thissetRequestMethod()
public
setRequestMethod([string $method = ]) : $this
Parameters
- $method : string =
Return values
$thissetResponseDecoder()
public
setResponseDecoder([callable $decoder = ]) : $this
Parameters
- $decoder : callable =
Return values
$thissetResponseType()
public
setResponseType([string $type = ]) : $this
Parameters
- $type : string =
Return values
$thisassertBodyRegexp()
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
$thisassertNotBodyRegexp()
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
$thiscallApi4AjaxError()
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> = []
createDecoder()
protected
createDecoder() : callable
Return values
callablecreateGuzzle()
Create an HTTP client suitable for simulating AJAX requests.
protected
createGuzzle([array<string|int, mixed> $options = [] ]) : Client
The client may include some mix of these middlewares:
Parameters
- $options : array<string|int, mixed> = []
Tags
Return values
ClientformatFailure()
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|stringgetMagicProperties()
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
assertTestEnvironment()
private
static assertTestEnvironment() : void
createClient()
private
createClient() : Client|ClientInterface
Return values
Client|ClientInterfacegetCodePath()
private
static getCodePath(string $className, string $name) : string
Parameters
- $className : string
- $name : string
Return values
stringgetIndexPath()
The folder which stores information about test functions.
private
static getIndexPath(string $id) : string
Parameters
- $id : string
Return values
stringrender()
private
render(ReflectionClosure $refl) : string
Parameters
- $refl : ReflectionClosure
Return values
stringsave()
private
save(ReflectionClosure $refl) : void
Parameters
- $refl : ReflectionClosure
writeFile()
private
writeFile(string $path, string $content) : void
Parameters
- $path : string
- $content : string