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

Eloquent Mockery Laravel Package

imanghafoori/eloquent-mockery

View on GitHub
Deep Wiki
Context7

Eloquent Mockery

Mock your eloquent queries without the repository pattern.

Required Laravel Version tests Imports

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.

:raising_hand: Contributing

If you find an issue or have a better way to do something, feel free to open an issue, or a pull request.

:exclamation: Security

If you discover any security-related issues, please email imanghafoori1@gmail.com instead of using the issue tracker.

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.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle