【问题标题】:Unit Testing Doctrine 2 Models单元测试准则 2 模型
【发布时间】:2011-05-27 03:34:23
【问题描述】:

单元测试 Doctrine 2 模型的方法是什么?我将它与 Zend Framework 1.11 一起使用。它有使用 PHPUnit 的 Zend_Test。我认为正确使用的是PHPUnit_Extensions_Database_TestCase。在 Zend Framework 中,我可以使用Zend_Test_PHPUnit_Db。如何修改代码以单元测试 Doctrine Models 而不是 Zend_Db 类。

第一,我认为我必须使用 Doctrine 的东西,而不是使用 Zend_Db 的东西

class BugsTest extends Zend_Test_PHPUnit_DatabaseTestCase
{
    private $_connectionMock;

    protected function getConnection()
    {
        if($this->_connectionMock == null) {
            $connection = Zend_Db::factory(...);
            $this->_connectionMock = $this->createZendDbConnection(
                $connection, 'zfunittests'
            );
            Zend_Db_Table_Abstract::setDefaultAdapter($connection);
        }
        return $this->_connectionMock;
    }
    ...
}

【问题讨论】:

    标签: unit-testing zend-framework doctrine phpunit zend-test


    【解决方案1】:

    您到底想测试什么?如果它只是模型本身 - 那并不太难。 Doctrine 2 模型只是简单的 PHP 对象,带有包含映射信息的注释。这就是 Doctrine 2 的“坚持无知”的好处:您可以像任何常规课程一样测试模型。

    【讨论】:

      【解决方案2】:

      我发现 DoctrineExtensions 通过 ORMTestCase 扩展了 PHPUnit。用法看起来像

      namespace MyProject\Tests;
      
      use DoctrineExtensions\PHPUnit\OrmTestCase
      
      class EntityFunctionalTest extends OrmTestCase
      {
          protected function createEntityManager()
          {
              return Doctrine\ORM\EntityManager::create(..);
          }
      
          protected function getDataSet()
          {
              return $this->createFlatXmlDataSet(__DIR__."/_files/entityFixture.xml");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-21
        • 2015-06-24
        • 2017-12-19
        • 2010-12-26
        • 2015-06-25
        • 2016-11-19
        相关资源
        最近更新 更多