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

Laravel Database Mock Laravel Package

mpyw/laravel-database-mock

View on GitHub
Deep Wiki
Context7

Laravel Database Mock Build Status Coverage Status

[!WARNING] Experimental

Database Mocking Library which mocks PDO underlying Laravel Connection classes.

Requirements

  • PHP: ^8.2
  • Laravel: ^11.0 || ^12.0 || ^13.0 || ^14.0
  • Mockery: ^1.6.12
  • 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' => 'john@example.com',
        'created_at' => '2020-01-01 00:00:00',
        'updated_at' => '2020-01-01 00:00:00',
    ]]);

$this->assertEquals([[
    'id' => 1,
    'name' => 'John',
    'email' => 'john@example.com',
    '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', 'john@example.com', '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' => 'john@example.com'])->save();
$this->assertEquals([
    'id' => 2,
    'name' => 'John',
    'email' => 'john@example.com',
    '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', ['john@example.com'])
    ->shouldFetchAllReturns([[
        'id' => 2,
        'name' => 'John',
        'email' => 'john@example.com',
        '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` = ?',
    ['john-01@example.com', '2020-01-02 00:00:00', 2]
);

$user = User::query()->where('email', 'john@example.com')->first();
$user->forceFill(['email' => 'john-01@example.com'])->save();
$this->assertEquals([
    'id' => 2,
    'name' => 'John',
    'email' => 'john-01@example.com',
    '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', ['john@example.com'])
    ->shouldFetchAllReturns([[
        'id' => 2,
        'name' => 'John',
        'email' => 'john@example.com',
        '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` = ?',
        ['john-01@example.com', '2020-01-02 00:00:00', 2]
    );

$user = User::query()->where('email', 'john@example.com')->first();
$user->forceFill(['email' => 'john-01@example.com'])->save();
$this->assertEquals([
    'id' => 2,
    'name' => 'John',
    'email' => 'john-01@example.com',
    'created_at' => '2020-01-01T00:00:00.000000Z',
    'updated_at' => '2020-01-02T00:00:00.000000Z',
], $user->toArray());
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity