在PHPUnit中使用静态方法

时间:2013-09-09 22:00:14

标签: php mocking phpunit

我正在使用第三方库(因此无法更改静态方法调用的用法),这是ORM。现在为了使它工作,我的'实体'类必须扩展它们的实体类,它使用静态方法继承了几个依赖项,我想测试我的实体类抽象出这些静态调用,但不确定如何

代码如下:

// Entity class ripped from third party library
class Entity
{
    // other methods etc

    /**
     * Setter for field properties
     */
    public function __set($field, $value)
    {
        $fields = $this->fields();
        if(isset($fields[$field])) {
            // Ensure value is set with type handler
            $typeHandler = Config::typeHandler($fields[$field]['type']);
            $value = $typeHandler::set($this, $value);
        }
        $this->_dataModified[$field] = $value;
    }
}

// my Entity class
class User extends Entity
{

    public static function fields()
    {
        return array(
            'id' => array('type' => 'int', 'primary' => true, 'serial' => true),
            'fullname' => array('type' => 'string', 'required' => false),
            'email_address' => array('type' => 'string', 'required' => true, 'email' => true),
        );
    }
}

$user = new User();
$user->fullname = 'John Doe'; // dependancy on Config::typeHandler

现在我想模拟Config对象,允许我设置变量,我该怎么做?

由于

0 个答案:

没有答案
相关问题