Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么Mockry返回demeter_getMyClass而不是MyClass?_Php_Unit Testing_Mocking_Mockery - Fatal编程技术网

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;
}