我正在使用 PHPUnit 对基于 Zend 框架的项目进行单元测试,似乎测试太多了。
症状:我正在编写另一个测试并对此特定功能启动此测试导致测试失败并出现错误
堆栈跟踪:
Zend_Db_Adapter_Exception: SQLSTATE[08004] [1040] Too many connections in /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Db/Adapter/Pdo/Abstract.php on line 144
Call Stack:
0.0002 639184 1. {main}() /Users/MyUser/pear/bin/phpunit:0
0.0036 1243552 2. PHPUnit_TextUI_Command::main() /Users/MyUser/pear/bin/phpunit:46
0.0036 1244568 3. PHPUnit_TextUI_Command->run() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:130
0.0036 1244568 4. PHPUnit_TextUI_Command->handleArguments() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:139
0.0202 5745936 5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /Users/MyUser/pear/share/pear/PHPUnit/TextUI/Command.php:671
0.0202 5747448 6. PHPUnit_Util_Configuration->getTestSuite() /Users/MyUser/pear/share/pear/PHPUnit/Util/Configuration.php:768
0.0539 6143840 7. PHPUnit_Framework_TestSuite->addTestFiles() /Users/MyUser/pear/share/pear/PHPUnit/Util/Configuration.php:848
0.8321 26889016 8. PHPUnit_Framework_TestSuite->addTestFile() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:419
0.8329 27067008 9. PHPUnit_Framework_TestSuite->addTestSuite() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:392
0.8329 27068424 10. PHPUnit_Framework_TestSuite->__construct() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:318
0.8554 27706232 11. PHPUnit_Framework_TestSuite->addTestMethod() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:214
0.8555 27706384 12. PHPUnit_Framework_TestSuite::createTest() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:831
0.8556 27719664 13. ControllerTestCaseAbstract->__construct() /Users/MyUser/pear/share/pear/PHPUnit/Framework/TestSuite.php:554
0.8586 27775696 14. Zend_Application_Bootstrap_BootstrapAbstract->bootstrap() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/tests/ControllerTestCaseAbstract.php:24
0.8586 27775696 15. Zend_Application_Bootstrap_BootstrapAbstract->_bootstrap() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:586
0.8586 27775696 16. Zend_Application_Bootstrap_BootstrapAbstract->_executeResource() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:629
0.8586 27776040 17. Bootstrap->_initDb() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Application/Bootstrap/BootstrapAbstract.php:669
0.8590 27782440 18. Zend_Db_Adapter_Abstract->getConnection() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/application/Bootstrap.php:97
0.8590 27782440 19. Zend_Db_Adapter_Pdo_Mysql->_connect() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Db/Adapter/Abstract.php:315
0.8591 27782440 20. Zend_Db_Adapter_Pdo_Abstract->_connect() /Users/MyUser/workspaces/gitrepo/myprojectcom/webapp/library/Zend/Db/Adapter/Pdo/Mysql.php:109
为避免怀疑,这是我的数据库引导功能:
protected function _initDb() {
$databaseConfig = $this->_getDbConfig(); //loaded from an .ini file
$resource = Zend_Db::factory($databaseConfig);
$resource->getConnection();
Zend_Db_Table::setDefaultAdapter($resource);
return $resource;
}
各种其他元素:
- 评论一个类的测试方法让我可以毫无问题地添加这个新的测试用例,所以问题不是来自我特定的新案例,而是来自完成的测试量
- 如您所见,我有一个非常基本的 _initDb 引导函数
- 在不使用“--filter”启动整个测试套件时也会失败
- 我的环境是:Mac OS X 10.7.3 Lion,XAMPP 的 MySQL 服务器 5.1.44,通过 PEAR 安装的 PHPunit 3.6.11,PHP 5.3.8
- 我确实在拆除时关闭了我的连接[编辑]
我在找什么:任何解释,最重要的是一种解决方法
已找到解决方法:在 /Applications/XAMPP/etc/my.cnf 的 [mysqld] 中添加脏修复:
set-variable=max_connections=200