简体   繁体   English

在 PHPUnit 中,如何模拟父方法?

[英]In PHPUnit, how do I mock parent methods?

I want to test a class method that calls upon a parent method with the same name.我想测试一个调用同名父方法的 class 方法。 Is there a way to do this?有没有办法做到这一点?

class Parent {

    function foo() {
        echo 'bar';
    }
}

class Child {

    function foo() {
            $foo = parent::foo();
            return $foo;
    }
}

class ChildTest extend PHPUnit_TestCase {

    function testFoo() {
        $mock = $this->getMock('Child', array('foo'));

        //how do i mock parent methods and simulate responses?
    }
}

You dont mock or stub methods in the Subject-under-Test (SUT).您不在被测对象 (SUT) 中模拟或存根方法。 If you feel you have the need to mock or stub a method in the parent of the SUT, it likely means you shouldnt have used inheritance, but aggregation.如果您觉得需要在 SUT 的父级中模拟或存根方法,这可能意味着您不应该使用 inheritance,而是使用聚合。

You mock dependencies of the Subject-under-Test.您模拟了被测主题的依赖关系 That means any other objects the SUT requires to do work.这意味着 SUT 需要执行工作的任何其他对象。

An approach that works to my is the implementation of a wrap to the parent call on the child class, and finally mock those wrap.一种适用于我的方法是对子 class 的父调用执行包装,最后模拟这些包装。

You code modified:您修改了代码:

class Parent {

    function foo() {
        echo 'bar';
    }
}

class Child {

    function foo() {
            $foo = $this->parentFooCall();
            return $foo;
    }
    function parentFooCall() {
            return parent::foo();
    }
}

class ChildTest extend PHPUnit_TestCase {

    function testFoo() {
        $mock = $this->getMock('Child', array('foo', 'parentFooCall'));

        //how do i mock parent methods and simulate responses?
    }
 }

Here is how I did it, I have no idea if this is correct but it works:这是我的做法,我不知道这是否正确,但它有效:

class parentClass {
    public function whatever() {
        $this->doSomething();
    }
}

class childClass extends parentClass {
    public $variable;
    public function subjectUnderTest() {
        $this->variable = 'whocares';
        parent::whatever();
    }
}

now in the test i do:现在在我做的测试中:

public function testSubjectUnderTest() {
    $ChildClass = $this->getMock('childClass', array('doSomething'))
    $ChildClass->expects($this->once())
               ->method('doSomething');
    $ChildClass->subjectUnderTest();
    $this->assertEquals('whocares', $ChildClass->variable);
}

what the?什么?

My reasoning here is that all i really want to test is whether or not my variable got set.我的理由是,我真正想要测试的是我的变量是否已设置。 i don't really care about what happens in the parent method but since you can't prevent the parent method from being called what i do is mock the dependent methods of the parent method.我并不真正关心父方法中发生的事情,但是由于您无法阻止调用父方法,所以我要做的是模拟父方法的依赖方法。

now go ahead and tell me i'm wrong:)现在 go 提前告诉我我错了:)

I'm totally agree with @Gordon.我完全同意@Gordon。 I have same issue but I have tried few tricky concept.我有同样的问题,但我尝试了一些棘手的概念。

My scenario is like我的场景就像

class Parent { // Actual-Parent Class
    function save() {
        // do something
        return $this
    }
}

class Child extends Parent {
   // Subject under test
    function save() {
          // do something
          return parent::save();
    }
}

I have created another parent class with same name "Parent" and treat as a stub and include my stub class(parent) and ignore to actual parent (Actual parent class set into auto-load and stub-parent must be be included )我创建了另一个具有相同名称“父级”的父级 class 并将其视为存根并包含我的存根类(父级)并忽略实际父级(实际父级 class 设置为自动加载并且必须包含存根父级)

class Parent { //Stub-Parent class
    function save() {
        return $this
    }
}

Now I create mock object of Child class (via Mock-builder) and complete my test cases with end of assertSame.现在我创建子 class 的模拟 object(通过 Mock-builder),并以 assertSame 结尾完成我的测试用例。 :-) :-)

$this->assertSame($mock, $mock->save());

A satisfying solution in my opinion is to create a class that inherits from your class under test and override the implementation of the method you want to give another implementation.在我看来,一个令人满意的解决方案是创建一个继承自被测 class 的 class 并覆盖您想要提供另一个实现的方法的实现。 This has its flaws: it does not always work, eg for already overridden methods and for private methods.这有其缺陷:它并不总是有效,例如对于已经被覆盖的方法和私有方法。

class Parent
{
    function bar()
    {
        echo 'bar';
    }
}

class Child extends Parent
{
    function foo()
    {
        parent::bar();
        echo 'foo';
    }
}

class mockChild extends Child
{
    function bar()
    {
        echo 'baz';
    }
}

class ChildTest extends PHPUnit_TestCase 
{
    function testFoo() {
        $sut = new mockChild();
        $sut->foo();
    }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM