【问题标题】:How can I get started with PHPUnit, where my class construct requires a preconfigured db connection?我如何开始使用 PHPUnit,我的类构造需要预配置的数据库连接?
【发布时间】:2011-02-25 13:40:26
【问题描述】:

我有一个在内部使用大量数据库的类,因此我使用应该传递给它的 $db 句柄构建了构造函数。

我刚刚开始使用 PHPUnit,我不确定我应该如何继续并通过设置传递数据库句柄。

// Test code
public function setUp(/*do I pass a database handle through here, using a reference? aka &$db*/){
    $this->_acl = new acl;
}

// Construct from acl class
public function __construct(Zend_Db_Adapter_Abstract $db, $config = array()){

【问题讨论】:

    标签: database unit-testing zend-framework phpunit


    【解决方案1】:

    你会这样做:

    public class TestMyACL extends PHPUnit_Framework_TestCase {
    
        protected $adapter;
        protected $config;
        protected $myACL;
    
        protected function setUp() {
            $this->adapter  = // however you create a new ZendDbADapter
            $this->config   = // however you create a new config array
            $this->myACL    = new ACL($this->adapter, $this->config); // This is the System  Under Test (SUT)
        }
    
    }
    

    恕我直言,您需要改进命名约定。请参阅Zend Framework Naming Conventions,作为开始。一个例子是下划线,在链接中查找变量。还有类命名。

    【讨论】:

      【解决方案2】:

      你可以像构造函数一样不引用,因为这种方法最简单。

      【讨论】:

      • 您的意思是我应该在我的 setUp 函数中使用与在我的类构造中相同的代码吗?
      • @Ben Dauphinee:对不起,我想念你的问题。您可以在没有参考的情况下正常执行与构造函数相同的操作,因为此方法最简单。您真的必须为此使用参考吗?
      猜你喜欢
      • 1970-01-01
      • 2020-12-08
      • 2014-07-22
      • 1970-01-01
      • 1970-01-01
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多