开发者

How to set test datasource for specific models in CakePHP?

开发者 https://www.devze.com 2023-02-13 18:28 出处:网络
Out of the box, CakePHP 1.x has a default and test datasource. When running unit tests it uses the test database and before each test it can fixturise the data from code, 开发者_开发百科or import data

Out of the box, CakePHP 1.x has a default and test datasource. When running unit tests it uses the test database and before each test it can fixturise the data from code, 开发者_开发百科or import data from your default database.

In an app I'm building I have a couple of cases where it would be useful to specify in some of the model fixtures a database other than test:

  1. the datasource is a web service and the data returned can't easily be replicated with the test database,
  2. there are large read-only tables and it is very slow (and unnecessary) to recreate all this data before each test.

I've looked at CakeTestFixture but I can't see a good way to do this yet. Perhaps each model really wants to have a useDbConfig and useTestConfig property?

I can hack my way round this in some cases by doing something like:

function startTest() {
    $this->Flock =& ClassRegistry::init('Flock');
    $this->Flock->Sheep->useDbConfig = 'readonly_db';
    $this->Flock->Dog->useDbConfig = 'soap';
}

But this will only cover specific dependencies for one test case, and doing this across a suite of tests would be a maintenance nightmare.

Has anyone come across a similar situation and did you have a solution?


create the $test db connection in database.php and cake will take care of the rest. Look at some of the examples in the core test cases and you will see there is nothing special added.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号