Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Dummy Laravel Package

directorytree/dummy

directorytree/dummy is a Laravel/PHP package providing a lightweight dummy/test utility for generating placeholder data and fixtures. Useful for local development, demos, and automated tests where realistic sample content is needed quickly and consistently.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require directorytree/dummy
    
    • No Laravel dependency required (since v1.1.0), but orchestra/testbench is needed for Laravel-specific testing.
  2. Basic Instantiation:

    use DirectoryTree\Dummy\Dummy;
    
    $dummy = new Dummy();
    
  3. First Use Case: Generate a Single Fake Model

    $user = $dummy->make('App\Models\User', [
        'name' => 'Test User',
        'email' => 'test@example.com'
    ]);
    
    • Replace App\Models\User with your Eloquent model or class name.
  4. First Use Case: Generate Multiple Fake Models

    $users = $dummy->times(5)->make('App\Models\User');
    
  5. First Use Case: Use with Laravel’s HasFactory (if applicable)

    use DirectoryTree\Dummy\Concerns\HasFactory;
    
    class User extends Model
    {
        use HasFactory;
    }
    
    // In test:
    $user = User::factory()->create();
    

Where to Look First

  • Package Docs: GitHub Repository (limited but clear).
  • Release Notes: Focus on v1.3.0+ for HasFactory and Arr helper improvements.
  • Examples: Check the tests/ directory in the repo for usage patterns.

Implementation Patterns

Core Workflows

  1. Basic Data Generation

    $dummy = new Dummy();
    $post = $dummy->make('App\Models\Post', [
        'title' => 'Dummy Post',
        'content' => 'This is a test post.'
    ]);
    
  2. Bulk Data Generation

    $posts = $dummy->times(10)->make('App\Models\Post');
    
  3. Dynamic Attributes with filled()

    $user = $dummy->make('App\Models\User');
    $user->filled('email', fn() => 'dynamic@example.com');
    
  4. Integration with Laravel’s HasFactory

    // In your model:
    use DirectoryTree\Dummy\Concerns\HasFactory;
    
    class User extends Model
    {
        use HasFactory;
    }
    
    // In test:
    $user = User::factory()->create();
    
  5. Stateful Factories (v1.3.0+)

    User::factory()
        ->state(['role' => 'admin'])
        ->create();
    

Laravel-Specific Patterns

  1. Using with Eloquent Relationships

    $user = User::factory()->create();
    $user->posts()->saveMany(
        Post::factory()->count(3)->create()
    );
    
  2. Customizing Factories via HasFactory

    // In model:
    public function defineDummy()
    {
        return [
            'name' => $this->faker->name,
            'email' => $this->faker->unique()->safeEmail,
        ];
    }
    
  3. PestPHP Integration

    use DirectoryTree\Dummy\Dummy;
    
    it('creates a user', function () {
        $dummy = new Dummy();
        $user = $dummy->make('App\Models\User');
        expect($user->email)->toBeString();
    });
    

Non-Laravel Patterns

  1. Generating Plain PHP Objects

    $dummy = new Dummy();
    $data = $dummy->make(stdClass::class, [
        'id' => 1,
        'name' => 'Test Object'
    ]);
    
  2. Using with Collections

    $collection = collect($dummy->times(5)->make(stdClass::class));
    
  3. Custom Faker Providers

    $dummy->faker()->addProvider(new CustomFakerProvider());
    

Gotchas and Tips

Pitfalls

  1. Non-Stringable Objects in filled() (v2.0.1 Fix)

    • Issue: filled() throws an exception if the attribute holds a non-Stringable object (e.g., DateTime, Carbon).
    • Fix: Ensure attributes passed to filled() are compatible or cast them:
      $user->filled('created_at', fn() => now()->format('Y-m-d'));
      
  2. Late Static Binding in HasFactory (v1.6.1 Fix)

    • Issue: Static closures in HasFactory may fail if not bound correctly.
    • Fix: Use static:: or ensure proper closure binding:
      public function defineDummy()
      {
          return [
              'name' => static::faker()->name,
          ];
      }
      
  3. Laravel-Specific Assumptions

    • Issue: Methods like Arr::get() assume Laravel’s Arr helper (v1.3.1+).
    • Fix: For non-Laravel projects, replace with array_key_first() or similar:
      // Non-Laravel alternative:
      $value = array_key_first($array) ?: null;
      
  4. Dynamic States Overhead

    • Issue: Complex stateful factories can slow down tests if not optimized.
    • Fix: Cache factory definitions or use simpler states where possible.
  5. No Built-in Database Seeding

    • Issue: The package generates objects in memory; you must manually persist them (e.g., create() for Eloquent).
    • Fix: Chain with Eloquent methods:
      $user = User::factory()->create(); // Persists to DB
      

Debugging Tips

  1. Inspect Generated Data

    $dummy = new Dummy();
    $data = $dummy->make('App\Models\User');
    dd($data->toArray()); // Debug output
    
  2. Check Faker Providers

    • Ensure custom providers are registered:
      $dummy->faker()->addProvider(new CustomProvider());
      
  3. Validate HasFactory Definitions

    • If factories behave unexpectedly, verify the defineDummy() method:
      public function defineDummy()
      {
          return [
              'name' => $this->faker->name,
              // Ensure all required fields are defined
          ];
      }
      

Extension Points

  1. Custom Faker Providers

    use Faker\Provider\Base as FakerProvider;
    
    class CustomProvider extends FakerProvider
    {
        public function customField()
        {
            return 'custom_value';
        }
    }
    
    $dummy->faker()->addProvider(new CustomProvider());
    
  2. Override Default Attributes

    $dummy->make('App\Models\User', [
        'name' => 'Override Name',
        'email' => 'override@example.com'
    ]);
    
  3. Dynamic Factory States

    User::factory()
        ->state(['role' => 'admin'])
        ->state(fn(array $attributes) => [
            'is_active' => $attributes['role'] === 'admin' ? true : false,
        ])
        ->create();
    
  4. Integration with Laravel’s Fake

    use Illuminate\Foundation\Testing\WithFaker;
    
    class UserFactoryTest extends TestCase
    {
        use WithFaker;
    
        public function testFactory()
        {
            $user = User::factory()->create();
            $this->assertEquals('test@example.com', $user->email);
        }
    }
    

Configuration Quirks

  1. Laravel 13+ Compatibility

    • The package explicitly supports Laravel 13 (v1.4.0+), but some features may require adjustments for newer Laravel versions.
  2. Generic Types (v1.5.1)

    • Useful for type-hinted properties:
      $dummy->make(stdClass::class, [
          'id' => 1,
          'name' => 'Test',
      ]);
      
  3. Factory Method Parity (v1.5.0)

    • Mimics Laravel’s factory() methods for consistency:
      $user = User::factory()->create(); // Works if using HasFactory
      

Performance Tips

  1. Avoid Overusing Dynamic States

    • Complex state logic can slow down bulk operations. Simplify where possible.
  2. Cache Factory Definitions

    • For frequently used factories, define them in a trait or base class to avoid re-parsing.
  3. Use times() Efficiently

    • Generate data in batches to reduce memory usage:
      $dummy->times(100)->make('App\Models\User'); // Better than looping manually
      
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata