gpt4 book ai didi

database - 我如何开始使用 PHPUnit,其中我的类构造需要预配置的数据库连接?

转载 作者:搜寻专家 更新时间:2023-10-30 20:32:14 25 4
gpt4 key购买 nike

我有一个在内部使用大量数据库的类,所以我用我应该传递给它的 $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()){

最佳答案

你会这样做:

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 , 作为一个开始。一个例子是下划线,在链接中查找变量。还有类命名。

关于database - 我如何开始使用 PHPUnit,其中我的类构造需要预配置的数据库连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2936631/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com