mpyw / laravel-database-mock
Database Mocking Library which mocks PDO underlying Laravel Connection classes
Installs: 4 099
Dependents: 1
Suggesters: 0
Security: 0
Stars: 6
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: ^7.3 || ^8.0
- ext-pdo: *
- illuminate/database: ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0
- mpyw/mockery-pdo: 0.0.1-alpha7@alpha
Requires (Dev)
- nilportugues/sql-query-formatter: ^1.2.2
- orchestra/testbench: *
- orchestra/testbench-core: >=7.0
- phpunit/phpunit: >=9.5
This package is auto-updated.
Last update: 2024-12-21 20:08:48 UTC
README
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' => '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());