Php 为什么Mockry返回demeter_getMyClass而不是MyClass?
我尝试模拟多个方法调用。如上所述,要测试以下方法调用:Php 为什么Mockry返回demeter_getMyClass而不是MyClass?,php,unit-testing,mocking,mockery,Php,Unit Testing,Mocking,Mockery,我尝试模拟多个方法调用。如上所述,要测试以下方法调用: $object->foo()->bar()->zebra()->alpha()->selfDestruct(); 我们可以使用以下代码: $mock = \Mockery::mock('CaptainsConsole'); $mock->shouldReceive('foo->bar->zebra->alpha->selfDestruct')->andReturn('Ten
$object->foo()->bar()->zebra()->alpha()->selfDestruct();
我们可以使用以下代码:
$mock = \Mockery::mock('CaptainsConsole');
$mock->shouldReceive('foo->bar->zebra->alpha->selfDestruct')->andReturn('Ten!');
因此,我实施了它:
public function testProcessPayment()
{
$offerPayment = m::mock('MyApp\Model\Entity\OfferPayment');
$paymentTransaction = m::mock('MyApp\Model\Entity\PaymentTransaction');
$paymentTransaction->shouldReceive('getOfferPayment->getOffer')->andReturn($offerPayment);
$transactionManager = new TransactionManager();
$transactionManager->processPayment($paymentTransaction);
$this->assertInstanceOf('Offer', $paymentTransaction->getOfferPayment()->getOffer());
}
相关类别:
class TransactionManager
{
public function processPayment(PaymentTransaction $paymentTransaction) {
$itemGroupEntity = $paymentTransaction->getOfferPayment()->getOffer();
}
}
我得到:
Mockry\u 3\u MyApp\u Model\u Entity\u PaymentTransaction::getOfferPayment()的返回值必须是MyApp\Model\Entity\OfferPayment、Mockry\u 4\u demeter\u getOfferPayment的实例返回的
getOfferPayment和getOffer的实现:
public function getOfferPayment() : OfferPayment
{
return $this->offerPayment;
}
public function getOffer() : Offer
{
return $this->offer;
}
在这种情况下,嘲弄不支持。 可以通过删除getter的返回类型声明来解决此问题:
public function getOfferPayment()
{
return $this->offerPayment;
}
public function getOffer()
{
return $this->offer;
}