PHP code example of imanghafoori / eloquent-mockery

1. Go to this page and download the library: Download imanghafoori/eloquent-mockery 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/ */

    

imanghafoori / eloquent-mockery example snippets




return [
  
   ...
  
  'connections' => [
     'my_test_connection' => [
         'driver' => 'arrayDB',
         'database' => '',
     ],
     
     ...
  ],
  ...
]

public function test_basic()
{
    config()->set('database.default', 'my_test_connection');

    # ::Arrange:: (Setup Sample Data)
    FakeDB::addRow('users', ['id' => 1, 'username' => 'faky', 'password' => '...']);
    FakeDB::addRow('users', ['id' => 1, 'username' => 'maky', 'password' => '...']);

    # ::Act:: (This query resides in your controller)
    $user = User::where('username', 'faky')->first();   # <=== This does NOT connect to a real DB.

    # ::Assert::
    $this->assert($user->id === 1);
    $this->assert($user->username === 'faky');
}

public function test_basic()
{
    # In setUp:
    FakeDB::mockEloquentBuilder();

    # ::Act::
    $this->post('/create-url', ['some' => 'data' ])

    # ::Assert::
    $user = User::first();
    $this->assertEquals('iman', $user->username);

    # In tearDown
    FakeDB::dontMockEloquentBuilder();
}