【发布时间】:2017-09-11 14:17:31
【问题描述】:
上下文:
我有这个接口,它扩展了其他三个接口:
interface BasicTagsInterface extends TitleTagInterface, DescriptionTagInterface, KeywordsTagInterface {}
-
TitleTagInterface只有一个getSeoTitle方法。 -
DescriptionTagInterface只有一个getSeoDescription方法。 -
KeywordsTagInterface只有一个getSeoKeywords方法。
这是我想用 PhpUnit 测试的方法:
public function fromResource($resource)
{
if ($resource instanceof TitleTagInterface) {
$this->setTitle($resource->getSeoTitle());
}
if ($resource instanceof DescriptionTagInterface) {
$this->setDescription($resource->getSeoDescription());
}
if ($resource instanceof KeywordsTagInterface) {
$this->setKeywords($resource->getSeoKeywords());
}
}
这就是我目前尝试测试它的方式:
$resource = $this->getMockBuilder(BasicTagsInterface::class)->setMethods([
'getSeoTitle',
'getSeoDescription',
'getSeoKeywords',
])->getMock();
$resource->method('getSeoTitle')->willReturn('Awesome site');
$resource->method('getSeoDescription')->willReturn('My awesome site is so cool!');
$resource->method('getSeoKeywords')->willReturn('awesome, cool');
// TagBuilder::fromResource() is the method above
$this->tagBuilder->fromResource($resource);
我也试过了:
class A implements BasicTagsInterface {
public function getSeoDescription(){}
public function getSeoKeywords(){}
public function getSeoTitle(){}
}
// And in the test method:
$resource = $this->getMockBuilder(A::class) // etc.
问题:
使用此配置,$resource instanseof BasicTagsInterface 返回 true,其他 instanceof 测试返回 false。
我的问题:
如何模拟扩展其他接口的接口并让扩展接口上的 instanceof 测试返回 true,因为它们应该在测试用例之外?
【问题讨论】: