How can I unit test my Eloquent models when I'm using the ORM outside of Laravel? What I'm hoping to do it run tests on each model but somehow mock the database connection/query/builder(?) object.

Below is something along the lines of what I'm trying to do:

$capsule = new Capsule; 

    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'mydb',
    'username'  => 'dbuser',
    'password'  => 'badpass',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => ''


Below is my model:


use Illuminate\Database\Eloquent\Model as Eloquent;

class Users extends Eloquent
    public function myCustomFind($id)
        // .. lots of various checks before find() ...

        return $this->find($id);

Then, in my unit tests I can do the following (not a working version, but I hope you can see what I'm hoping to do):


$users = new Users();

$user = $users->myCustomFind(1);

...I just have no idea how to do this without full integration (data being written to the database). I just want to test my models, not the ORM, also so my tests are fast and only checking the correct params are passed to the query builder, and that my method handles the resulting data correctly. Will Eloquent allow this? I notice that Laravel has it's own TestCase class but from what I see this caters for all aspects of testing in Laravel (controllers, etc)

If Eloquent is not a good choice for unit testing outside of Laravel, are there any popular ORMs that can? I haven't spent much time with others like Doctrine or Propel to know what they are capable of in that respect. Would appreciate any recommendations.


You probably want to test a class that is using an eloquent model rather than the model itself.

To create an in memory database for eloquent to query against in your test you want to do create a data base and then create your tables in it with some data to test against.

use MyNamespace\ClassToTest;
use MyNamespace\Models\MyModel;
use Illuminate\Database\Capsule\Manager as DB;

class ExampleTest extends PHPUnit_Framework_TestCase

    public function setUp()
    protected function configureDatabase()
        $db = new DB;
            'driver'    => 'sqlite',
            'database'  => ':memory:',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',

    public function migrateIdentitiesTable()
        DB::schema()->create('my_table', function($table) {
        MyModel::create(array('page_id' => 1));
        MyModel::create(array('page_id' => 2));


    public function testExtract()
        $extractor = new ClassToTest();
        $result = $extractor->someFunctionThatUsesAnEloquentModel(1);
        $expected = array(
            'id' => '1',
            'page_id' => '1'
        $this->assertEquals($expected['page_id'], $result['page_id']);


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.