Download the PHP package directorytree/dummy without Composer
On this page you can find all versions of the php package directorytree/dummy. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package dummy
Generate PHP class instances populated with dummy data using Faker
Index
- Requirements
- Installation
- Introduction
- Setup
- HasFactory Trait
- Class Factory
- Usage
- Factory States
- Factory Callbacks
- Factory Sequences
- Factory Collections
Requirements
- PHP >= 8.0
Installation
You can install the package via composer:
Introduction
Consider you have a class representing a restaurant reservation:
To make dummy instances of this class during testing, you have to manually populate it with dummy data.
This can quickly get out of hand as your class grows, and you may find yourself writing the same dummy data generation code over and over again.
Dummy provides you with a simple way to generate dummy instances of your classes using a simple API:
Setup
Dummy provides you two different ways to generate classes with dummy data.
HasFactory Trait
The HasFactory
trait is applied directly to the class you would like to generate dummy instances of.
To use the HasFactory
trait, you must implement the toFactoryInstance
and getFactoryDefinition
methods:
[!note] The
HasFactory
trait does not provide you the capability of defining state methods or callbacks. If you need this functionality, you should define a separateFactory
class instead.
Once implemented, you may call the Reservation::factory()
method to create a new dummy factory:
Class Factory
If you need more control over the dummy data generation process, you may use the Factory
class.
The Factory
class is used to generate dummy instances of a class using a separate factory class definition.
To use the Factory
class, you must extend it with your own and override the definition
and generate
methods:
Usage
Once you've defined a factory, you can generate dummy instances of your class using the make
method:
To add or override attributes in your definition, you may pass an array of attributes to the make
method:
To generate multiple instances of the class, you may use the count
method:
This will return a
Illuminate\SupportCollection
instance containing the generated classes.
Factory States
State manipulation methods allow you to define discrete modifications that can be applied to your dummy factories in any combination.
For example, your App\Factories\Reservation
factory might contain a tomorrow
state method that modifies one of its default attribute values:
Factory Callbacks
Factory callbacks are registered using the afterMaking
method and allow you to perform
additional tasks after making or creating a class. You should register these callbacks
by defining a configure
method on your factory class. This method will be
automatically called when the factory is instantiated:
Factory Sequences
Sometimes you may wish to alternate the value of a given attribute for each generated class.
You may accomplish this by defining a state transformation as a sequence
:
Factory Collections
By default, when making more than one dummy class, an instance of Illuminate\Support\Collection
will be returned.
If you need to customize the collection of classes generated by a factory, you may override the collect
method: