Skip to content
v1
Docs

PHP SDK

Testing your integration

Nothing in your tests needs to reach WatchTower15.

Testing\FakeClient is a Client that never reaches a network. It answers pushes itself, and records every call of the REST API methods, the waiter and request().

use WatchTower15\Testing\FakeClient;
use WatchTower15\Testing\MockTransport;
$fake = new FakeClient();
$fake->queue('public.servers.list', MockTransport::json(['data' => [$serverRow], 'current_page' => 1, 'per_page' => 25, 'last_page' => 1, 'total' => 1]));
$names = (new Inventory($fake))->serverNames(); // your code, typed against Client
$fake->assertCalled('public.servers.list');
$fake->assertNotCalled('public.incidents.acknowledge');
$fake->assertPushed(fn (array $point) => $point['metric'] === 'orders.completed');

Queue an answer by the operation’s id (queue('public.me.view', $response)); queue('request:GET /servers/{server_uuid}/ports', …) answers a request() call by its path. A call with nothing queued for its operation throws a LogicException naming it. The fake never sleeps: the waiter’s polls and the paging’s waits run on a clock that only moves forward.

Testing\MockTransport queues responses and records requests for lower-level tests.

Testing\FakeAggregator is built with no arguments and records increment() and gauge():

$metrics = new FakeAggregator();
$service = new Checkout($metrics);
$service->ship($order);
$metrics->assertIncremented('orders.shipped', 3);
$metrics->assertGaugeSet('queue.depth');
$metrics->assertNothingRecorded();

Its flush() pushes every bucket, the open one included, into its own FakeClient (client()).

Wt15::fake() records counters, gauges and API calls and sends nothing:

use WatchTower15\Laravel\Facades\Wt15;
$fake = Wt15::fake();
event(new OrderRefunded(amount: 5));
$fake->assertIncremented('orders.refunded');
Wt15::client()->assertCalled('public.me.view');

Your bindings record through it, a rolled-back transaction records nothing, and a key that no row declares is not recorded, as in production. The assertions are assertIncremented($key, $by = null), assertNotIncremented($key), assertGaugeSet($key, $value = null), assertNothingRecorded(), and for log lines assertLogged($level, $contains), assertNothingLogged() and loggedLines(). Under APP_ENV=testing the metrics pipe sends into a fake client even without Wt15::fake(), unless wt15.fake_in_tests is false.

The Symfony bundle and the CakePHP plugin ship no fake of their own. Type your code against WatchTower15\Client and WatchTower15\Metrics\Aggregator, which the container autowires, and bind FakeClient and FakeAggregator for your tests.