imanghafoori / eloquent-mockery
Allows you to design your tests in an independent manner.
Installs: 192 373
Dependents: 1
Suggesters: 0
Security: 0
Stars: 129
Watchers: 3
Forks: 17
Open Issues: 3
Requires
- php: ^7.2|^8.0
- illuminate/database: 5.*|6.*|7.*|8.*|9.*
- illuminate/events: 5.*|6.*|7.*|8.*|9.*
Requires (Dev)
- phpunit/phpunit: ^7.5.20|^8.5.28|^9.5
- symfony/var-dumper: 3.*|4.*|5.*
- dev-main
- v1.0.40
- v0.1.48
- v0.1.47
- v0.1.46
- v0.1.45
- v0.1.44
- v0.1.43
- v0.1.42
- v0.1.41
- v0.1.40
- v0.1.39
- v0.1.38
- v0.1.37
- v0.1.36
- v0.1.35
- v0.1.34
- v0.1.33
- v0.1.32
- v0.1.31
- v0.1.30
- v0.1.29
- v0.1.28
- v0.1.27
- v0.1.26
- v0.1.25
- v0.1.24
- v0.1.23
- v0.1.22
- v0.1.21
- v0.1.20
- v0.1.19
- v0.1.18
- v0.1.17
- v0.1.16
- v0.1.15
- v0.1.14
- v0.1.13
- v0.1.12
- v0.1.11
- v0.1.10
- v0.1.9
- v0.1.8
- v0.1.7
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- dev-fakeDb
This package is auto-updated.
Last update: 2024-10-25 16:10:07 UTC
README
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 imanghafoori/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' => [ 'my_test_connection' => [ '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
tests
directory.
License
The MIT License (MIT). Please see License File for more information.
🙋 Contributing
If you find an issue or have a better way to do something, feel free to open an issue, or a pull request.
❗ Security
If you discover any security-related issues, please email imanghafoori1@gmail.com
instead of using the issue tracker.