Omnipay PHPUnit Guzzle httpClient 404 Error语言 - mock json



我是OmniPay的新手,正在尝试使用它并尝试制作一个简单的自定义网关,并使用模拟json http响应创建一个单元测试。

在网关测试中.php我设置了一个模拟的http响应:

public function testPurchaseSuccess()
{
    $this->setMockHttpResponse('TransactionSuccess.txt');
    $response = $this->gateway->purchase($this->options)->send();
    echo $response->isSuccessful();
    $this->assertEquals(true, $response->isSuccessful());
}

在购买请求中.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 响应呢?

---更新---

事实证明,在我的购买响应中.php

use OmnipayCommonMessageRequestInterface;
//and...
public function __construct(RequestInterface $request, $data)
{
    parent::__construct($request, $data);
}

不见了。

现在有了 PurchaseRequest 中的$httpResponse = $this->httpClient->post(null)->send();.php断言是可以的,但是当我使用 httpClient 时,Guzzle 会抛出 404 错误。我检查了 Guzzle 的文档并尝试创建一个模拟响应,但随后我的断言再次失败,仍然存在 404:

购买请求.php

public function sendData($data)
{
    $plugin = new GuzzlePluginMockMockPlugin();
    $plugin->addResponse(new GuzzleHttpMessageResponse(200));
    $this->httpClient->addSubscriber($plugin);
    $httpResponse = $this->httpClient->post(null)->send(); 
    return $this->response = new PurchaseResponse($this, $httpResponse->json());
}

有什么建议,如何摆脱404?

好的

,所以这是结果是解决方案:

原始问题

这在我的PucrhaseResponse中缺失.php:

use OmnipayCommonMessageRequestInterface;
//and...
public function __construct(RequestInterface $request, $data)
{
    parent::__construct($request, $data);
}

购买请求.php:

public function sendData($data)
{
    $httpResponse = $this->httpClient->post(null)->send();
    return $this->response = new PurchaseResponse($this, $httpResponse->json());
}

解决更新中的 404 问题

为了防止Guzzle抛出异常,我不得不为request.error添加一个侦听器。

购买请求.php:

public function sendData($data)
{
    $this->httpClient->getEventDispatcher()->addListener(
        'request.error',
        function (GuzzleCommonEvent $event) {
            $event->stopPropagation();
        }
    );
    $httpResponse = $this->httpClient->post(null)->send(); 
    return $this->response = new PurchaseResponse($this, $httpResponse->json());
}

最新更新