Skip to content

Testing with fake() ​

AnikeenConsole::fake() swaps the client for a fake that records every request and answers from your responses - no network, no tokens.

php
use Anikeen\Console\Facades\AnikeenConsole;
use Anikeen\Console\Testing\AnikeenConsoleFake;
use Anikeen\Console\Testing\RecordedRequest;

$fake = AnikeenConsole::fake([
    // "METHOD path" or "path" (relative to the base URL, * matches anything) => response
    'GET containers/01JCT' => ['id' => '01JCT', 'name' => 'game-1', 'status' => 'running'],
    'POST containers/*/power' => fn (RecordedRequest $request) => ['message' => "Doing {$request->json['action']}"],
    'GET catalog' => AnikeenConsoleFake::response(['message' => 'Missing scope.', 'error' => 'missing_scope'], 403),
]);

$this->post('/servers', [...]);   // your code calls AnikeenConsole::forUser($user)->...

$fake->assertOrdered(fn (array $items, array $body, string $team) => $items[0]['family'] === 'container');
$fake->assertNothingOrdered();
$fake->assertSent('POST containers/*/power');
$fake->assertNotSent(fn (RecordedRequest $request) => $request->method === 'DELETE');
$fake->recorded();                // every RecordedRequest (method, path, query, json, header())

Responses ​

You giveThe fake answers
an arrayJSON, 201 for orders, else 200
a closureits return value; it receives the RecordedRequest
AnikeenConsoleFake::response($body, $status)that body and status - e.g. to test errors

Add or replace responses later with $fake->respondWith([...]).

Defaults ​

Without a matching response the fake answers with defaults:

RequestDefault
GET app-teama team (team_fake)
POST teams/{team}/ordersan open order with a checkout_url
listsempty
other POSTs202
deleting a container204
anything else200 {}

Users need no tokens with the fake, and nothing is refreshed.

Assertions ​

Method
assertSent($match)a request matched - "METHOD path" pattern or closure
assertNotSent($match)no request matched
assertNothingSent()no request at all
assertOrdered(?$callback)an order was placed; the callback gets $items, $body, $team
assertNothingOrdered()no order was placed
recorded(?$filter)the RecordedRequests (method, path, query, json, header())

Webhooks ​

Build webhook requests signed with your configured secret and send them through your app:

php
use Anikeen\Console\Facades\AnikeenConsole;
use Anikeen\Console\Webhooks\EventType;

AnikeenConsole::webhookRequest(EventType::PRODUCT_READY, [
    'id' => '01JCT', 'family' => 'container', 'team_id' => 'team_fake', 'status' => 'running',
])->send()->assertOk();

webhookRequest($type, $object, $data = [], $secret = null) returns a WebhookRequest with the event, the signed body and the headers:

  • send($uri) posts it to the webhook route and returns the test response,
  • toRequest($uri) gives an Illuminate\Http\Request,
  • withHeaders([...]) adds or overrides headers (e.g. to test a bad signature).

Anikeen Console documentation