如何调用remove()方法?

时间:2011-05-12 14:19:35

标签: unit-testing zend-framework phpunit

我在zend项目中测试模型,我有一个关于如何调用remove()方法的问题?

这是我正在测试的查找方法:

<?

class Admin_Model_Member2 extends Custom_Model_Base {


    protected function __construct() {
        parent::__construct();
    }



    static function load($id) {
        return self::_selectAndBind(
                get_class(),
                        self::getDefaultAdapter()
                        ->select()
                        ->from('member')
                        ->where('id = ?', array($id)),
                true);
    }



    function remove() {
        return $this->delete();
    }



}

Admin_Model_Member2扩展了Custom_Model_Base,这是Custom_Model_Base,

abstract class Custom_Model_Base    { 
static public function init($default_adapter = null)
    {
        if (self::$_db_default === null)
        {
            if (!is_null($default_adapter))
            {
                if (!$default_adapter instanceof Zend_Db_Adapter_Abstract)
                {
                    throw new Exception('Provided adapter does not extend Zend_Db_Adapter_Abstract');
                }
                self::$_db_default = $default_adapter;
            }
            else if (Zend_Registry::isRegistered('db'))
            {
                self::$_db_default = Zend_Registry::get('db');
            }
            else
            {
                throw new Exception('No default adapter provided for the model layer');
            }

        }
    }

 public function delete()
    {
        $where = array();
        foreach($this->_primary as $column)
        {
            $where[$column] = $this->_data[$column];
        }

        if ($this->_db->delete($this->_table, $where) != 0)
        {
            foreach($this->_primary as $column)
            {
                $this->_data[$column] = null;
            }
            return true;
        }
        return false;
    }
}

这是我写的测试用例,

public function testCanRemove() {
        $data = "80176";
            Admin_Model_Member2::init();
        $this->_model = Admin_Model_Member2::load($data);
        $this->assertTrue($this->_model->remove());
    }

我想测试remove()方法,所以我加载($ id)一个对象,但是当我调用$ this-&gt; _model-&gt; remove()时,它告诉我“调用成员函数delete( )在一个非对象“113中的类Custom_Model_Base是”if($ this-&gt; _db-&gt; delete($ this-&gt; _table,$ where)!= 0)“,有什么问题以及如何调用remove()方法? 无法在Admin_Model_Member2的构造函数中调用Admin_Model_Member2 :: init(),因为构造函数受到保护。

1 个答案:

答案 0 :(得分:1)

$ this-&gt; _db未在Custom_Model_Base中初始化。在尝试删除记录之前,您需要在某个时刻调用Admin_Model_Member2 :: init()。也许在Admin_Model_Member2的构造函数中。