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

Laravel Database Mock Laravel Package

mpyw/laravel-database-mock

Experimental Laravel package that mocks underlying PDO connections with Mockery, letting you stub SELECT, INSERT, and UPDATE queries and results for fast, database-free tests. Requires PHP 8.2+ and Laravel 11+.

View on GitHub
Deep Wiki
Context7

Laravel Database Mock is an experimental library for mocking the PDO layer beneath Laravel database connections. It lets you define expected SQL queries and bound parameters, then return controlled results—ideal for testing Eloquent models and query builder logic without a real database.

  • Mock SELECT, INSERT, and UPDATE statements with shouldSelect(), shouldInsert(), and shouldUpdateOne()
  • Assert exact SQL and bindings while returning fake fetch results
  • Chain PDO expectations such as lastInsertId()
  • Built on Mockery and mpyw/mockery-pdo
  • Supports PHP 8.2+ and Laravel 11–14
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.
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core
nodisrupt/laravel-agent
sierratecnologia/media-manager