VOOZH about

URL: https://deepwiki.com/hypervel/testbench/7.2-model-factories

⇱ Model Factories | hypervel/testbench | DeepWiki


Loading...
Last indexed: 7 February 2026 (93289f)
Menu

Model Factories

Model factories provide a mechanism for generating test data by defining reusable attribute templates for models. The Hypervel Testbench includes a factory system that integrates with the Faker library to create realistic model instances for testing purposes.

This document covers factory definition syntax, attribute generation patterns, and integration with the Faker library. For information about the User model that factories instantiate, see Authentication Models. For database migration setup, see Database Migrations.

Factory File Structure

Model factories are defined in PHP files located in the workbench/database/factories/ directory. Each factory file uses a closure-based syntax where the $factory variable is used to define model templates.

The factory definition consists of:

  • A model class reference (e.g., User::class)
  • A closure that receives a Faker\Generator instance
  • A return array mapping attribute names to generated values

Sources: workbench/database/factories/UserFactory.php1-18

Factory Definition Syntax


Diagram: Factory Definition Structure

The UserFactory demonstrates the standard factory pattern:


The $factory variable is injected by the framework and provides the define() method workbench/database/factories/UserFactory.php10 The first parameter specifies which model class the factory creates, and the second parameter is a closure that generates attribute values.

Sources: workbench/database/factories/UserFactory.php10-17

Faker Integration

The factory closure receives a Faker\Generator instance that provides methods for generating realistic fake data. The Faker library supports generating various data types including names, emails, addresses, dates, and more.


Diagram: Faker Data Generation Flow

The UserFactory uses Faker methods with uniqueness constraints:

AttributeFaker MethodDescription
name$faker->unique()->name()Generates a unique person name
email$faker->unique()->safeEmail()Generates a unique safe email address

The unique() modifier ensures that each generated value is distinct within the test suite, preventing duplicate constraint violations workbench/database/factories/UserFactory.php12-13

Sources: workbench/database/factories/UserFactory.php12-13

Attribute Generation Patterns

Static and Dynamic Values

Factory attributes can be either statically defined or dynamically generated:


Diagram: Attribute Generation Patterns

The UserFactory demonstrates three patterns:

  1. Dynamic Faker Attributes: The name and email fields use Faker methods to generate different values for each factory invocation workbench/database/factories/UserFactory.php12-13

  2. Computed Timestamps: The email_verified_at field uses Carbon::now() to generate the current timestamp at the moment of factory execution workbench/database/factories/UserFactory.php14

  3. Static Values: The password field contains a pre-hashed bcrypt string representing "password", which remains constant across all factory invocations workbench/database/factories/UserFactory.php15

Sources: workbench/database/factories/UserFactory.php12-15

Model Attribute Mapping

Factory attributes must align with the model's fillable attributes to enable mass assignment during model instantiation.


Diagram: Factory-Model Attribute Mapping

The User model defines a $fillable property that lists which attributes can be mass-assigned workbench/app/Models/User.php14-19 All four attributes generated by the UserFactory correspond exactly to the fillable attributes on the User model, ensuring that factory-generated data can be properly assigned during model creation.

Factory AttributeModel FillableTypePurpose
namenamestringUser's full name
emailemailstringUser's email address
email_verified_atemail_verified_attimestampEmail verification timestamp
passwordpasswordstringHashed password

Sources: workbench/database/factories/UserFactory.php12-15 workbench/app/Models/User.php14-19

Dependencies and Imports

Factory files require specific imports to access necessary functionality:

ImportPurposeUsage
Carbon\CarbonDate/time manipulationGenerating timestamp values
Faker\GeneratorFake data generationType hint for closure parameter
Workbench\App\Models\UserModel class referenceFactory definition target

The factory leverages these dependencies to generate realistic test data workbench/database/factories/UserFactory.php5-7 The Carbon library provides the now() method for current timestamps, while Faker\Generator provides the type hint for the closure parameter, enabling IDE autocomplete and static analysis.

Sources: workbench/database/factories/UserFactory.php5-7

Password Hashing

The factory includes a pre-hashed password value: $2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi, which is a bcrypt hash of the string "password" workbench/database/factories/UserFactory.php15

This approach provides several benefits for testing:

  • Performance: Avoids expensive bcrypt hashing operations during test data generation
  • Consistency: All factory-generated users share the same known password for authentication testing
  • Simplicity: Tests can use the plaintext "password" when simulating user login

The hash uses bcrypt with a cost factor of 10, which is the standard default for Laravel/Hypervel applications.

Sources: workbench/database/factories/UserFactory.php15