Skip to content

Testing

Most of a PAM Desktop application is plain PHP, so most of it can be tested without a host or a window. The Pam\Desktop\Testing helpers mount components through their real lifecycle, render them and read back their text; pam desktop check validates every template and route statically; and end-to-end scripts drive the real window through the same input path as a user. Use the cheapest layer that proves the behavior.

Layer Needs Use it for
Services and domain classes PHP only Business rules, formatting, data mapping
Testing\mount() PHP only Component state, actions and rendered text
pam desktop check PHP only Templates, stylesheets, routes
PAM_DESKTOP_SCRIPT The host and a display Critical flows in the real window

mount() runs a component through boot(), mount() and the first render, without a host:

tests/HomeScreenTest.php
<?php
declare(strict_types=1);
use App\Screens\Home\HomeScreen;
use Pam\Desktop\App;
use function Pam\Desktop\Testing\mount;
require dirname(__DIR__).'/vendor/autoload.php';
App::components(dirname(__DIR__).'/src', dirname(__DIR__).'/.pam-desktop/components');
function check(bool $passed, string $what): void
{
if (!$passed) {
fwrite(STDERR, "failed: {$what}\n");
exit(1);
}
}
$home = mount(HomeScreen::class);
check(str_contains($home->text(), '0 clicks'), 'the home screen starts at zero');
$home->instance->increment();
$home->render();
check(str_contains($home->text(), '1 click'), 'a press is counted');
$home->unmount();
echo "ok\n";
Terminal window
pam tests/HomeScreenTest.php
Member Meaning
mount($component, $props = [], $params = null) A class name or instance; $props are constructor arguments by name; $params are route parameters.
->instance The component, to call actions and read state.
->render() Renders again after you changed state; returns the root element.
->text() All the text of the rendered tree, joined with spaces.
->focus(), ->blur() Simulate the route becoming visible or hidden.
->unmount() Runs the unmount hooks.

The skeleton’s tests are plain PHP scripts. PHPUnit or Pest work the same way: call mount() inside a test method and assert on text() and instance.

environment() makes the framework believe the host reported a color scheme, window size, locale or time zone, through the same events a running app receives:

use function Pam\Desktop\Testing\environment;
use function Pam\Desktop\Testing\reset;
environment(scheme: 'dark', window: ['width' => 800, 'height' => 600, 'fontScale' => 1.5]);
environment(locale: 'pt-BR', timezone: 'America/Sao_Paulo');
$screen = mount(DashboardScreen::class);
// assert on the dark, narrow, large-text rendering…
reset(); // back to a clean runtime between tests

For an application that uses routes.php, routes() mounts the real navigator and returns a tester:

use function Pam\Desktop\Testing\routes;
$app = routes(__DIR__.'/../routes.php')->open('/chat/1');
check($app->path() === '/chat/1', 'the deep link opens the chat');
check(str_contains($app->text(), 'Ana'), 'the chat shows the contact');
$app->back();

Without a host there is nobody to answer module calls. Keep I/O in services you can replace with fakes, or install a test transport with NativeModules::useTransport(). When you do run against a host, start it with PAM_DESKTOP_SECURE_STORAGE=memory and temporary PAM_DESKTOP_DATA_DIR, PAM_DESKTOP_CACHE_DIR and PAM_DESKTOP_STATE_DIR, so tests never touch the real keychain or files.

Terminal window
pam desktop check

check compiles every component and reports unknown attributes, events and props, CSS errors, route errors and template expressions that cannot run. It exits 1 on any diagnostic, so put it first in CI. See Debugging for the codes.

PAM_DESKTOP_SCRIPT=steps.json runs a script against the real window and exits with its result: 0 when every step passed, 1 at the first failure. Steps go through the same core as a user’s input, so a script behaves the same on every system:

tests/e2e.json
[
{"wait": "Inbox"},
{"click": "#new-message"},
{"type": "Shipped the fix", "into": "#composer"},
{"submit": "#composer"},
{"expect": "Shipped the fix"},
{"key": "CmdOrCtrl+B"},
{"expectNot": "EXPLORER"}
]
Terminal window
PAM_DESKTOP_SCRIPT=tests/e2e.json PAM_DESKTOP_WINDOW=1200x800 pam desktop run --yes
Step Does
{"wait": selector} Waits until it is on screen.
{"click": …}, {"doubleClick": …}, {"hover": …} The pointer at the center of the element.
{"type": "text", "into": selector} Types into a field, or the focused one without into.
{"submit": selector} Enter in a field.
{"key": "CmdOrCtrl+P"} A registered shortcut, else the key for the focused element.
{"expect": …}, {"expectNot": …} Fails unless it appears, or disappears, within the timeout.
{"sleep": ms} Waits.

A selector is #testId, @accessibility label, or text the element shows. Each step waits up to 10 seconds. When one fails, the output lists the texts on screen. Give elements a testId in templates (testId="composer") so scripts survive copy changes. On a Linux CI runner, run the script under Xvfb as shown in Debugging.