Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[HttpClient] Allow pass array of callable to the mocking http client#34871

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nicolas-grekas merged 1 commit intosymfony:masterfromKoc:mocking-http-client
Feb 2, 2020

Conversation

Koc
Copy link
Contributor

@KocKoc commentedDec 7, 2019
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
Deprecations?no
Tickets-
LicenseMIT
Doc PRnot yet

For the now MockHttpClient allows pass closure as response factory. It useful for tests to perform assertions that expected request was sent. But If we are sending multiple sequental requests then it became a little bit tricky to perform assertions:

<?php$requestIndex =0;$expectedRequest =function ($method,$url,$options)use (&$requestIndex) {switch (++$requestIndex) {case1:$this->assertSame('GET',$method);$this->assertSame('https://example.com/api/v1/customer',$url);returnnewMockResponse(CustomerFixture::CUSTOMER_RESPONSE);case2:$this->assertSame('POST',$method);$this->assertSame('https://example.com/api/v1/customer/1/products',$url);$this->assertJsonStringEqualsJsonFile(CustomerFixture::CUSTOMER_PRODUCT_PAYLOAD,$options['json']);returnnewMockResponse();default:thrownew \InvalidArgumentException('Too much requests');    }};$client =newMockHttpClient($expectedRequest);static::$container->set('http_client.example',$client);$commandTester->execute(['--since' =>'2019-01-01 00:05:00','--until' =>'2019-01-01 00:35:00']);$this->assertSame(2,$requestIndex,'All expected requests was sent.');

This PR introduces possibility to define multiple callable response factories andgetSentRequestsCount method to make sure that each factory was called:

<?php$expectedRequests = [function ($method,$url,$options) {$this->assertSame('GET',$method);$this->assertSame('https://example.com/api/v1/customer',$url);returnnewMockResponse(CustomerFixture::CUSTOMER_RESPONSE);    },function ($method,$url,$options) {$this->assertSame('POST',$method);$this->assertSame('https://example.com/api/v1/customer/1/products',$url);$this->assertJsonStringEqualsJsonFile(CustomerFixture::CUSTOMER_PRODUCT_PAYLOAD,$options['json']);returnnewMockResponse();    },];$client =newMockHttpClient($expectedRequest);static::$container->set('http_client.example',$client);$commandTester->execute(['--since' =>'2019-01-01 00:05:00','--until' =>'2019-01-01 00:35:00']);$this->assertSame(2,$client->getSentRequestsCount(),'All expected requests was sent.');

Also it adds a lot of tests.

OskarStark reacted with heart emoji
@KocKocforce-pushed themocking-http-client branch 4 times, most recently from1ba760f to8accca2CompareDecember 7, 2019 16:12
@nicolas-grekasnicolas-grekas added this to thenext milestoneDec 7, 2019
@nicolas-grekas
Copy link
Member

friendly ping@Koc

@KocKocforce-pushed themocking-http-client branch 2 times, most recently fromd5bb399 toda57266CompareJanuary 14, 2020 23:28
@Koc
Copy link
ContributorAuthor

Koc commentedJan 14, 2020

@nicolas-grekas I've updated the PR.

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

(with minor comments)

@Koc
Copy link
ContributorAuthor

Koc commentedJan 30, 2020

@nicolas-grekas done

@KocKoc closed thisFeb 1, 2020
@KocKoc reopened thisFeb 1, 2020
@Koc
Copy link
ContributorAuthor

Koc commentedFeb 1, 2020

Try to restart build

@Koc
Copy link
ContributorAuthor

Koc commentedFeb 2, 2020

@nicolas-grekas all green

@nicolas-grekas
Copy link
Member

Thank you@Koc.

nicolas-grekas added a commit that referenced this pull requestFeb 2, 2020
…ng http client (Koc)This PR was merged into the 5.1-dev branch.Discussion----------[HttpClient] Allow pass array of callable to the mocking http client| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| Deprecations? | no| Tickets       | -| License       | MIT| Doc PR        | not yetFor the now MockHttpClient allows pass closure as response factory. It useful for tests to perform assertions that expected request was sent. But If we are sending multiple sequental requests then it became a little bit tricky to perform assertions:```php<?php$requestIndex = 0;$expectedRequest = function ($method, $url, $options) use (&$requestIndex) {    switch (++$requestIndex) {        case 1:            $this->assertSame('GET', $method);            $this->assertSame('https://example.com/api/v1/customer', $url);            return new MockResponse(CustomerFixture::CUSTOMER_RESPONSE);        case 2:            $this->assertSame('POST', $method);            $this->assertSame('https://example.com/api/v1/customer/1/products', $url);            $this->assertJsonStringEqualsJsonFile(CustomerFixture::CUSTOMER_PRODUCT_PAYLOAD, $options['json']);            return new MockResponse();        default:            throw new \InvalidArgumentException('Too much requests');    }};$client = new MockHttpClient($expectedRequest);static::$container->set('http_client.example', $client);$commandTester->execute(['--since' => '2019-01-01 00:05:00', '--until' => '2019-01-01 00:35:00']);$this->assertSame(2, $requestIndex, 'All expected requests was sent.');```This PR introduces possibility to define multiple callable response factories and `getSentRequestsCount` method to make sure that each factory was called:```php<?php$expectedRequests = [    function ($method, $url, $options) {        $this->assertSame('GET', $method);        $this->assertSame('https://example.com/api/v1/customer', $url);        return new MockResponse(CustomerFixture::CUSTOMER_RESPONSE);    },    function ($method, $url, $options) {        $this->assertSame('POST', $method);        $this->assertSame('https://example.com/api/v1/customer/1/products', $url);        $this->assertJsonStringEqualsJsonFile(CustomerFixture::CUSTOMER_PRODUCT_PAYLOAD, $options['json']);        return new MockResponse();    },];$client = new MockHttpClient($expectedRequest);static::$container->set('http_client.example', $client);$commandTester->execute(['--since' => '2019-01-01 00:05:00', '--until' => '2019-01-01 00:35:00']);$this->assertSame(2, $client->getSentRequestsCount(), 'All expected requests was sent.');```Also it adds a lot of tests.Commits-------a36797d Allow pass array of callable to the mocking http client
@nicolas-grekasnicolas-grekas merged commita36797d intosymfony:masterFeb 2, 2020
@nicolas-grekasnicolas-grekas removed this from thenext milestoneMay 4, 2020
@nicolas-grekasnicolas-grekas added this to the5.1 milestoneMay 4, 2020
@fabpotfabpot mentioned this pull requestMay 5, 2020
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
5.1
Development

Successfully merging this pull request may close these issues.

3 participants
@Koc@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp