0

我想做的是在单元测试中获取 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。

4

1 回答 1

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();
于 2012-06-07T08:52:29.280 回答