Testing Dynamic Laravel Forms with Pest 4: A Production Matrix
Testing Dynamic Laravel Forms with Pest 4: A Production Matrix
A dynamic form multiplies states: schema revisions, conditional paths, locales, tenants, files, drafts, scheduled publishing, and anonymous browser behavior. A handful of “page loads” tests cannot cover the boundaries that fail in production.
Use layers. Keep schema and policy rules fast, exercise Laravel/Livewire integration with focused feature tests, and reserve Pest 4 browser tests for behavior only a real browser can prove.
Layer 1: schema contract tests
Treat stored definitions as untrusted configuration. For every supported field and rule, test valid serialization plus malformed cases:
- unknown field/rule identifiers;
- wrong parameter types and out-of-range values;
- excessive field/nesting/option counts;
- duplicate or changing immutable field codes;
- invalid conditional references and cycles;
- missing translations;
- schema-format migrations.
Property-style datasets are useful here. Generate combinations inside bounded fixtures, but keep named regression examples for every production bug.
Layer 2: server validation and authorization
Feature tests should submit directly without relying on the browser UI. Prove:
- unknown payload keys do not persist;
- hidden/conditional values follow exclusion or prohibition policy;
- nested repeater shapes and collection limits hold;
- Eloquent option IDs obey active, tenant, and ownership scopes;
- inactive/unpublished forms reject submissions;
- a submission stores the exact form revision;
- rate limits and idempotency boundaries return documented responses.
Use factories and explicit states for tenants, published revisions, expired drafts, and held submissions. Prefer specific assertions such as assertForbidden() and assertUnprocessable() where the response contract supports them.
Layer 3: Livewire component tests
Livewire tests cover server-side component state efficiently:
Livewire::test(FormRenderer::class, ['form' => $form])
->set('data.contact_email', 'not-an-email')
->call('submit')
->assertHasErrors(['data.contact_email' => 'email']);
Add cases for dependent fields, wizard navigation, loading actions, draft autosave, locale persistence, file ownership, and stale revision/component state. Treat every public property and action parameter as untrusted; the Livewire security guide makes that boundary explicit.
Layer 4: database concurrency tests
The failures with the highest cost often need two requests:
- same idempotency key racing to create a submission;
- two publishers racing to activate a form revision;
- two tabs autosaving one draft version;
- last-seat/capacity contention;
- duplicate analytics start events.
Application “check then insert” tests do not prove concurrency safety. Assert the database unique constraint or lock behavior and verify one durable outcome plus a clear loser response.
Run these cases on the same database engine used in production when semantics differ. SQLite can be excellent for ordinary tests but should not be the only evidence for PostgreSQL/MySQL locking behavior.
Layer 5: file and integration boundaries
Fake storage and notifications for most happy paths, then add targeted integration coverage for:
- MIME/extension mismatch and oversized/multiple uploads;
- generated private paths and authorized downloads;
- abandoned draft cleanup;
- after-commit notification dispatch;
- HMAC webhook test vectors;
- retry classification and receiver deduplication;
- SSRF destination rejection;
- log redaction.
One end-to-end storage test against the real configured S3-compatible service can catch assumptions that fakes cannot, but keep it separate from the fast default suite.
Layer 6: Pest browser tests
Use a real browser for the questions PHP assertions cannot answer:
- scripts and styles load without console errors;
- keyboard and focus behavior across validation/wizard steps;
- conditional fields actually appear/disappear;
- double-click/loading UI behaves;
- uploads show progress/recovery;
- iframe resize and completion messaging;
- back/forward cache and navigation;
- mobile viewport and RTL layout;
- successful submit is announced and navigates correctly.
Pest 4 browser tests live in tests/Browser/ and can use application factories and authentication. The Pest browser-testing documentation describes the current API.
Test a small representative form on multiple browsers or devices when the feature depends on third-party cookies, iframe policy, uploads, or native controls. Do not multiply every server-validation case across browsers.
Accessibility belongs in behavior tests
Automated checks catch missing labels, invalid ARIA, contrast, and some focus problems. Add manual/behavior assertions for error-summary links, focus after step failure, live-region announcements, keyboard ordering, field descriptions, language, and RTL direction. Our WCAG form guide supplies the checklist.
Scheduled publication needs time tests
Freeze time, create a future published revision/post, assert it is hidden, travel across the boundary, run the scheduled visibility/cache command, and assert it becomes visible exactly once. Also test timezone boundaries and stale response caches.
The scheduler should use overlap/single-server controls where required, but the command itself must remain idempotent.
Organize the suite by risk
| Suite | Runs | Purpose |
|---|---|---|
| Unit/schema | Every edit | Compiler and data invariants |
| Feature/Livewire | Every PR | Server behavior and policies |
| Browser smoke | Every PR or merge | Critical visitor journeys |
| Multi-browser/integration | Nightly/release | Environment-specific behavior |
| Performance/security | Release or scheduled | Regression budgets and hostile cases |
Keep fixtures readable: one minimal contact form, one complex conditional wizard, one multilingual form, and a pair of tenants often cover more than dozens of opaque factories.
Tests become valuable when each proves a boundary: stored configuration, server authority, concurrency, browser behavior, or external side effect. That matrix lets a dynamic builder evolve without asking production users to discover the missing combinations.