Skip to content

TomEasterbrook/livewire-fakeable

Repository files navigation

Livewire Fakeable

GitHub Workflow Status Total Downloads Latest Version License


Livewire 4. Fill empty component state with Faker while you build — after mount, only on your machine, never overwriting values you already set.

Livewire Fakeable is a focused Laravel package with a simple idea: declare fake data next to your Livewire properties, and let a component hook apply it when it is safe. No seeding scripts scattered across mount() methods, and no guessing whether you are looking at real or dummy data.

Installation

composer require tomeasterbrook/livewire-fakeable

The service provider is discovered automatically. Publish the config if you want to change locale, hosts, or the on-page indicator:

php artisan vendor:publish --tag="livewire-fakeable-config"

Requires PHP 8.1+, Laravel 10–13, and Livewire 4 (livewire/livewire:^4.0 is required by this package).

Usage

Explicit formatters

Annotate public properties with #[Fakeable] and a Faker formatter name. Livewire Fakeable runs after mount and only fills properties that are still empty — null, '', [], or arrays where every leaf value is one of those (e.g. placeholder structures like [['name' => '', 'phone' => '']]).

use Livewire\Component;
use TomEasterbrook\LivewireFakeable\Attributes\Fakeable;

class EditProfilePage extends Component
{
    #[Fakeable('name')]
    public string $name = '';

    #[Fakeable('safeEmail')]
    public string $email = '';

    #[Fakeable('paragraph')]
    public string $bio = '';
}

Pass arguments through to Faker using the named formatterArguments parameter, or fix a seed for stable reloads (screenshots, demos):

// Preferred: named formatterArguments
#[Fakeable('sentence', formatterArguments: [12])]
public string $title = '';

#[Fakeable('boolean', formatterArguments: [100])]
public bool $alwaysTrue = false;

#[Fakeable('randomElement', formatterArguments: [['draft', 'published', 'archived']])]
public string $status = '';

// Alternative: variadic named args still work
#[Fakeable('sentence', nbWords: 3)]
public string $subtitle = '';

#[Fakeable('name', seed: 42)]
public string $name = '';

Bare #[Fakeable] — automatic inference

Use the attribute without a formatter and Livewire Fakeable will infer one automatically. It checks in order:

  1. Enum type — picks a random case from the property's enum type.
  2. Property name — maps common names to Faker formatters (see table below).
  3. PHP type — falls back to the property's type (stringword, intrandomNumber, floatrandomFloat, boolboolean).
#[Fakeable]
public string $email = '';       // inferred → safeEmail

#[Fakeable]
public int $quantity = 0;        // inferred → randomNumber

#[Fakeable]
public OrderStatus $status;      // inferred → random enum case
Property name → formatter map
Property name(s) Formatter
name, fullName, full_name name
firstName, first_name firstName
lastName, last_name lastName
email, emailAddress, email_address safeEmail
phone, phoneNumber, phone_number phoneNumber
address address
street, streetAddress, street_address streetAddress
city city
state state
country country
postcode, zipCode, zip_code postcode
company, companyName, company_name company
title sentence
description, bio paragraph
summary sentence
url, website url
username, user_name userName

Enums

PHP enums (unit, string-backed, and int-backed) are supported out of the box. A bare #[Fakeable] attribute detects the enum type and picks a random case. Seeds are supported for deterministic selection.

enum OrderStatus: string
{
    case Pending = 'pending';
    case Shipped = 'shipped';
    case Delivered = 'delivered';
}

#[Fakeable]
public OrderStatus $status;

#[Fakeable(seed: 42)]
public OrderStatus $lockedStatus;

Array shapes

Generate arrays of fake data by passing an array shape — keys are output keys, values are Faker formatter names. Use count to control the number of rows:

#[Fakeable(['name' => 'name', 'email' => 'safeEmail'], count: 3)]
public array $users = [];

This produces an array like:

[
    ['name' => 'Jane Doe', 'email' => 'jane@example.com'],
    ['name' => 'John Smith', 'email' => 'john@example.com'],
    ['name' => 'Alice Brown', 'email' => 'alice@example.com'],
]

Arrays pre-filled with empty placeholder structures are also replaced — as long as every leaf value is null, '', or []:

#[Fakeable(['name' => 'name', 'phone' => 'phoneNumber', 'email' => 'safeEmail'], count: 2)]
public array $references = [
    ['name' => '', 'phone' => '', 'email' => ''],
    ['name' => '', 'phone' => '', 'email' => ''],
];

State classes

For a whole component, use a state class that returns an array keyed by property name, then reference it on the class:

use Faker\Generator;

class ProfileFormState
{
    public function __invoke(Generator $faker): array
    {
        return [
            'name' => $faker->name(),
            'email' => $faker->safeEmail(),
        ];
    }
}
use App\FakerStates\ProfileFormState;
use Livewire\Component;
use TomEasterbrook\LivewireFakeable\Attributes\Fakeable;

#[Fakeable(ProfileFormState::class)]
class EditProfilePage extends Component
{
    public string $name = '';
    public string $email = '';
}

You can mix class-level and property-level #[Fakeable] — property-level attributes take precedence.

Livewire Form objects

Livewire Fakeable automatically resolves #[Fakeable] attributes on Livewire Form properties too. Both property-level and class-level attributes work on Form classes:

use Livewire\Form;
use TomEasterbrook\LivewireFakeable\Attributes\Fakeable;

class ProfileForm extends Form
{
    #[Fakeable('name')]
    public string $name = '';

    #[Fakeable('safeEmail')]
    public string $email = '';
}
class EditProfilePage extends Component
{
    public ProfileForm $form;
}

HasFakeable trait

You can also call fakeable() from mount() with the HasFakeable trait for programmatic control. The trait respects the same guard conditions.

use App\FakerStates\ProfileFormState;
use Livewire\Component;
use TomEasterbrook\LivewireFakeable\Concerns\HasFakeable;

class EditProfilePage extends Component
{
    use HasFakeable;

    public string $name = '';
    public string $email = '';

    public function mount(): void
    {
        $this->fakeable(ProfileFormState::class);
    }
}

Locale & indicator

Locale — set locale in config/fakeable.php (default en_US). Any Faker-supported locale works.

Indicator — when faking is active, a collapsible banner is injected before </body> so you do not confuse local data with production. Its collapsed state persists in localStorage. Disable with show_indicator in the config.

Safety

Livewire Fakeable is for local development only. Faking runs only when all of the following are true:

  1. enabled is true in config
  2. The app environment is local
  3. The request host matches at least one allowed_hosts pattern (case-insensitive fnmatch globs — *.test matches myapp.test and sub.myapp.test)
  4. Faker\Generator exists as a class

Because APP_ENV=testing is not local, your test suite is unaffected. If any condition fails, Livewire Fakeable does nothing — no properties are touched, no banner is injected.

Configuration

// config/fakeable.php
return [
    'enabled' => env('FAKEABLE_ENABLED', true),

    'allowed_hosts' => [
        '*.test',
        '*.dev',
        'localhost',
    ],

    'locale' => 'en_US',

    'show_indicator' => true,
];

Testing

composer test

Changelog

Please see CHANGELOG.

Contributing

Please see CONTRIBUTING.

Security

Please see our security policy.

Credits

License

Livewire Fakeable is open-sourced software licensed under the MIT license.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors