homedistil / eloquent-mockery
Allows you to design your tests in an independent manner.
1.0.0
2026-04-17 14:34 UTC
Requires
- php: ^7.2|^8.0
README
This is a fork of the project:
https://github.com/imanghafoori1/eloquent-mockery
$\color{red}{\text{With unlocked Laravel versions.}}$
Eloquent Mockery
Mock your eloquent queries without the repository pattern.
Why this package was invented?
- It solves the problem of "slow tests" by removing the interactions with a real database.
- It simplifies the process of writing and running tests since your tests will be "DB Independent".
Installation
You can install the package via Composer:
composer require homedistil/eloquent-mockery --dev dev-main
Usage:
First, you have to define a new connection in your config/database.php and set the driver to 'arrayDB'.
<?php return [ ... 'connections' => [ 'fake' => [ 'driver' => 'arrayDB', 'database' => '', ], ... ], ... ]
Then you can:
public function test_basic() { config()->set('database.default', 'my_test_connection'); # ::Arrange:: (Setup Sample Data) FakeDB::addRow('users', ['id' => 1, 'username' => 'faky', 'password' => '...']); FakeDB::addRow('users', ['id' => 1, 'username' => 'maky', 'password' => '...']); # ::Act:: (This query resides in your controller) $user = User::where('username', 'faky')->first(); # <=== This does NOT connect to a real DB. # ::Assert:: $this->assert($user->id === 1); $this->assert($user->username === 'faky'); }
Mocking a create query:
public function test_basic() { # In setUp: FakeDB::mockEloquentBuilder(); # ::Act:: $this->post('/create-url', ['some' => 'data' ]) # ::Assert:: $user = User::first(); $this->assertEquals('iman', $user->username); # In tearDown FakeDB::dontMockEloquentBuilder(); }
- For more examples take a look at the
testsdirectory.
License
The MIT License (MIT). Please see License File for more information.