PHP testing
Use CalmLens\Testing\MockServerClient to test your PHP SDK integration offline against an in-memory CalmLens, or CalmLens\Testing\MockTransport to replay responses you queue for exact request and error tests.
Setup
MockServerClient runs the real client's validation, encoding, retries and error handling, then answers from an in-memory stand-in for the API and CDN. $mock->client() returns the ServerClient to pass to the code under test.
<?php
require 'vendor/autoload.php';
$mock = new CalmLens\Testing\MockServerClient();
$client = $mock->client();
$asset = $client->assets->create(['name' => 'photo.png', 'visibility' => 'public',
'content' => ['data' => $bytes, 'type' => 'image/png']]); // pending
$mock->completeProcessing($asset['id'], 'age-restricted'); // as moderation would
$mock->decide($asset['id'], 'blocked'); // as a reviewer would; adds asset.blockedOptions
The constructor takes:
| Argument | Effect |
|---|---|
moderate | Decides each asset the mock moderates. See Moderation decisions. |
apiKey | A key from MockServerClient::createApiKey($grants). The default key has no grants. |
now, createId | Deterministic clock and IDs. Assets created in a row get increasing createdAt values by default, so newest-first results are predictable. |
projectId | The project the mock's client uses, project-1 by default. |
baseUrl, cdnBaseUrl, maxRetries, maxResponseBytes, uploadTimeout, partSizeBytes | Client settings. |
Content is kept only with keepContentAfterProcessing.
To read private assets, pass a key with a download grant:
$mock = new CalmLens\Testing\MockServerClient(
apiKey: CalmLens\Testing\MockServerClient::createApiKey([
['actions' => ['assets:download'], 'folderId' => null, 'includeSubfolders' => true],
]),
);A folderId limits the grant to that folder, and $mock->setApiKeyGrants($grants) changes the same key's grants later. The stored records are public properties, such as $mock->assets, so you can change them to arrange a test.
Moderation decisions
The mock doesn't classify content. The moderate callable decides each asset the mock moderates instead. It receives a CalmLens\Testing\MockModerationInput with:
asset: the stored asset, still processing.content: aCalmLens\Testing\MockModerationContentwithbytes, the uploaded bytes with the parts of a multipart upload joined in order, andtext, the bytes as text for a plain-text upload such astext->classify. Both arenullwhen there is no content, such as for a website.trigger: why the asset is moderated:upload,retry(afterassets->retry),rerun(afterassets->explainwith'rerun' => true) orcomplete(see below).queued:truewhen CalmLens would queue the work rather than answer with the decision: queued uploads, retries, reruns andcomplete.
It returns a status, pending, failed, allowed, limited, age-restricted, blocked or purged, or a decision built with MockDecision. pending leaves the asset processing, a failed asset gets classification.failureReason processing-error, and purged also removes the content, with reason policy. Without moderate, queued uploads, retries and reruns stay pending and everything else is allowed.
CalmLens\Testing\MockDecision::of($status) builds a decision with findings: flag($category, $score, $qualifiers) records a category's score from 0 to 1 and, optionally, its qualifiers' scores, and notEvaluated($category) marks a category as not assessed. Both return the decision, so calls chain. A decided asset gets a complete report, as from CalmLens: raw.categories holds every category (unflagged ones evaluated with score 0), categories holds each evaluated category's policy score and action, and flaggedCategories lists the categories the decision acted on (categories scored 0.5 or more, unless the status is allowed). An unknown status, category or qualifier, a score outside 0 to 1, a category used twice and findings on a pending or failed decision throw InvalidArgumentException with sdk/invalid-argument. CalmLens\Testing\Classification lists the categories and qualifiers.
<?php
require 'vendor/autoload.php';
use CalmLens\Testing\MockDecision;
use CalmLens\Testing\MockModerationInput;
use CalmLens\Testing\MockServerClient;
$mock = new MockServerClient(moderate: fn (MockModerationInput $input) => str_contains((string) $input->content->text, 'kill')
? MockDecision::of('blocked')->flag('violence', 0.92, ['violence-threat' => 0.9])
: 'allowed');
$comment = $mock->client()->text->classify(['text' => 'I will kill you']);
$moderation = $comment['classification']['moderation'];
// $moderation['categories']['violence']['score'] is 0.92
// $moderation['flaggedCategories'] is ['violence']A moderate callable decides queued uploads straight away too. Return pending for them ($input->queued && $input->trigger === 'upload') to finish them later.
Finish processing
$mock->completeProcessing($assetId, $decision) finishes processing a pending asset as CalmLens would, with the decision you pass (a status or a MockDecision) or else the one moderate returns for the complete trigger (allowed by default). It records no review, so the asset can still be moderated again with assets->explain. It throws NotFoundException with 404 asset/not-found for an unknown asset and InvalidArgumentException with sdk/invalid-argument for an asset that isn't processing or awaiting a pending re-run:
$upload = $client->assets->create(['name' => 'photo.png',
'content' => ['data' => $bytes, 'type' => 'image/png']]); // pending
$mock->completeProcessing($upload['id'], MockDecision::of('age-restricted')->flag('sexual', 0.8));
$decided = $client->assets->waitForDecision(['assetId' => $upload['id']]);
// $decided['classification']['moderation']['status'] is 'age-restricted'What the mock simulates
- Assets you upload can be retrieved, listed, updated, moved and deleted. Unset asset fields are
null(tagsandmetadataare empty lists), as in the API, and moderation fields staynulluntil a decision. A decided image reports the features its upload turned on as finding nothing: OCR and person and face detection when requested, link detection unless turned off. Requests the API's schema refuses fail with 400 validation/invalid-request, listing the field and its issue, such asvalidation/too-big, indetails['issues']. Error responses carry the API's body, with arequestIdmatching theX-Request-Idheader. - Folders, hash lists, child listings, lenses, multipart uploads and idempotency keys behave like the API. Folder pages are sorted by name or creation time, with the folder ID breaking ties. Uploads are not matched against hash lists.
- Policies: the project starts on the organisation's own General preset policy,
live. The mock's presets have one release, version 1, with every category at its default action and no rules; policy settings that leave out a category, or a category'saction,allowancesorrestrictions, fail with 400 validation/invalid-request. Policies never decide uploads. - IDs look like the API's: a prefix,
_and 20 random base62 characters, such asasset_5Gm2Rk8XqT3wLp9VbN4c. For predictable IDs, passcreateId, a callable that receives the prefix (asset,fldr,hl,hle,pol,lens,evt,revorbatch) and returns the ID. - Reviewer decisions from
$client->assets->decide()are refused like the API's: an asset pending moderation can only be blocked or purged (409 asset/decision-not-allowed), and its current status gets 409 asset/decision-unchanged. - Lists return 50 items by default and filter like the API: the documented asset fields with
==,!=,>,>=,<,<=,=in=and=out=, joined by;(and), which binds tighter than,(or), and grouped with( … ).*in an==or!=value matches any run of characters, ignoring case. Any other field, operator or syntax fails with 400 filter/invalid, and asortby anything butcreatedAtoridfails with 400 validation/invalid-request (issuevalidation/invalid-value). Cursors resume after the last item returned, so assets created or deleted between pages are neither repeated nor skipped. - Search matches every word of the query in an asset's name, ID, external ID, description, tags, type, content format or extension, or moderation status, and ranks results with the API's keyword scores.
- Waiting for a decision runs on a virtual clock, so a timeout doesn't slow your tests.
- CDN access: like the CDN, the mock refuses blocked, purged and failed assets, and assets whose content was removed, with 403 asset/not-available whatever the key. It refuses age-restricted assets requested without
'viewerClaims' => ['ageConfirmed' => true]with 403 viewer/age-confirmation-required, whatever the key. Without an assets:download grant, it also refuses private assets with 403 auth/permission-denied. - CDN responses carry the stored
Content-TypeandCache-Controlas the CDN sets it (publiconly when anyone may see the asset, otherwiseprivate;max-age=31536000, immutablefor images andmax-age=3600for other content;private, no-storewhen the request carries viewer claims), plusCache-Tag,X-Content-Type-Options: nosniff, a sandboxingContent-Security-Policyand, for images, anETagandExpires. A classified image gets a ThumbHash, which theplaceholderresource serves as a PNG and which goes with the content. URLs fromcdn->signAssetUrl()are served,/infoincluded, until they expire, and$mock->fetchCdn($url)loads one as a browser would, without the API key.
Events and reviewer decisions
Every upload adds asset.uploaded to the events feed, then asset.classified and the automated asset.<status> (whose actor is automated) once it is decided, or asset.failed. Like the API, deleting an asset, removing its content and retrying it add asset.deleted, asset.content-removed and asset.retried events. $client->assets->explain() with 'rerun' => true adds asset.rerun-requested and returns the asset unchanged: it keeps its decision until the one moderate returns replaces it, and keeps it when that is failed; a pending re-run waits for completeProcessing. $client->assets->decide() adds the asset.<status> event for a reviewer's decision, with its note. $mock->enqueueEvent($event) adds others to the feed, for cursors taken earlier, and returns the stored event; an event about a stored asset carries the asset as it is, as from CalmLens.
$mock->decide($assetId, $decision) applies a reviewer's decision (allowed, limited, age-restricted, blocked or purged) to a stored asset: it records the review in the asset's manualReviews and adds the asset.<decision> event with its usual severity (low for allowed and limited, high otherwise), whose actor is a user. A reviewer's purged removes the content with reason requested and first adds asset.content-removed. The moderation report stays as it was. An unknown asset throws NotFoundException with 404 asset/not-found. pending and failed are not decisions and throw InvalidArgumentException with sdk/invalid-argument; to finish processing as CalmLens's moderation would, use completeProcessing.
Recorded requests and failures
$mock->requests() returns every request received, oldest first, as CalmLens\Testing\RecordedRequest objects:
methodandpath, which is relative to the API or CDN base URL, so it leaves out/v1, with each segment decoded, such as/assets/<id>or/projects/project-1/assets.query: the query parameters as sent, decoded, such as['sort' => '-createdAt'].status: the HTTP status the mock answered with, ornullfor a request that failed without a response.request: the PSR-7 request, with its headers and a readable body.
$mock->clearRequests() forgets the requests recorded so far.
$client->text->classify(['text' => 'hello', 'externalId' => 'comment-1']);
$request = $mock->requests()[0];
// $request->method is 'POST', $request->path is '/projects/project-1/assets'
// $request->query['externalId'] is 'comment-1', $request->status is 200$mock->failNextRequest($failure) fails the next request:
$failure | Fails with |
|---|---|
'transport-error' (the default) | A connection failure |
'timeout' | A timeout |
An API error code, such as 'usage/free-uploads-exhausted' | That error and its HTTP status |
| An exception | That exception |
Pass a client method such as 'assets.create' as operation to fail the next request it sends instead; MockServerClient::OPERATIONS lists them, and another name throws InvalidArgumentException with sdk/invalid-argument. For an API error code, details sets the error's details and retryAfter answers with a Retry-After of that many seconds, so you can test code that reads $error->details or $error->retryAfter(). They don't apply to a transport error, a timeout or an exception: passing either with one throws InvalidArgumentException with sdk/invalid-argument, and nothing is queued. Each queued failure fails one request, so a retry takes the next queued failure or reaches the stand-in. Failures from failNextRequest are used first, then responses queued with enqueueJson or enqueue, before the stand-in answers.
$mock->failNextRequest('timeout', operation: 'text.classify');
// The next text->classify throws TimeoutException (sdk/timeout); the one after succeeds.
$mock->failNextRequest('upload/website-rate-limit-reached', retryAfter: 30);
// The next request throws RateLimitException: status 429, retryAfter() 30.0.The mock's maxRetries defaults to 0, unlike the real client, so a queued failure fails one call; pass maxRetries to the constructor to test retry handling. Retry backoff takes no real time.
Queued responses
MockTransport supplies FIFO responses while the real SDK validates options and serialises requests. It doesn't moderate content or store assets.
<?php
require 'vendor/autoload.php';
$mock = new CalmLens\Testing\MockTransport();
$mock->enqueueJson([
'id' => 'asset-1',
'upload' => ['status' => 'completed'],
'classification' => ['status' => 'completed', 'moderation' => ['status' => 'blocked']],
]);
$client = $mock->client();
$asset = $client->text->classify(['text' => 'Example comment']);
if ($asset['classification']['moderation']['status'] !== 'blocked') {
throw new RuntimeException('Expected the comment to be blocked');
}
$request = $mock->requests()[0];client($projectId)returns a client for the project,project-1by default.enqueueJson($body, $status, $headers)queues an API response, andenqueue($responseOrException)a PSR-7 response or a transport failure.requests()returns the requests sent asRecordedRequestobjects, like the in-memory mock's, with paths relative to the default base URLs, andpending()counts unused responses. An exhausted queue fails.- Uploads must be answered with an asset (an
id,upload.status,classification.statusandclassification.moderation.status), or they fail withsdk/invalid-response.
MockTransport's client never retries. For retry tests with queued responses, give ServerClient a Guzzle client whose handler is a Guzzle MockHandler, or use MockServerClient with maxRetries.
Webhook handlers
CalmLens\Webhooks::toPayload($event) converts an event from $client->events->list() or $mock->enqueueEvent() into the body a webhook delivers, which has the same flat fields as the event, and Webhooks::createHeaders($payload, $deliveryId, $secret) returns the headers of a signed delivery, using the current time unless you pass a timestamp. Use them together to call your handler as CalmLens would:
$event = $mock->enqueueEvent(['type' => 'asset.allowed', 'assetId' => $asset['id']]);
$body = json_encode(CalmLens\Webhooks::toPayload($event));
$headers = CalmLens\Webhooks::createHeaders($body, 'delivery-1', $secret);Webhooks::signatureHeader($payload, $timestamp, $secret) builds only the x-calmlens-signature value.
Upload tokens
MockServerClient::signUploadToken($apiKey, $options, $now, $nonce) signs an upload token at a fixed clock and nonce, for tests that compare exact tokens.
Generate test inputs
CalmLens\Testing\TestInputs generates authenticated test inputs from your project's secret test input key. CalmLens recognises them only in projects with Test inputs turned on. generateTestTextCode($scenario, $testInputKey) returns a text code to submit as the whole text: text with anything else around the code is moderated as ordinary content. generateMediaTestCode($signal, $testInputKey) returns a phrase to read aloud or show in a video. A scenario is an array of instances (each with category, qualifiers and optional score and action), and optional regionalActions and unassessedCategories. Pass a category, such as 'violence', instead of a scenario for one instance of it. TestInputs::PRESETS holds the portal's presets. An invalid key or scenario throws CalmLens\Exception\InvalidArgumentException with code sdk/invalid-argument.
<?php
require 'vendor/autoload.php';
use CalmLens\Testing\TestInputs;
$testInputKey = (string) getenv('CALMLENS_TEST_INPUT_KEY');
// Choose each instance's outcome: the strongest action wins
$text = TestInputs::generateTestTextCode(['instances' => [
['category' => 'harassment', 'qualifiers' => ['harassment-abuse', 'harassment-documentary'], 'action' => 'limit'],
['category' => 'harassment', 'qualifiers' => ['harassment-threat'], 'action' => 'limit'],
['category' => 'harassment', 'qualifiers' => ['harassment-abuse'], 'action' => 'block'],
]], $testInputKey);
// Regional delivery outcomes without category findings
$regional = TestInputs::generateTestTextCode(TestInputs::PRESETS['regionalDelivery'], $testInputKey);
// A test image to upload on its own: it survives JPEG, WebP and resizing
$image = TestInputs::paintTestImage('violence', $testInputKey);
file_put_contents('test-image.png', $image->png);
// A phrase to read aloud or show in a video
$phrase = TestInputs::generateMediaTestCode('violence', $testInputKey);
echo $text, PHP_EOL, $regional, PHP_EOL, $phrase, PHP_EOL;Set action on every instance to choose its outcome independently of the project policy, or on none to test the policy itself. TestInputs::paintTestImage($scenario, $testInputKey) returns a CalmLens\Testing\PaintedTestImage: upload its png bytes as the whole image, or encode its rgba pixels (width by height) in another format. An image with anything else drawn on or around the test image, apart from a plain single-colour border, is moderated as ordinary content. A signal stands for its image scenario, so 'sexual.adult' stands for adult imagery rather than explicit text. A scenario too large for a test image throws sdk/invalid-argument; split it or use a text code.