为什么Mockery返回demeter_getMyClass而不是MyClass?

时间:2017-02-12 15:31:31

标签: php unit-testing mocking mockery

我尝试模拟多个方法调用。如上所述in the documentation,要测试以下方法调用:

$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();
    }
}

我明白了:

Return value of Mockery_3_MyApp_Model_Entity_PaymentTransaction::getOfferPayment() must be an instance of MyApp\Model\Entity\OfferPayment, instance of Mockery_4__demeter_getOfferPayment returned

实施getOfferPayment和getOffer:

public function getOfferPayment() : OfferPayment
{
    return $this->offerPayment;
}


public function getOffer() : Offer
{
    return $this->offer;
}

1 个答案:

答案 0 :(得分:0)

在这种情况下,Mockery不支持return type declarations。 可以通过删除getter的返回类型声明来修复此问题:

public function getOfferPayment()
{
    return $this->offerPayment;
}


public function getOffer()
{
    return $this->offer;
}