Back to blog

Testing Dynamic Laravel Forms with Pest 4: A Production Matrix

Manuk Minasyan · · 5 min read
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:

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:

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:

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:

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:

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

SuiteRunsPurpose
Unit/schemaEvery editCompiler and data invariants
Feature/LivewireEvery PRServer behavior and policies
Browser smokeEvery PR or mergeCritical visitor journeys
Multi-browser/integrationNightly/releaseEnvironment-specific behavior
Performance/securityRelease or scheduledRegression 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.

Related posts