Git Product home page Git Product logo

eloquent-mockery's Introduction

Eloquent Mockery

Mock your eloquent queries without the repository pattern.

Required Laravel Version Required PHP Version tests Software License

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 you will be "DB Independent".

Usage:

You have to use the MockableModel in your model.

use Imanghafoori\EloquentMockery\MockableModel;

class User extends Model
{
    use MockableModel;
    
    ...
}

Then you can:

public function test_basic()
{
    // Arrange:
    User::addFakeRow(['id' => 1, 'username' => 'faky', 'password' => '...']);
    User::addFakeRow(['id' => 1, 'username' => 'maky', 'password' => '...']]);

    // Act (This query resides in your controller):
    $user = User::where('username', 'faky')->first();   # <=== This does NOT connect to DB.

    // assert:
    $this->assert($user->id === 1);
    $this->assert($user->username === 'faky');
}

Mocking a create query:

public function test_basic()
{
    User::fake();

    // in your controller:
    $user = User::create(['username' => 'iman', 'email' => '[email protected]']);   # <=== This does NOT connect to DB.

    // Assert:
    $user = User::getCreatedModel();
    $this->assert($user->id === 1);
    $this->assert($user->username === 'iman');

    User::stopFaking();
}

You can access the changed model instances by accessing the static properties below:

$model = User::getDeletedModel();

$model = User::getSoftDeletedModel();

$model = User::getCreatedModel();

$model = User::getUpdatedModel();

The User can be any other model using the Mockable trait.

  • 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 [email protected] instead of using the issue tracker.

eloquent-mockery's People

Contributors

imanghafoori1 avatar erfantkerfan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.