定制 mpyw/laravel-database-mock 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mpyw/laravel-database-mock

最新稳定版本:v0.0.1-alpha6

Composer 安装命令:

composer require mpyw/laravel-database-mock

包简介

Database Mocking Library which mocks PDO underlying Laravel Connection classes

README 文档

README

Warning

Experimental

Database Mocking Library which mocks PDO underlying Laravel Connection classes.

Requirements

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());

统计信息

  • 总下载量: 7.67k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 8
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04