用phpunit创建一个模拟类吗? [英] Create a mock class with phpunit?

查看:63
本文介绍了用phpunit创建一个模拟类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以用phpunit创建模拟类,而不是模拟对象?我正在寻找一种进行依赖注入的方法,而不必显式传递类可能需要在构造函数中(或在任何地方)使用的每个对象.在所有这些情况下都会返回"true"的东西:

Is there a way to create a mock class, as opposed to a mock object, with phpunit? I'm looking for a way to do dependency injection without having to explicitly pass every object a class might need to work with in the constructor (or wherever). Something that will return "true" for all of these cases:

public function testAAAA()
{
  $foo = $this->getMock('foo', array('bar'));
  var_dump(class_exists('foo', false));
  var_dump(method_exists('foo', 'bar'));
  var_dump(method_exists($foo, 'bar'));
}

此打印:

bool(true)
bool(false)
bool(true)

表示虽然它成功创建了一个伪造的'foo'类,但没有将'bar'方法绑定到它.

indicating that while it did successfully create a fake 'foo' class it did not bind a 'bar' method to it.

我正在使用phpunit 3.7.5.

I am using phpunit 3.7.5.

推荐答案

我怀疑您实际上并不想这样做(因为您可以使用PHPUnit的嘲笑生成器禁用构造函数等,请参见

I suspect that you don't actually want to do this (as you can disable constructors and so on with PHPUnit's mockbuilder, see the docs ), but assuming you do want or need to, this should do the trick:

$foo = $this->getMockBuilder('nonexistant')
        ->setMockClassName('foo')
        ->setMethods(array('bar'))
        ->getMock();

    var_dump(class_exists('foo', false));
    var_dump(method_exists('foo', 'bar'));
    var_dump(method_exists($foo, 'bar'));

    $cls = new ReflectionClass('foo');
    var_dump($cls->hasMethod('bar'));

老实说,我不确定为什么需要在上面指定不同的名称(不存在和foo),但是当要模拟的类还不存在时,这似乎与PHPUnit的行为有关,并且setMockClassName生成一个扩展该类的类.或者其他的东西.它可能有效地解决了错误/边缘情况-这是库的奇怪用法.您应该可以单独通过getMock函数执行相同的操作,这很丑陋.

I'm honestly not sure about the specifics of why you need to specify different names (nonexistant and foo) above, but it seems to have to do with PHPUnit's behavior when the class being mocked doesn't exist yet, and having setMockClassName generate a class extending that class. Or Something. It's probably effectively working around a bug/edge-case -- this is odd usage of the library. You should be able to do the same thing through the getMock function alone, it's just uglier.

顺便说一句,听起来您应该熟悉 php的反射功能.它不是那里功能最强大的反射库,但是非常不错.我已使用它根据类的构造函数参数和模型"库的属性来生成有关类的必填字段和可选字段的元信息,其中该元信息用于生成接受正确类型的值的形式.也就是说,生成类型化的表单而无需表单所针对的类的实例,并且无需手写大量的代码-整个功能总共约100行.显然我不知道您要做什么,但是从您发布的少量信息中,我猜想它比那种元数据更接近.

As an aside, it sounds like you should probably get familiar with php's reflection capabilities. It's not the most powerful reflection library out there, but it's pretty nice. I've used it to generate meta-information about required and optional fields for a class based on their constructor arguments and properties for a "model" library, where that meta-information is used to generate forms that accept the correct types of values. That is, generate typed forms without instances of the class the form is for, and without hand-writing a stupid amount of code -- it's about 100 lines in all for the entire feature. Obviously I don't know what you're trying to do, but from the small amount of info in your post, I'd guess it's closer to that type of meta-thing than not.

这篇关于用phpunit创建一个模拟类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆