Unit-testing – Unit testing Eloquent outside of Laravel

laravelormphpunitunit testing

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; 

$capsule->addConnection(array(
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'mydb',
    'username'  => 'dbuser',
    'password'  => 'badpass',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => ''
));

$capsule->bootEloquent();

Below is my model:

<?php

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):

$mockSomething->expects($this->once)
    ->method('find')
    ->with(1);

$users = new Users();
$users->setSomething($mockSomething);

$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.

Best Answer

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()
    {
        $this->configureDatabase();
        $this->migrateIdentitiesTable();
    }
    protected function configureDatabase()
    {
        $db = new DB;
        $db->addConnection(array(
            'driver'    => 'sqlite',
            'database'  => ':memory:',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ));
        $db->bootEloquent();
        $db->setAsGlobal();
    }

    public function migrateIdentitiesTable()
    {
        DB::schema()->create('my_table', function($table) {
            $table->increments('id');
            $table->integer('page_id');
            $table->timestamps();
        });
        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']);
    }

}
Related Topic