【问题标题】:How To Clean a Test Database With Doctrine?如何使用 Doctrine 清理测试数据库?
【发布时间】:2012-09-09 11:50:41
【问题描述】:

在运行单元测试时,我使用的是测试数据库(所以我不接触生产数据库)。单元测试完成后如何清理测试数据库?

我有实体管理器对象。有什么方法可以删除所有表中的所有行吗?

【问题讨论】:

    标签: zend-framework doctrine-orm zend-framework2 phpunit


    【解决方案1】:

    其实很简单。我创建了一个测试监听器:

    <?php
    
    class TestDbCleanupListener implements PHPUnit_Framework_TestListener
    {
    
        private $_em;
    
        private function _getEntityManager()
        {
            if (null === $this->_em) {
                $this->_em = Bootstrap::getServiceManager()->get('doctrine.entitymanager.orm_default');
            }
            return $this->_em;
        }
    
        /**
         * called when test is started - starts transaction
         * (non-PHPdoc)
         * @see PHPUnit_Framework_TestListener::startTest()
         */
        public function startTest(PHPUnit_Framework_Test $test)
        {
            $this->_getEntityManager()->beginTransaction();
        }
    
        /**
         * called when test is ended - rolls back the transaction 
         * @param PHUnit_Framework_Test $test
         * @param float $length the length of time for the test
         */
        public function endTest(PHPUnit_Framework_Test $test, $length)
        {
            $this->_getEntityManager()->rollback();
        }
    
        /**
         * Required for Interface
         * (non-PHPdoc)
         * @see PHPUnit_Framework_TestListener::addError()
         */
        public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
        {
    
        }
    
        /**
         * Required for Interface
         * (non-PHPdoc)
         * @see PHPUnit_Framework_TestListener::addFailure()
         */
        public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
        {
    
        }
    
        /**
         * Required for Interface
         * (non-PHPdoc)
         * @see PHPUnit_Framework_TestListener::addError()
         */
        public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
        {
    
        }
    
        /**
         * Required for Interface
         * (non-PHPdoc)
         * @see PHPUnit_Framework_TestListener::addSkippedTest()
         */
        public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
        {
    
        }
    
        /**
         * Required for Interface
         * (non-PHPdoc)
         * @see PHPUnit_Framework_TestListener::startTestSuite()
         */
        public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
        {
    
        }
    
        /**
         * Required for Interface
         * (non-PHPdoc)
         * @see PHPUnit_Framework_TestListener::endTestSuite()
         */
        public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
        {
    
        }
    
    }
    

    【讨论】:

    • 您能否提供更多关于如何让监听器运行的信息?我已经将 放在了我的 phpunit.xml 中......它会在那个时候自动运行还是什么?我这辈子都无法让它运行,而且几乎没有任何关于让它在谷歌上运行的问题。 gist.github.com/intellix/7145160
    • @DominicWatson 我已经几个月没有使用 ZF2,但是尝试在我的 github 中查看这个骨架 ZF2 项目 - 应该设置为正常工作:github.com/RichardKnop/zend-v2-skeleton
    • ZF2 Doctrine Module 的 Ocramius 告诉我使用 Doctrine Data Fixtures 测试 Doctrine 应用程序:gist.github.com/Ocramius/3994325
    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多