Unit testing and DB connnection

Hello.

I have troubles with configuring testing framework.

When try to run unit tests on the whole "unit" directory the first test runs good, but all other tests throw a CDbException:




macdi:tests cr0t$ phpunit unit

PHPUnit 3.4.0 by Sebastian Bergmann.


.EEEEEEEEEEEEEEEEEEEEEEE


Time: 1 second


There were 23 errors:


1) CategoryPropertiesSetTest::testCreate

CDbException: CDbConnection is inactive and cannot perform any DB operations.


/Users/cr0t/Workspace/frameworks/yii-trunk/framework/db/CDbConnection.php:380

/Users/cr0t/Workspace/frameworks/yii-trunk/framework/db/ar/CActiveRecord.php:1930

/Users/cr0t/Workspace/frameworks/yii-trunk/framework/db/ar/CActiveRecord.php:328

/Users/cr0t/Workspace/frameworks/yii-trunk/framework/test/CDbFixtureManager.php:294

/Users/cr0t/Workspace/frameworks/yii-trunk/framework/test/CDbTestCase.php:117


2) CategoryTest::testCreate

CDbException: CDbConnection is inactive and cannot perform any DB operations.


/Users/cr0t/Workspace/frameworks/yii-trunk/framework/db/CDbConnection.php:380

/Users/cr0t/Workspace/frameworks/yii-trunk/framework/db/ar/CActiveRecord.php:1930

/Users/cr0t/Workspace/frameworks/yii-trunk/framework/db/ar/CActiveRecord.php:328

/Users/cr0t/Workspace/frameworks/yii-trunk/framework/test/CDbFixtureManager.php:294

/Users/cr0t/Workspace/frameworks/yii-trunk/framework/test/CDbTestCase.php:117


[...]



Why this can be (maybe there is a problem with __sleep() function at CDbConnection class)? If I run each test manually - everything is fine.

Anyone?

To establish a connection, set active to true after specifying connectionString, username and password.


$connection=new CDbConnection($dsn,$username,$password);

$connection->active=true;

more informations here.

My problem is that the first test from units runs successfully, but all another tests fails with non active db error. This happens if I try to run the whole folder with tests, but if runs each test manually everything is fine.

Make sure you always reuse the same CDbConnection instance, the one we made active.

Please show your DB config.

protected/config/test.php:




<?php


return CMap::mergeArray(

	require(dirname(__FILE__).'/main.php'),

	array(

		'components'=>array(

			'fixture'=>array(

				'class'=>'system.test.CDbFixtureManager',

			),

			'db'=>array(

				'connectionString' => 'mysql:host=localhost;dbname=front_objects_one_yii_test',

				'emulatePrepare' => true,

				'username' => 'root',

				'password' => '',

				'charset' => 'utf8',

			),

		),

	)

);



protected/config/main.php:




[...]

		/**

		 * DBs configuration

		 * front_objects_one or front_objects_two database described in the "objects_db.php" file in this directory

		 */

		'db'=>require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'objects_db.php'),

		'db_internal'=>array(

			'connectionString' => 'mysql:host=localhost;dbname=front_internal',

			'emulatePrepare' => true,

			'username' => 'root',

			'password' => '',

			'charset' => 'utf8',

		),

[...]



protected/config/objects_db.php:




<?php


/**

 * Current front_objects_? database configuration

 * Can be changed at the exporting new data

 */


return array(

	'connectionString' => 'mysql:host=localhost;dbname=front_objects_one_yii',

	'emulatePrepare' => true,

	'username' => 'root',

	'password' => '',

	'charset' => 'utf8',

);



I try to run tests with simple ‘db’ config (without require another file), but it is also fails.

We need to use additional config-file (objects_db.php), because we have external scripts which can replace it, and also we have cache dependencies on this file.

Same as my test config for http://code.google.com/p/yiiext/. But mine works flawlessly.

I get this error too. But I want to modify the scenario a little:

One dot is written to the console before a row of ‘E’, but I don’t think the first test pass. I think the dot represents fixture setup. I have an empty test that generates a dot (pass), followed by a row of E´s.

I know that the first test being run in "directory" mode will fail with message "Trying to get property of non-object". This might be the reason all tests fail. The failing code is "$mymodel = new Mymodel;". IIRC it used to work in at least one 1.1 pre-release.

Edit:

If I comment out this code the problem mentioned above goes away:




public $fixtures=array(

  'mymodel'=>'Mymodel',

);



Table name as well as AR model name is Mymodel.

Edit2:

When this was added it worked again




require_once(dirname(__FILE__).'/../../models/Mymodel.php');



Then I removed this line again, still works ???

Edit3:

Now it’s needed again (I might have done a mistake, commenting it out)

Edit4:

This is really strange. I can provoke the error by omitting the ".php" e. g. for ItemTest.php:




phpunit unit/ItemTest



After that it wants a little help to find the model. The behavior isn’t deterministic.

When the "CDBConnection is inactive …" problem shows




phpunit unit



the failing test isn’t listed, so look for the problematic test file by calling them one by one.

/Tommy