我是OmniPay的新手,玩弄它并试图创建一个简单的自定义网关,并使用模拟json http响应创建一个单元测试。
在GatewayTest.php中,我设置了一个模拟的http响应:
public function testPurchaseSuccess()
{
$this->setMockHttpResponse('TransactionSuccess.txt');
$response = $this->gateway->purchase($this->options)->send();
echo $response->isSuccessful();
$this->assertEquals(true, $response->isSuccessful());
}
在PurchaseRequest.php中,我试图以某种方式得到它:
public function sendData($data)
{
$httpResponse = //how do I get the mock http response set before?
return $this->response = new PurchaseResponse($this, $httpResponse->json());
}
那么如何在PurchaseRequest.php中获取模拟http响应?
---更新---
原来我的PurchaseResponse.php
use Omnipay\Common\Message\RequestInterface;
//and...
public function __construct(RequestInterface $request, $data)
{
parent::__construct($request, $data);
}
失踪了。
现在在PurchaseRequest.php中使用$httpResponse = $this->httpClient->post(null)->send();
断言是正常的,但是当我使用httpClient时,Guzzle会抛出404错误。我检查Guzzle's docs并尝试创建模拟响应,但是我的断言再次失败并且404仍然存在:
PurchaseRequest.php
public function sendData($data)
{
$plugin = new Guzzle\Plugin\Mock\MockPlugin();
$plugin->addResponse(new Guzzle\Http\Message\Response(200));
$this->httpClient->addSubscriber($plugin);
$httpResponse = $this->httpClient->post(null)->send();
return $this->response = new PurchaseResponse($this, $httpResponse->json());
}
有什么建议,如何摆脱404?
答案 0 :(得分:0)
好的,所以这就是解决方案:
原始问题
我的PucrhaseResponse.php遗漏了这个:
use Omnipay\Common\Message\RequestInterface;
//and...
public function __construct(RequestInterface $request, $data)
{
parent::__construct($request, $data);
}
PurchaseRequest.php:
public function sendData($data)
{
$httpResponse = $this->httpClient->post(null)->send();
return $this->response = new PurchaseResponse($this, $httpResponse->json());
}
解决更新中的404问题
为了防止Guzzle抛出异常,我必须为request.error添加一个监听器。
PurchaseRequest.php:
public function sendData($data)
{
$this->httpClient->getEventDispatcher()->addListener(
'request.error',
function (\Guzzle\Common\Event $event) {
$event->stopPropagation();
}
);
$httpResponse = $this->httpClient->post(null)->send();
return $this->response = new PurchaseResponse($this, $httpResponse->json());
}