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
Laravel Database Mock

Laravel Database Mock Laravel Package

mpyw/laravel-database-mock

View on GitHub
Deep Wiki
Context7

[Experimental] Database Mocking Library which mocks PDO underlying Laravel Connection classes

Frequently asked questions about Laravel Database Mock
How do I install and set up laravel-database-mock in my Laravel 12 project?
Run `composer require mpyw/laravel-database-mock:VERSION@alpha` to install the package. Ensure you have PHP 8.2+, Mockery 1.6.12+, and Laravel 11/12. The package replaces PDO connections at runtime, so no additional configuration is needed beyond requiring it in your test setup.
Can this package mock complex queries like raw SQL, stored procedures, or CTEs?
The package primarily supports CRUD operations and basic queries. Complex queries like raw SQL with dynamic placeholders or stored procedures may not be fully mocked. For now, focus on Eloquent and Query Builder operations, and test edge cases manually if needed.
Will this work with Laravel’s DatabaseTransactions or RefreshDatabase traits?
No, this package replaces the database connection entirely, so it’s an alternative to those traits. Use it instead of DatabaseTransactions for faster, deterministic tests, but avoid mixing them in the same test suite to prevent conflicts.
How does laravel-database-mock handle transactions, savepoints, or rollbacks?
Basic transactions are supported, but savepoints and explicit rollbacks may not be fully mocked. The package simulates transaction behavior by default, but complex transaction logic should be tested separately with real database interactions if critical.
Does this package simulate database errors like deadlocks or timeouts?
Currently, error simulation is limited. You can manually throw exceptions in your mocks, but the package doesn’t natively support simulating specific database errors like deadlocks or timeouts. This may be added in future updates.
Can I use this with PestPHP or other testing frameworks alongside PHPUnit?
Yes, but be cautious with Mockery dependencies. If your project uses PestPHP’s fake() or PHPUnit’s native mocking, conflicts may arise. Stick to one mocking approach per test suite to avoid issues, or isolate tests using this package in separate files.
What’s the performance impact of using this in CI/CD pipelines?
Mocking introduces minimal overhead, but reflection-based interception can slow down large test suites. In CI/CD, expect faster execution than real database tests, but benchmark your suite to compare against alternatives like SQLite in-memory databases.
How do I debug failed mocks or inspect mocked queries?
The package doesn’t include built-in debugging tools yet. Use Mockery’s built-in assertions or log query expectations manually. For complex issues, enable Laravel’s query logging (`DB::enableQueryLog()`) to compare expected vs. actual queries.
Are there alternatives like PestPHP’s fake() or Laravel’s DatabaseMocker that I should consider?
PestPHP’s fake() is simpler for basic mocking but lacks PDO-level control. Laravel’s DatabaseMocker (if available) may offer more stability, but this package provides deeper integration with Eloquent and Query Builder. For production-grade testing, consider SQLite in-memory databases or tools like Laravel’s `RefreshDatabase` for a balance of speed and realism.
How do I handle Carbon timestamps or time-based mocking in tests?
Use `Carbon::setTestNow()` to freeze time, as the package relies on this for timestamp consistency. Avoid mixing it with other time-mocking tools like PestPHP’s `fake()` or Carbon’s built-in testing helpers, as conflicts may arise. Reset time after tests to avoid side effects.
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.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky