Repository navigation
Blade and testing
Application rules belong to the consuming application. Use context to make an explicit decision and provide a useful fallback when it is unavailable.
The discovered facade alias works in Blade when it is not claimed by another package:
@php($visitor = Cloudflare::visitor())
@if ($visitor->countryCode === 'TH')
<p>Delivery options for Thailand</p>
@else
<p>Explore our delivery destinations</p>
@endif
@if ($visitor->region)
<p>Estimated region: {{ $visitor->region }}</p>
@endifUse Blade's escaped output for free-form values. Do not render a city or region using unescaped output.
Passing a snapshot from a controller avoids reliance on a global alias in the view:
use Moztopia\Cloudflare\Facades\Cloudflare;
return view('delivery', [
'visitor' => Cloudflare::visitor(),
]);The view can then use the supplied $visitor without resolving a facade itself. See Getting started for dependency injection.
When your controller passes a visitor snapshot to the view, test the view's application decision with a known snapshot:
use Moztopia\Cloudflare\Visitor\Visitor;
$this->view('delivery', [
'visitor' => new Visitor(countryCode: 'TH', region: 'Bangkok'),
])->assertSee('Delivery options for Thailand');This assumes your application has a delivery view that uses the passed $visitor, without resolving the facade again inside the view. Use it inside your application's Laravel test lifecycle. A new Visitor() represents unknown location for a fallback test.
This verifies application behavior for the supplied snapshot. It does not establish that a header was trustworthy or that parsing was correct.
For ingress behavior, make real synthetic requests through the application with an explicit REMOTE_ADDR, controlled trusted-proxy configuration and chosen headers. Cover trusted and untrusted peers, missing/invalid fields, IPv4/IPv6, country markers and consecutive requests.
For example, when your application owns a /delivery route that reads context:
config(['cloudflare.trusted_proxies' => ['192.0.2.10']]);
$this->withServerVariables(['REMOTE_ADDR' => '192.0.2.10'])
->withHeaders(['CF-IPCountry' => 'TH'])
->get('/delivery')
->assertSee('Delivery options for Thailand');Change REMOTE_ADDR to an untrusted documentation address in another test and assert the normal fallback. The documentation address above is a synthetic test fixture, not an ingress address to deploy.
The package tests show these patterns, including active-container isolation. The example tests exercise real application routes, private caching, escaping, language support and scenario isolation.
No dedicated Cloudflare::fake() or package fixture API is shipped in tagged 0.9.1, whose runtime is unchanged from 0.9.0. Direct Laravel facade mocking with Cloudflare::shouldReceive() also fails because the resolved service class is final. Use the snapshot/request paths above; do not assume a mock can replace the typed service. The owner-approved replacement is implemented in the unreleased core work below, tracked by issue #9. The current example harness is application-owned example code, not a public SDK method.
The following API is implemented at source 905518bc31f0b8a2c60e79b722b743c61513c82a, under PR #53. It is not in tagged 0.9.1. The context service supports native Laravel mocks; Visitor and RequestContext remain final/readonly.
For an application route that reads visitor(), use Laravel’s normal test lifecycle:
use Moztopia\Cloudflare\Facades\Cloudflare;
use Moztopia\Cloudflare\Visitor\Visitor;
Cloudflare::shouldReceive('visitor')
->andReturn(new Visitor(countryCode: 'TH'));
$this->get('/delivery')->assertSee('Delivery options for Thailand');Freshly resolved typed Moztopia\Cloudflare\Cloudflare\Cloudflare injection shares the mocked facade binding. Already-held objects retain their identity. Configure every method that the consumer calls, and let Laravel’s isolated test application/Mockery lifecycle handle teardown. These are test bindings, not request-scoped production fake state.
For trust/parser behavior, the immutable fixture supplies data that your test explicitly applies:
use Moztopia\Cloudflare\RequestFixture\RequestFixture;
$fixture = RequestFixture::trusted(headers: ['CF-IPCountry' => 'TH']);
config()->set('moztopia-cloudflare.trusted_proxies', $fixture->trustedProxies);
$this->withServerVariables($fixture->server)
->withHeaders($fixture->headers)
->get('/delivery')
->assertSee('Delivery options for Thailand');trusted() defaults to a known TH country header; supplying headers replaces that complete set. missing() supplies a trusted peer without metadata, malformed() supplies invalid visitor/request values, and untrusted() supplies plausible headers from a different peer outside the supplied trust list. The server/headers/trustedProxies arrays are readonly independent copies, including PHP references and duplicate-value lists. Mutable header objects and non-string list entries are rejected, while deliberately malformed header text remains available for parser tests.
Construction never modifies the live request, configuration, facade state or network. Applying the returned proxy list is an explicit action in a test; its documentation addresses are not production ingress recommendations. Clear retained Laravel test-builder headers or use a fresh builder before a consecutive missing-header request. No package fake/reset API is introduced.
Actual consumer tests verify routed typed mocks, the real parser and Blade fallbacks, empty trust, fresh-application teardown and immutable snapshots. The example tests use all four public fixtures through the real UI route. See Unreleased core for the exact matrix and CI evidence.
Test the unknown-value path as carefully as the known-location path. Do not infer the user's desired language or currency from their country. The package leaves application locale and timezone settings unchanged.
If output varies by visitor, review its full-page and CDN caching policy as part of the application feature. Read Trusted ingress for the cache boundary.
Maintained by-dolly.
Released API reference: 0.9.0 — Cirrus. Future capabilities are marked on the Roadmap.
Repository · Project website · Apache-2.0
Maintained by-dolly.

Moztopia Cloudflare
- Home
- Getting started
- Trusted ingress
- Visitor and request context
- Diagnostics and doctor
- Blade and testing
- Example website
- Development and verification
- Branding
- SDK contract
- Roadmap
- Unreleased core work
- Release gates
- Optional credential-free additions
- V2 discovery
- Planning decisions
- Ecosystem review
- Ecosystem inventory
Maintained by-dolly.