我想做的是在单元测试中获取 Doctrine EM 的实例(使用 Bisna 库来加载它)。当它在“正常”(不是单元测试)模式下运行时,我可以在控制器/模型中获取实例。而且我会发疯地将逐行代码与其工作的项目进行比较:(
单元测试:
<?php
class Application_Models_UserTest extends PHPUNit_Framework_TestCase
{
protected $_em;
protected $_model;
public function setUp()
{
$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$this->_em = Zend_Registry::get('doctrine')->getEntityManager();
$this->_model = new Application_Model_User();
parent::setUp();
//$this->_em = Zend_Registry::get('doctrine')->getEntityManager();
//$this->_model = new Application_Model_User();
}
我得到:
1) Application_Models_UserTest::testGetByEmailExists
Zend_Exception: No entry is registered for key 'doctrine'
/var/www/html/social_test/library/vendor/Zend/Registry.php:147
/var/www/html/social_test/tests/application/models/UserTest.php:10
正如您从注释行中看到的那样,我尝试(绝望地)在 parent::setUp() 调用后获取教义 EM 实例,但未成功。认为 parent::setUp() 完成初始化应用程序,它可能会有所帮助。
我正在运行它phpunit -c phpunit.xml
。phpunit.xml:
<phpunit bootstrap="./bootstrap.php">
<testsuite name="Application Test Suite">
<directory>./application</directory>
</testsuite>
<testsuite name="Library Test Suite">
<directory>./library</directory>
</testsuite>
<filter>
<whitelist>
<directory suffix=".php">../../library</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./public/report" charset="UTF-8"
yui="true" highlight = "true" lowUpperBound="50" highLowerBound="80" />
</logging>
</phpunit>
单元测试 bootstrap.php 也是标准的且未受影响。插件路径在 application.ini 中指定,它在生产部分(所以它在测试部分继承)。ZF 版本 1.11.11。