PHP code example of mpyw / laravel-database-mock

1. Go to this page and download the library: Download mpyw/laravel-database-mock library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mpyw / laravel-database-mock example snippets


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

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

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

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