Posts Tagged ‘unitTesting’

CakePHP: Testing static database tables

Tuesday, November 30th, 2010

Took a while to work out how to do this, hope it helps someone. If you are unit testing models in CakePHP against large database tables that don’t ever change (in my case millions of rows of location data), you don’t really want to bother copying everything to the test database, or worse have the data re-imported before each individual test.

Instead you can set up your fixture like this:

class MyBigStaticTableFixture extends CakeTestFixture {
    var $name = 'MyBigStaticTable';
    var $table = 'my_big_static_tables';
    var $import = array('model' => 'MyBigStaticTable', 'records' => false);
}

// and your model test case as follows:

class MyBigStaticTableTestCase {
    ...
    function startTest() {
        ClassRegistry::config(array('ds' => 'default'));
        $this->MyBigStaticTable => ClassRegistry::init(array(
            'class' => 'MyBigStaticTable', 'ds' => 'default'));
    }
    ...
}

Not currently sure how this will affect related models. This is with CakePHP version 1.2 but I think should also work with 1.3.

Happy testing.