我正在使用PHPSpec测试一个类,直到我想为具有静态函数的类创建一个模拟器。
我正在测试的课程:
<?php
namespace App\Service;
class PaymentService
{
public function paymentVerification($orderId, array $data)
{
...
// Get the payment details
$payment = \PayPal\Api\Payment::get($data['payKey'], $apiContext);
...
}
}
PHPSpec类:
<?php
namespace App\Spec\Service;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class PaymentServiceSpec extends ObjectBehavior
{
/**
* @param \PayPal\API\Payment $payment
*/
function it_should_return_false_when_the_payment_verification_failed($payment)
{
...
// This throws a PHP Fatal error: Call to undefined method PhpSpec\Wrapper\Collaborator::get() in
$payment::get(Argument::exact($data['payKey']), Argument::exact($apiContext))->shouldReturn(array('foobar'));
...
$this->paymentVerification($orderId, $data)->shouldReturn(false);
}
}
我如何模拟\ PayPal \ Api \ Payment :: get($ data ['payKey'],$ apiContext); ? 目前这会引发PHP致命错误:调用未定义的方法PhpSpec \ Wrapper \ Collaborator :: get()
如何正确完成?提前谢谢!
答案 0 :(得分:0)
你应该避免这样做。创建一个薄层,它将包装paypal库。嘲笑你自己的东西。
请参阅https://github.com/phpspec/prophecy/pull/20#issuecomment-18133965