Download the PHP package joshbonnick/filament-faker without Composer
On this page you can find all versions of the php package joshbonnick/filament-faker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download joshbonnick/filament-faker
More information about joshbonnick/filament-faker
Files in joshbonnick/filament-faker
Package filament-faker
Short Description Generate fake blocks content for Filament forms, blocks and components.
License MIT
Homepage https://github.com/joshbonnick/filament-faker
Informations about the package filament-faker
Filament Faker
Filament Testing Utility Library
Filament Faker is a utility library designed to streamline writing testing that use Filament resources, forms, blocks, and components content. This library assists in automatically generating mock data for your tests within the Filament ecosystem.
Features and Usage Highlights
- Data Generation: Automatically generate test data for Filament resources, forms, blocks, and components.
- Factory Support: Utilize factory definitions for precise and accurate data generation.
- Mutations: Modify specific component values to suit your testing scenarios.
- Configurable: Control the behavior of data generation using configuration options.
- Seamless Integration: Easily integrate the library into your Filament-based projects.
Contents
- Filament Utility Library
- Requirements
- Installation
- Usage
- Usage In Tests
- Faking Custom & Plugin Components
- Mutating Generated Data
- Generate Data Using Factory Definitions
- Selecting Definitions
- IDE Support
- Changelog
- Credits
- License
Requirements
- Filament v3 or higher.
- PHP 8.1 or higher.
Installation
You can install the package via composer:
You can publish the config file with:
Usage
Call the fake
method on a resource to retrieve an array of fields filled with fake data.
Generated Data Example
Usage In Tests
You can use the faked data in your tests.
Faking Custom & Plugin Components
If you have added a plugin such
as Spatie Media Library,
which adds the SpatieMediaLibraryFileUpload
component you can register it in config/filament-faker.php
like so:
If you do not register extra components, the default
callback will be used which returns the result of
fake()->sentence()
.
You may also override the default faker method attached to built in components by adding them to the config.
Mutating Generated Data
If you need to control a specific components value, you can chain mutateFake
onto the fake builder. If this method
returns
null
for a component then it will be ignored and filled by other methods.
Mutate Method As Method
Alternatively you can add a mutateFake
method to your Form, Block or Resource.
The closure passed to mutateFake
supports dependency injection, you just need to type
hint \Filament\Forms\Components\Field
or the specific component type (e.g. \Filament\Forms\Components\TextInput
) to get an instance of the component.
Disabling Generations From Component Names
By default, component names are used to map to a Faker method for more accurate data. There are several ways to disable this behavior:
Set use_component_names_for_fake
to false
in config/filament-faker.php
which will disable the behavior for
the entire package as default.
You can chain shouldFakeUsingComponentName
on the Faker API to disable the feature per test.
Generate Data Using Factory Definitions
If you need increased accuracy for a specific test then you can enable the usage of Factories. When the use of factories is enabled the generated data will be generated using definitions from the factory provided.
If no factory is provided the package will attempt to resolve one from the given resource, form, component or block.
As this feature executes Factory::makeOne
under the hood, I recommend only using it in tests where the accuracy of the
faked
data is of significant importance.
If you need to specify a factory you can pass a class-string
or instance of a Factory
to the withFactory()
method.
Only Resources
can resolve a factory automatically, if you wish to use a factory with a Block or Component, you must
provide
either the factory to withFactory
or provide the model to the Component
, Form
or Block
.
Selecting Definitions
If you want to select only a specific set of definitions from your factory you can pass an array
as to
the withFactory()
method
which lists the definitions you want you use.
Generate Data for Specific Fields
If you only need a specific field or fields, you can specify them with the onlyFields
method on Resource and Form
fakers.
IDE Support
As this package adds methods using Laravel's Macroable
trait, your IDE will not find the methods on its own. To fix
this you will need
to use the ide-helper package.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
- Josh
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of filament-faker with dependencies
filament/filament Version ^3.2
illuminate/contracts Version ^10.0|^11.0
illuminate/support Version ^10.0|^11.0
livewire/livewire Version ^3.0
spatie/laravel-package-tools Version ^1.14.0