Hi All,
Not quite sure where to start. I guess I'll list the problems I'm having: my unit tests will not connect to the database (using the same bootstrap and config.ini file as the application), while the application has no problem connecting. It seems as if the unit tests run on a different php.ini file than the application.
My config file (obfuscated, of course):
[default]
database.adapter = PDO_MYSQL
database.params.host = xxx.xxx.xxx.xxx
database.params.username = me
database.params.password = mypassword
database.params.dbname = test_data
This works just dandy for the modules under the application folder. When I try to run a unit test with this I get:
The mysql driver is not currently installed.
Doing a phpinfo() dump within the unit test does indeed show that the MYSQL drivers are loaded, and the bootstrap file is getting run properly and set the correct values in Zend_Db_Table_Abstract::setDefaultAdapter.
For completeness, here is my bootstrap file:
class Bootstrap
{
public function __construct($config_section = 'production')
{
define('ROOT_DIR', dirname(dirname(__FILE__)));
define('APPLICATION_PATH', dirname(__FILE__));
define('APPLICATION_ENVIRONMENT', 'development');
set_include_path
(
get_include_path() .
PATH_SEPARATOR . ROOT_DIR . '/library/' .
PATH_SEPARATOR . APPLICATION_PATH . '/default/models/'
);
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
Zend_Registry::set('configSection', $config_section);
$config = new Zend_Config_Ini(APPLICATION_PATH . '/config.ini', $config_section);
Zend_Registry::set('config', $config);
date_default_timezone_set($config->date_default_timezone);
$db = Zend_Db::factory($config->database);
Zend_Db_Table_Abstract::setDefaultAdapter($db);
Zend_Registry::set('db', $db);
}
public function configureFrontController()
{
$frontController = Zend_Controller_Front::getInstance();
$frontController->setControllerDirectory(APPLICATION_PATH . '/default/controllers/');
}
public function runApp()
{
$this->configureFrontController();
$frontController = Zend_Controller_Front::getInstance();
$frontController->dispatch();
}
}
and here my unit test:
require_once dirname(__FILE__) . '/../../../TestConfiguration.php';
require_once '../../../../application/default/models/Users.php';
class UsersTest extends PHPUnit_Framework_TestCase
{
public function setup()
{
TestConfiguration::setupDatabase();
}
public function testFetchAll()
{
$users = new Users();
$this->assertSame(4, $users->fetchAll()->count());
}
public function testFetchLatestShouldFetchLatestEntry()
{
$usersFinder = new Users();
$users = $usersFinder->fetchLatest(1);
$this->assertSame(1, $users->count());
$user = $users->current();
$this->assertSame(4, (int)$user->id);
}
}
And the TestConfiguration class:
TestConfiguration::setup();
class TestConfiguration
{
static $bootstrap;
static function setup()
{
require_once dirname(__FILE__) . '/../application/bootstrap.php';
set_include_path(get_include_path() . PATH_SEPARATOR . realpath(dirname(__FILE__ . '/../library/')));
self::$bootstrap = new Bootstrap('testing');
}
static function setupDatabase()
{
$db = Zend_Registry::get('db');
$db->query(<<<EOT
DROP TABLE IF EXISTS users
EOT
);
}
}
And the Users class:
class Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
protected $_rowClass = 'User';
function __get($key)
{
if (method_exists($this, $key)) {return $this->$key();}
else {return parent::__get($key);}
}
function fetchLatest($count = 10)
{
return $this->fetchAll(null, 'date_created DESC', $count);
}
}
I'm not sure where to go from here. My thought is that since the application is working just fine, perhaps PHPUNIT is not configured correctly, or reading a different PHP.INI file (eventhough I did a global search for all PHP.INI and edited them accordingly to load all MYSQL drivers).
Thanks for any help!
-Mike
2009年1月15日星期四
订阅:
博文评论 (Atom)
没有评论:
发表评论