Weave 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

Mocker Laravel Package

reecem/mocker

A lightweight Laravel-friendly mocking helper for PHP tests. Quickly define and swap mocks, set expectations, and control return values to simplify unit and feature testing. Designed to reduce boilerplate and keep test setup clean and readable.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer: composer require --dev reecem/mocker
  2. In tests or seeders, instantiate the Mocker class (typically namespaced as Reecem\Mocker\Mocker)
  3. Begin by mocking simple values:
    $mocker = new Mocker();
    $name = $mocker->string(); // e.g., 'John Doe'
    $age = $mocker->integer(18, 80); // e.g., 42
    
  4. For Laravel: Use in DatabaseSeeder, feature tests, or tinker to generate realistic test data on-the-fly.

Implementation Patterns

  • Object hydration: Mock complex objects with chained generators:
    $user = new User();
    $user->name = $mocker->string();
    $user->email = $mocker->email();
    $user->created_at = $mocker->dateTime(); // Carbon instance
    
  • Seeded reproducibility: Set a seed for deterministic mocks in tests:
    $mocker->seed(12345); // Ensures same values across runs
    
  • Laravel integration: Register as a shared instance in TestCase::setUp() or via Laravel’s container (e.g., bind Mocker::class as singleton) to reuse across test classes.
  • Custom generators: Extend or override built-ins with domain-specific logic (e.g., ->companyName()), often via a custom wrapper or factory class.

Gotchas and Tips

  • Randomness vs. determinism: Without seeding, mocks differ per run — add $mocker->seed() explicitly in tests where consistency matters.
  • Laravel time helpers: Its dateTime() returns Carbon instances (via faker), but other generators may return primitives — verify type compatibility before asserting or persisting.
  • Package maturity: Low stars (7) and last release in 2023 indicate low activity — audit the codebase before adopting in critical flows; consider for internal tooling or non-production-critical test scaffolds only.
  • No out-of-the-box Laravel model support: Unlike factory() workflows, it won’t handle Eloquent relationships automatically — pair with factories for full ORM seeding.
  • Error-prone chaining: Nested object mocks can become messy; prefer extracting logic into small, reusable generator methods (e.g., createUserMock()) for maintainability.
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.
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc
animalyoung/laravel-influxdb
onix-solutions/sms-telera
lonely-lights/prosetta
oxygen/data
oxygen/core
jengo/base
xiloryx/log-collector
a21ns1g4ts/filament-stripe
la-php/laravel-admin
artisan-build/community-prompts
vleroy/smart
ayles-software/laravel-australian-public-holidays
cboxdk/laravel-id-spatie
cboxdk/laravel-id-billing
cboxdk/laravel-id-billing-bridge
simple-as-fuck/php-performance-log