Giter Site home page Giter Site logo

mpyw / laravel-database-mock Goto Github PK

View Code? Open in Web Editor NEW
5.0 4.0 0.0 33 KB

[Experimental] Database Mocking Library which mocks PDO underlying Laravel Connection classes

License: MIT License

PHP 100.00%
mockery php laravel mock database eloquent

laravel-database-mock's Introduction

Laravel Database Mock Build Status Coverage Status Scrutinizer Code Quality

Warning

Experimental

Database Mocking Library which mocks PDO underlying Laravel Connection classes.

Requirements

  • PHP: ^8.0
  • Laravel: ^9.0 || ^10.0
  • Mockery: ^1.3.3 || ^1.4.2
  • mpyw/mockery-pdo: alpha

Installing

composer require mpyw/laravel-database-mock:VERSION@alpha

Example

SELECT

$pdo = DBMock::mockPdo();
$pdo->shouldSelect('select * from `users`')
    ->shouldFetchAllReturns([[
        'id' => 1,
        'name' => 'John',
        'email' => '[email protected]',
        'created_at' => '2020-01-01 00:00:00',
        'updated_at' => '2020-01-01 00:00:00',
    ]]);

$this->assertEquals([[
    'id' => 1,
    'name' => 'John',
    'email' => '[email protected]',
    'created_at' => '2020-01-01T00:00:00.000000Z',
    'updated_at' => '2020-01-01T00:00:00.000000Z',
]], User::all()->toArray());

INSERT

Carbon::setTestNow('2020-01-01 00:00:00');

$pdo = DBMock::mockPdo();
$pdo->shouldInsert(
    'insert into `users` (`name`, `email`, `updated_at`, `created_at`) values (?, ?, ?, ?)',
    ['John', '[email protected]', '2020-01-01 00:00:00', '2020-01-01 00:00:00']
);
$pdo->expects('lastInsertId')->andReturn(2);

$user = new User();
$user->forceFill(['name' => 'John', 'email' => '[email protected]'])->save();
$this->assertEquals([
    'id' => 2,
    'name' => 'John',
    'email' => '[email protected]',
    'created_at' => '2020-01-01T00:00:00.000000Z',
    'updated_at' => '2020-01-01T00:00:00.000000Z',
], $user->toArray());

UPDATE

Basic

Carbon::setTestNow('2020-01-02 00:00:00');

$pdo = DBMock::mockPdo();
$pdo->shouldSelect('select * from `users` where `email` = ? limit 1', ['[email protected]'])
    ->shouldFetchAllReturns([[
        'id' => 2,
        'name' => 'John',
        'email' => '[email protected]',
        'created_at' => '2020-01-01 00:00:00',
        'updated_at' => '2020-01-01 00:00:00',
    ]]);
$pdo->shouldUpdateOne(
    'update `users` set `email` = ?, `users`.`updated_at` = ? where `id` = ?',
    ['[email protected]', '2020-01-02 00:00:00', 2]
);

$user = User::query()->where('email', '[email protected]')->first();
$user->forceFill(['email' => '[email protected]'])->save();
$this->assertEquals([
    'id' => 2,
    'name' => 'John',
    'email' => '[email protected]',
    'created_at' => '2020-01-01T00:00:00.000000Z',
    'updated_at' => '2020-01-02T00:00:00.000000Z',
], $user->toArray());

Using Read Replica

Carbon::setTestNow('2020-01-02 00:00:00');

$pdos = DBMock::mockEachPdo();
$pdos->reader()
    ->shouldSelect('select * from `users` where `email` = ? limit 1', ['[email protected]'])
    ->shouldFetchAllReturns([[
        'id' => 2,
        'name' => 'John',
        'email' => '[email protected]',
        'created_at' => '2020-01-01 00:00:00',
        'updated_at' => '2020-01-01 00:00:00',
    ]]);
$pdos->writer()
    ->shouldUpdateOne(
        'update `users` set `email` = ?, `users`.`updated_at` = ? where `id` = ?',
        ['[email protected]', '2020-01-02 00:00:00', 2]
    );

$user = User::query()->where('email', '[email protected]')->first();
$user->forceFill(['email' => '[email protected]'])->save();
$this->assertEquals([
    'id' => 2,
    'name' => 'John',
    'email' => '[email protected]',
    'created_at' => '2020-01-01T00:00:00.000000Z',
    'updated_at' => '2020-01-02T00:00:00.000000Z',
], $user->toArray());

laravel-database-mock's People

Contributors

hexium310 avatar mpyw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  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.