我正在使用Braintree PHP Client,它非常依赖静态方法。我在这个项目中的所有端点都被集成测试所覆盖,例如:
Storage::shouldReceive('put')->once()->andReturn(true);
$this->post('/api/payment');
如您所见,我还使用Mockery来创建模拟。但是,由于Braintree库严重依赖静态方法,因此我无法创建方法,因此无法测试这些端点。
这是使用Braintree PHP客户端编写的代码示例:
$result = Braintree\Transaction::sale([
'amount' => '1000.00',
'paymentMethodNonce' => 'nonceFromTheClient',
'options' => [ 'submitForSettlement' => true ]
]);
我在这里有什么选择?
此答案仅在mockery 1.*
安装后才有效。早期版本不会进行静态方法模拟。以下代码有效:
$brainTreeMock = Mockery::mock('alias:Braintree_Transaction');
$transaction = (object)[ 'id' => str_random(5) ];
$brainTreeMock->shouldReceive('sale')->andReturn((object)[
'success' => true,
'transaction' => $transaction
]
);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句