单元测试中未加载Bootstrap资源

时间:2012-06-07 02:46:55

标签: zend-framework

我要做的是在单元测试中获取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()调用之后获取doctrine 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。

1 个答案:

答案 0 :(得分:0)

这对我有用(取自ZendCast Many-to-Many with Doctrine 2

    class ModelTestCase
        extends PHPUnit_Framework_TestCase
    {
        /**
         *
         * @var \Bisna\Application\Container\DoctrineContainer
         */
        protected $doctrineContainer;


      public function setUp()
    {
        global $application;
        $application->bootstrap();
        $this->doctrineContainer = Zend_Registry::get('doctrine');

        $em = $this->doctrineContainer->getEntityManager();
        $tool = new \Doctrine\ORM\Tools\SchemaTool($em);
        $tool->dropDatabase();
        $tool->createSchema($em->getMetadataFactory()->getAllMetadata());

        parent::setUp();
    }
用于单元测试的

Bootstrap.php如下所示:

<?php
    // Define path to application directory
    defined('APPLICATION_PATH')
       || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../../application'));

    // Define application environment
    define('APPLICATION_ENV', 'testing');

    // Ensure library/ is on include_path
    set_include_path(implode(PATH_SEPARATOR, array(
       realpath(APPLICATION_PATH . '/../library'),
       get_include_path()
    )));


    /** Zend_Application */
    require_once 'Zend/Application.php';
    require_once 'ModelTestCase.php';
    require_once 'ControllerTestCase.php';
    // Create application, bootstrap, and run
    $application = new Zend_Application(
       APPLICATION_ENV,
       APPLICATION_PATH . '/configs/application.ini'
    );
    //$application->bootstrap();
    clearstatcache();