PHPackages                             mpyw/laravel-database-mock - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. mpyw/laravel-database-mock

ActiveLibrary[Testing &amp; Quality](/categories/testing)

mpyw/laravel-database-mock
==========================

Database Mocking Library which mocks PDO underlying Laravel Connection classes

v0.0.1-alpha6(1y ago)69.8k↓44.3%2MITPHPPHP ^8.2CI passing

Since Aug 27Pushed 5mo ago2 watchersCompare

[ Source](https://github.com/mpyw/laravel-database-mock)[ Packagist](https://packagist.org/packages/mpyw/laravel-database-mock)[ RSS](/packages/mpyw-laravel-database-mock/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (7)Versions (7)Used By (2)

Laravel Database Mock [![Build Status](https://github.com/mpyw/laravel-database-mock/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/mpyw/laravel-database-mock/actions) [![Coverage Status](https://camo.githubusercontent.com/3f5895c79e142a9b3cca40a0e510c1fe84171153c4d68ce8d32cb2f8b0d53c6e/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d7079772f6c61726176656c2d64617461626173652d6d6f636b2f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/mpyw/laravel-database-mock?branch=master)
=======================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#laravel-database-mock--)

Warning

**Experimental**

Database Mocking Library which mocks `PDO` underlying Laravel Connection classes.

Requirements
------------

[](#requirements)

- PHP: `^8.2`
- Laravel: `^11.0 || ^12.0`
- Mockery: `^1.6.12`
- [mpyw/mockery-pdo](https://github.com/mpyw/mockery-pdo): `alpha`

Installing
----------

[](#installing)

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

Example
-------

[](#example)

### SELECT

[](#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

[](#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

[](#update)

#### Basic

[](#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

[](#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());
```

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance60

Regular maintenance activity

Popularity30

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~330 days

Recently: every ~403 days

Total

6

Last Release

440d ago

PHP version history (4 changes)v0.0.1-alpha1PHP ^7.1

v0.0.1-alpha2PHP ^7.1 || ^8.0

v0.0.1-alpha4PHP ^7.3 || ^8.0

v0.0.1-alpha6PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1351893?v=4)[mpyw](/maintainers/mpyw)[@mpyw](https://github.com/mpyw)

---

Top Contributors

[![mpyw](https://avatars.githubusercontent.com/u/1351893?v=4)](https://github.com/mpyw "mpyw (16 commits)")[![hexium310](https://avatars.githubusercontent.com/u/10758173?v=4)](https://github.com/hexium310 "hexium310 (1 commits)")

---

Tags

databaseeloquentlaravelmockmockeryphpunitmockingtestlaraveldatabasemockeloquentqueryilluminate

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mpyw-laravel-database-mock/health.svg)

```
[![Health](https://phpackages.com/badges/mpyw-laravel-database-mock/health.svg)](https://phpackages.com/packages/mpyw-laravel-database-mock)
```

###  Alternatives

[tucker-eric/eloquentfilter

An Eloquent way to filter Eloquent Models

1.8k4.8M26](/packages/tucker-eric-eloquentfilter)[reedware/laravel-relation-joins

Adds the ability to join on a relationship by name.

2121.2M13](/packages/reedware-laravel-relation-joins)[sofa/eloquent-testsuite

Helpers for fast and reliable UNIT tests for your Eloquent Models with PHPUnit

10104.7k](/packages/sofa-eloquent-testsuite)[mpyw/laravel-local-class-scope

A tiny macro that reuse a global scope class as a local scope

24102.6k](/packages/mpyw-laravel-local-class-scope)[elliotchance/concise

Concise is test framework for using plain English and minimal code, built on PHPUnit.

45223.8k4](/packages/elliotchance-concise)[toponepercent/baum

Baum is an implementation of the Nested Set pattern for Eloquent models.

3154.7k](/packages/toponepercent-baum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
