PHPUnit:同时模拟一个接口和超类

时间:2017-07-25 17:11:56

标签: php unit-testing mocking phpunit

我有一个界面:

interface MyInterface {

   const SOME_CONSTANT='hi';

   function method(): void;
}

和超级班:

class MyClass {

   private $id;

   function method1(){
       //do something
   }
}

我需要得到一个实现接口的模拟并扩展超类,即mock需要同时是MyInterface和MyClass类型。

TestCase::createMock方法只需要一个类来模拟,所以我希望看看是否有可能使用PHPUnit 6获得我需要的模拟。

1 个答案:

答案 0 :(得分:4)

你可以在这里使用一些预言。

class ClassAndInterfaceTest extends PHPUnit_Framework_TestCase
{
    /**
     * @test
     */
    function classAndIface ()
    {
        $myclass_instance = $this->prophesize (MyClass::class)
            ->willImplement (MyInterface::class)->reveal ();

        $this->a ($myclass_instance);
        $this->b ($myclass_instance);
    }

    function a (MyInterface $i)
    {
    }

    function b (MyClass $i)
    {
    }
}