0
我有這種接口,其延伸其它三個接口:
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,因爲他們應該?
嗯,對我而言,這是一個命名空間問題......抱歉,感謝您的幫助! – Leogout