【问题标题】:Zend + Doctrine + PHPUnit = There is no open connectionZend + Doctrine + PHPUnit = 没有打开的连接
【发布时间】:2012-01-19 04:17:39
【问题描述】:

我正在尝试使用 Doctrine ORM 为 Zend 创建带有 phpunit 的单元测试。当我尝试创建一个扩展 Zend_Test_PHPUnit_DatabaseTestCase 的测试类时,我在执行 PHPUnit 时收到一条消息:“没有打开的连接”

这里是完整的来源:

<?php

class AclTest extends Zend_Test_PHPUnit_DatabaseTestCase
{
    private $_userAdmin;

    public function setUp()
    {
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');

        $this->_userAdmin = Model_UserTable::getInstance()->findOneByUsername('admin');

        parent::setUp();
    }

    protected function getConnection()
    {
        $pdo = new PDO('mysql:host=localhost;dbname=mydbname', 'root', 'pwd');
        return $this->createDefaultDBConnection($pdo, 'testdb');
    }

    protected function getDataSet()
    {
        return null;
    }


    public function testHasProfilPermission()
    {
        //execute some tests

    }
}

你怎么看?

谢谢

【问题讨论】:

  • 你运行的是什么版本? Zend Framework 1.x 不适用于最新的 PHPUnit 3.6。
  • 我正在使用 Zend Framework 1.11.11、Doctrine 1.2.4、PHPUnit 3.4.14 和桥 ZFDoctrine

标签: zend-framework doctrine phpunit


【解决方案1】:

请试试这个:

class AclTest extends Zend_Test_PHPUnit_DatabaseTestCase
{
    private $_userAdmin;
    /** @var PDO **/
    protected $pdo;

    public function __construct()
    {
        $this->pdo = new PDO('mysql:host=localhost;dbname=mydbname', 'root', 'pwd');
    }

    public function setUp()
    {
        $this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');

        $this->_userAdmin = Model_UserTable::getInstance()->findOneByUsername('admin');

        parent::setUp();
    }

    protected function getConnection()
    {
        return $this->createDefaultDBConnection($this->pdo, 'testdb');
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 2013-04-20
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多