Testing
Most of a PAM Native app is PHP, so most of it can be tested like any PHP code: fast,
on your machine, without a device. PAM Native adds a small testing API,
Pam\Native\Testing, that mounts components and routes through the real
runtime (lifecycle hooks, composables, middleware and focus) and lets you fake what
the device would report: dark mode, safe-area insets, the keyboard, the window
size, the locale and the time zone.
Every new project already contains a tests/ directory, a phpunit.xml and
PHPUnit as a development dependency.
Running tests
Section titled “Running tests”Run the suite with the PAM CLI. It finds PHPUnit (or Pest, when installed) in
vendor/ and runs it inside PAM’s embedded PHP:
pam testExtra arguments go to the test runner:
pam test --filter GreetingTestIf both runners are installed, choose one with --phpunit or --pest.
A base test case
Section titled “A base test case”Single-file components need to be registered, and utility classes such as
surface or text-primary need the app’s theme, exactly as in index.php. Put
that setup, and the teardown, in one base class:
<?php
declare(strict_types=1);
namespace Tests;
use Pam\Native\App;use Pam\Native\Testing;use Pam\Native\Theme;use PHPUnit\Framework\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase{ protected function setUp(): void { parent::setUp();
$root = dirname(__DIR__); App::components($root.'/src', $root.'/.pam-native/components'); App::theme(Theme::pamLab()); }
protected function tearDown(): void { Testing\reset();
parent::tearDown(); }}Register the Tests\ namespace in composer.json:
{ "autoload-dev": { "psr-4": { "Tests\\": "tests/" } }}Then regenerate the autoloader:
pam composer dump-autoloadTesting\reset() unmounts every root, shuts the runtime down and clears
composable sources, so each test starts clean.
Testing components
Section titled “Testing components”Testing\mount() creates a component, runs boot(), setup(), mount(), the
first render and onMounted(), and returns a Mounted handle. Pass props by
name:
<?php
declare(strict_types=1);
namespace App\Components;
use Pam\Native\Attributes\Prop;use Pam\Native\Component;
final class Greeting extends Component{ public function __construct( #[Prop(required: true)] public readonly string $name, #[Prop] public readonly int $unread = 0, ) { }}?>
<template language="2"> <Column> <Text>Hello, {{ $name }}</Text> <Text p-if="$unread > 0">{{ $unread }} new messages</Text> </Column></template><?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Components\Greeting;use Pam\Native\Testing;use Tests\TestCase;
final class GreetingTest extends TestCase{ public function test_it_greets_by_name(): void { $greeting = Testing\mount(Greeting::class, ['name' => 'Alice']);
$this->assertSame('Hello, Alice', $greeting->text()); }
public function test_it_shows_unread_messages(): void { $greeting = Testing\mount(Greeting::class, ['name' => 'Alice', 'unread' => 3]);
$this->assertSame('Hello, Alice 3 new messages', $greeting->text()); }}The Mounted handle gives you:
| Member | Description |
|---|---|
instance |
The component object, so you can call actions and read state. |
render() |
Runs another render pass and returns the root Element. |
element() |
The Element of the last render. |
text() |
Every Text of the last render, joined by a space. Button labels are properties, not Text nodes. |
focus(), blur() |
Report navigation focus, running onFocus() / onBlur() hooks. |
unmount(), mounted() |
Unmount (running onUnmounted() and releasing everything setup() created), and check whether it is still mounted. |
Actions and state
Section titled “Actions and state”Call an action on the instance, then render again, just as a tap would:
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Hello;use Pam\Native\Testing;use Tests\TestCase;
final class CounterTest extends TestCase{ public function test_increment_updates_the_count(): void { $counter = Testing\mount(Hello::class);
$counter->instance->increment(); $counter->render();
$this->assertSame(1, $counter->instance->count); }}Screens, parameters and focus
Section titled “Screens, parameters and focus”Pass params to mount a component as a screen: the values reach setup() like
route parameters (and are converted to the declared types), and the screen also
receives onFocus(), as it would from the router. Use it to check that work stops
when a screen is hidden:
<?php
declare(strict_types=1);
namespace Tests\Feature;
use App\Screens\LivePrices;use Pam\Native\Testing;use Tests\TestCase;
final class LivePricesTest extends TestCase{ public function test_it_refreshes_on_mount(): void { $screen = Testing\mount(LivePrices::class, params: []);
$this->assertSame('Refreshed 1 times', $screen->text());
$screen->blur(); // another screen covered it: onBlur() cancels polling $screen->focus(); // it is visible again $screen->unmount();
$this->assertFalse($screen->mounted()); }}LivePrices is the focus-aware screen from Architecture.
Testing routes
Section titled “Testing routes”Testing\routes() loads your routes.php, activates the real navigator (with
middleware from config/middleware.php) and mounts it. Open paths or deep links and
navigate as a user would:
<?php
declare(strict_types=1);
use App\Screens\HomeScreen;use App\Screens\ProductScreen;use Pam\Native\Route;
Route::screen('/', HomeScreen::class)->name('home');Route::screen('/products/{id}', ProductScreen::class)->name('product');<?php
declare(strict_types=1);
namespace Tests\Feature;
use Pam\Native\Testing;use Tests\TestCase;
final class RoutesTest extends TestCase{ public function test_a_deep_link_opens_the_product(): void { $app = Testing\routes(dirname(__DIR__, 2).'/routes.php')->open('/products/42');
$this->assertSame('product', $app->current()?->routeName()); $this->assertSame('Product 42', $app->text());
$app->back(); $this->assertSame('home', $app->current()?->routeName());
$app->navigate('product', ['id' => '7']); $this->assertSame('/products/7', $app->path()); }}The RouteTester offers open($uri), navigate($route, $params), back(),
current(), path(), screen() (the focused screen component), text() (of the
focused screen), render() and unmount(). Registered routes survive
Testing\reset(); pass flushRoutes: true to clear them as well.
Simulating the device
Section titled “Simulating the device”Testing\environment() sends the same events the host would, and mounted
components re-render:
use Pam\Native\Testing;
use function Pam\Native\useColorScheme;use function Pam\Native\useSafeAreaInsets;
Testing\environment( scheme: 'dark', insets: ['top' => 24, 'bottom' => 34], keyboard: 280, window: ['width' => 390, 'height' => 844, 'fontScale' => 1.3], locale: 'pt-BR', timezone: 'America/Sao_Paulo',);
$this->assertSame('dark', useColorScheme()->value);$this->assertSame(34.0, (float) useSafeAreaInsets()->bottom);| Argument | Effect |
|---|---|
scheme |
'light' or 'dark', reported as the system scheme. |
insets |
top, right, bottom, left (and cutoutTop… for display cutouts). |
keyboard |
A height shows the keyboard; 0 or false hides it; true is 300 pt. |
window |
width, height, scale, fontScale. |
locale, timezone |
What useLocale() and useTimezone() read. |
Arguments you leave out keep their value. This is how you check that a layout respects the safe area and large text without a device; see Safe Areas and Accessibility.
Faking native modules
Section titled “Faking native modules”Code that calls native modules (directly or through a plugin facade built on
NativeModules::call()) can run against a fake transport. Implement
NativeModuleTransport and install it with NativeModules::useTransport():
<?php
declare(strict_types=1);
namespace Tests\Feature;
use Closure;use Pam\Native\ModuleResultStatus;use Pam\Native\Modules\NativeModuleResult;use Pam\Native\Modules\NativeModules;use Pam\Native\Modules\NativeModuleTransport;use Tests\TestCase;
final class FakeTransport implements NativeModuleTransport{ /** @var list<array{string, string}> */ public array $calls = [];
public function __construct(private readonly string $reply) { }
public function invoke(int $requestId, string $module, string $method, string $payload, Closure $complete): void { $this->calls[] = [$module, $method]; $complete(ModuleResultStatus::Success, $this->reply); }}
final class WeatherTest extends TestCase{ protected function tearDown(): void { NativeModules::useTransport(null);
parent::tearDown(); }
public function test_it_asks_the_weather_module(): void { $fake = new FakeTransport('sunny'); NativeModules::useTransport($fake);
$forecast = null; NativeModules::call('acme.weather', 'today', ['city' => 'Lisbon'], function (NativeModuleResult $result) use (&$forecast): void { $forecast = $result->payload; });
$this->assertSame([['acme.weather', 'today']], $fake->calls); $this->assertSame('sunny', $forecast); }}Complete with ModuleResultStatus::Failure to exercise error paths. Always restore
the real transport with NativeModules::useTransport(null) in tearDown().
Testing stores
Section titled “Testing stores”Global stores have their own testing helpers for actions, snapshots and time travel. See Global Store.
On-device checks
Section titled “On-device checks”Unit and component tests cover logic and structure. Before a release, also check the real device:
- capture screenshots with
pam screenshotand compare them in review (see Debugging); - measure startup and frame pacing with
pam benchmark(see Performance); - run
pam production:certifyand the release gates.
Running tests in CI
Section titled “Running tests in CI”A CI job needs only the PAM CLI. No Android SDK or Xcode is required for pam test:
curl --proto '=https' --proto-redir '=https' --tlsv1.2 \ --connect-timeout 15 --max-time 60 --max-filesize 1048576 \ --fail --silent --show-error --location \ https://pamphp.com/install | shpam composer installpam testNext steps
Section titled “Next steps”- Debugging: logs, diagnostics and the error overlay.
- Component Runtime: every lifecycle hook you can test.
- Navigation: routes, middleware and deep links.