How to evaluate responses for specific URLs using Guzzle?

Guzzle 6 documentation provides an easy way to mock HTTP calls so that each request returns a specific response: http://docs.guzzlephp.org/en/latest/testing.html#mock-handler

However, as stated in the documentation, it MockHandlerdetermines the response queue that will be sent for each request, regardless of the URL, in the same order.

How do I tell Guzzle to send a specific response for a given URL every time it is called?

For example, I need this call:

$client->request('GET', '/products')->getBody();

Do not fulfill the actual request, but always return:

{'products' => [{id: 1, name: 'Product 1'}, {id: 2, name: 'Product 2'}]

Doing this using the AngularJS service $httpBackendwould be simple:

$httpBackend
    .when('GET', '/products')
    .respond("{id: 1, name: 'Product 1'}, {id: 2, name: 'Product 2'}")

Any idea on how to achieve this with Guzzle 6?

+4
1

Behat Mink, , . , Twitter , webapp ( mocks () ) URL- (SUT).

HTTP Mock PHPUnit: , PHPUnit . API AngularJS:

$this->http->mock
    ->when()
        ->methodIs('GET')
        ->pathIs('/foo')
    ->then()
        ->body('mocked body')
    ->end();
$this->http->setUp();
0

Source: https://habr.com/ru/post/1610890/


All Articles