我需要创建一个两倍的PHPUnit测试:
我该怎么办?
在->getMockBuilder()->getMock()
,->createMock()
,->createPartialMock()
,->createTestProxy()
和->getMockBuilder()->enableProxyingToOriginalMethods()->setProxyTarget()
之间,我似乎能够满足上述三个要求中的两个,但不是全部三个。
答案 0 :(得分:1)
通过模拟链接解决:
protected function getMixedMock(
string $className,
array $constructorArgs,
array $stubMethods,
array $proxyMethods
) : array
{
$stub = $this->getMockBuilder($className)
->setMethods($stubMethods)
->getMock();
$proxy = $this->getMockBuilder(Local::class)
->setConstructorArgs($constructorArgs)
->setMethods(array_unique(array_merge($stubMethods, $proxyMethods)))
->enableProxyingToOriginalMethods()
->setProxyTarget($stub) // <-- CHAINING HERE
->getMock();
return [$stub, $proxy];
}
public function test()
{
[$stub, $proxy] = $this->getMixedMock(
LocalFSAdapter::class,
["/"],
['write'],
['has']
);
//methods that must be stubbed should be spied on using $stub
//methods that must be proxied should be spied on using $proxy
//use $proxy for testing
}