https://github.com/mpyw/laravel-database-mock
[Experimental] Database Mocking Library which mocks PDO underlying Laravel Connection classes
https://github.com/mpyw/laravel-database-mock
database eloquent laravel mock mockery php
Last synced: 15 days ago
JSON representation
[Experimental] Database Mocking Library which mocks PDO underlying Laravel Connection classes
- Host: GitHub
- URL: https://github.com/mpyw/laravel-database-mock
- Owner: mpyw
- License: mit
- Created: 2020-08-27T08:07:41.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2025-03-05T11:25:04.000Z (about 2 months ago)
- Last Synced: 2025-04-01T23:37:07.817Z (22 days ago)
- Topics: database, eloquent, laravel, mock, mockery, php
- Language: PHP
- Homepage:
- Size: 21.5 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Laravel Database Mock [](https://github.com/mpyw/laravel-database-mock/actions) [](https://coveralls.io/github/mpyw/laravel-database-mock?branch=master)
> [!WARNING]
> **Experimental**Database Mocking Library which mocks `PDO` underlying Laravel Connection classes.
## Requirements
- PHP: `^8.2`
- Laravel: `^11.0 || ^12.0`
- Mockery: `^1.6.12`
- [mpyw/mockery-pdo](https://github.com/mpyw/mockery-pdo): `alpha`## Installing
```bash
composer require mpyw/laravel-database-mock:VERSION@alpha
```## Example
### SELECT
```php
$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
```php
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
```php
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
```php
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());
```