Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] Add a mock client #30553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from

Conversation

GaryPEGEOT
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR TODO

Add a new MockClient usable for test purpose:

/** @var ResponseInterface $response */
$response = $this->createMock(ResponseInterface::class);
$client = new MockClient();

// Add one or more predefined responses
$client->addResponse($response);

$client->request('GET', '/whatever', ['base_uri' => 'http://example.org']);

WDYT?

@dunglas
Copy link
Member

dunglas commented Mar 14, 2019

Maybe could we also add a helper to create mock responses? Especially, implementing getInfo() requires some care (this method must return some mandatory values), and this could probably be automated. What I've in mind is something like:

new MockResponse(/* all args are optional */
    'content',
    200,
    ['X-Foo' => 'custom header, mandatory headers will be automatically populated'],
    ['x-info' => ['custom info, mandatory keys, including raw_headers will be automatically populated']
);

@GaryPEGEOT
Copy link
Contributor Author

@dunglas just added the MockResponse, feedback welcome :)

@dunglas
Copy link
Member

dunglas commented Mar 14, 2019

It looks great!

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1st round :)

@@ -5,3 +5,4 @@ CHANGELOG
-----

* added the component
* added a mock client
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add this in the changelog, as there is no component at all in previous version, so added the component describes the changes already.

}

/**
* Clear all predefined responses.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clears

{
use ResponseTrait;

protected const DEFAULT_INFO = [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be private rather than protected IMO, following our convention of not using protected without having a clear use case for inheritance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and change the access to self::, as using static:: to access a private thing is broken.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@nicolas-grekas
Copy link
Member

A big thank you for starting this, I submitted another complete implementation in #30604.

@nicolas-grekas nicolas-grekas modified the milestones: next, 4.3 Apr 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants