Как эта переменная не определена? - PullRequest
0 голосов
/ 11 мая 2011

Я хочу знать, как тестировать модели в Zend Framework, но при запуске теста выдает ошибку, код следующий:

это модель, которую я хочу протестировать:

<?php



class Application_Model_User extends Custom_Model_Base {
    protected $_table = 'user';
    protected $_primary = array('id');
    protected $_primary_ai = 'id';
    protected $_data = array();
    protected $_data_changed = array();
    protected $_readonly = array('id');
    static
    protected $_columns = array(
        'id',
        'login',
        'password_hash',
        'name',
        'surname',
        'gender',
        'street',
        'postal_code',
        'city',
        'mobile',
        'homephone',
        'email',
        'is_active');

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

    static function create(array $data) {
        return parent::_create(
                $_table,
                get_class(),
                $data,
                self::$_columns,
                true
        );
    }

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

    static function find($name, $order=null, $limit=null, $offset=null) {
        return self::_selectAndBind(
                get_class(),
                        self::getDefaultAdapter()
                        ->select()
                        ->from($_table)
                        ->where('name = ?', array($name))
                        ->order($order)
                        ->limit($limit, $offset)
        );
    }

}

расширяет базовый класс:

<?

abstract class Custom_Model_Base
{
    /** @var Zend_Db_Adapter_Abstract */
    static protected $_db_default = null;

    /** @var Zend_Db_Adapter_Abstract */
    protected $_db = null;
    protected $_table = '';
    protected $_primary = array();
    /** $var string indicates which column from pk using auto increment function, set to null if none column is using auto incrementation */
    protected $_primary_ai = null;
    protected $_data = array();
    protected $_data_changed = array();
    protected $_readonly = array();


    /**
     * @param Zend_Db_Adapter_Abstract $adapter overrides global (static) adapter used for all models
     */
    protected function  __construct($adapter=null) {
        if ($adapter !== null) {
            if ($adapter instanceof Zend_Db_Adapter_Abstract)
            {
                $this->_db = $adapter;
                return;
            }
            $this->_db = &self::$_db_default;
        }

    }

    /**
     * @param $default_adapter allows to set default adapter for whole model layer based on that class
     */
    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');
            }

        }
    }

    /**
     * @return Zend_Db_Adapter_Abstract default database adapter
     */
    static public function getDefaultAdapter()
    {
        return self::$_db_default;
    }

    /**
     * Saves changed columns from the model object
     * @return bool success - true / failure - false
     */
    public function save()
    {
        $to_update = array();
        foreach(array_keys($this->_data_changed) as $col)
        {
            $to_update[$col] = $this->_data[$col];
        }

        if (count($to_update))
        {
            // create where clause
            $where = array();
            foreach($this->_primary as $pk)
            {
                $where = array($pk.' = ?' => $this->_data[$pk]);
            }

            return ($this->_db->update($this->_table, $to_update, $where) != 0);
        }
        else
        {
            return true;
        }
    }


    public function  __set($n, $v)
    {
        if (!isset($this->_data[$n]))
        {
            throw new Exception('Column \''.$n.'\' doesn\'t exists');
        }
        else if (in_array($n, $this->_readonly))
        {
            throw new Exception('Column \''.$n.'\' is set as read-only');
        }

        if ($this->_data[$n] != $v)
        {
            $this->_data_changed[$n] = 1;
            $this->_data[$n] = $v;
        }
    }

    public function  __get($v)
    {
        if (!isset($this->_data[$n]))
        {
            throw new Exception('Column \''.$n.'\' doesn\'t exists');
        }
        return $this->_data[$n];
    }


}

мой тестовый код:

<?php

require_once(APPLICATION_PATH.'/models/CustomModelBase.php');

class Model_User2Test 
    extends PHPUnit_Framework_TestCase
{
    protected $_model;

    public function setUp() {

        parent::setUp();

        $this->_model = new Application_Model_User2();

        //$foo = $this->getMock();
    }

    public function testCanDoTest() {
        $this->assertInstanceOf('Application_Model_User2', $this->_model);
        //$this->assertType('Application_Model_User2',new Application_Model_User2());
    }

    public function testCanFind() {
        $this->assertTrue(true);
        $this->_model->init();
        $this->assertNotNull($this->_model->find('admin'));
    }   
}

, когда я запускаю тест, он дает мне ошибку:

1) Model_User2Test::testCanFind
Undefined variable: _table
application\models\User2.php:57
tests\application\models\User2Test.php:27

почему _table не определен?на самом деле это определяется, когда я создаю объект?как я мог это исправить?

Ответы [ 2 ]

2 голосов
/ 11 мая 2011

Вы объявляете _$table защищенным:

protected $_table = 'user';

Таким образом, вы не можете получить к нему доступ, как вы делаете через экземпляр класса.Только класс, который наследует, может сделать это.Вам нужно объявить его общедоступным или использовать доступ в стиле получения / установки.

Редактировать:

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

В вашем классе вы используете $ _table, а не таблицу $ this -> _,Это то же самое в другом месте.Проверьте, чтобы убедиться, что вы правильно обращаетесь к переменным класса.

1 голос
/ 11 мая 2011

В вашем статическом методе Application_Model_User::find() в вашем запросе есть эта строка:

->from($_table)

Но в этом контексте $_table является локальной переменной, которая никогда не устанавливается,Похоже, вы хотите получить доступ к $this->_table вместо.

[Примечание: поскольку вы определили find() как статический метод, вы можете столкнуться с проблемами при попытке сослаться на $this во время статического вызова.Конечно, в вашем тесте вы, похоже, вызываете find() для экземпляра , поэтому с вами все будет в порядке.Вам действительно нужно find(), чтобы быть статическим методом?]

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...