Omnipay PHPUnit Guzzle httpClient Ошибка 404 — имитация json

Я новичок в 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());
}

Итак, как мне получить фиктивный http-ответ в PurchaseRequest.php?

--- ОБНОВЛЕНИЕ ---

Оказалось, что в моем PurchaseResponse.php

use Omnipay\Common\Message\RequestInterface;

//and...

public function __construct(RequestInterface $request, $data)
{
    parent::__construct($request, $data);
}

скучал.

Теперь с $httpResponse = $this->httpClient->post(null)->send(); в PurchaseRequest.php утверждения в порядке, но когда я использую httpClient, Guzzle выдает ошибку 404. Я проверил документы Guzzle и попытался создать фиктивный ответ. , но опять мои утверждения терпят неудачу и остается 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?


person orszaczky    schedule 24.01.2015    source источник


Ответы (1)


Итак, вот что оказалось решением:

Исходная проблема

Это отсутствовало в моем PucrhaseResponse.php:

use Omnipay\Common\Message\RequestInterface;

//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 (\Guzzle\Common\Event $event) {
            $event->stopPropagation();
        }
    );

    $httpResponse = $this->httpClient->post(null)->send(); 

    return $this->response = new PurchaseResponse($this, $httpResponse->json());
}
person orszaczky    schedule 25.01.2015