8000 [FrameworkBundle] Add HttpClientAssertions trait documentation by welcoMattic · Pull Request #18970 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle] Add HttpClientAssertions trait documentation #18970

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

Merged
merged 1 commit into from
Oct 4, 2023

Conversation

welcoMattic
Copy link
Member

@carsonbot carsonbot added this to the 6.4 milestone Oct 2, 2023
@OskarStark OskarStark added the Waiting Code Merge Docs for features pending to be merged label Oct 2, 2023
@carsonbot carsonbot modified the milestones: 6.4, next Oct 2, 2023
fabpot added a commit to symfony/symfony that referenced this pull request Oct 2, 2023
…h provide shortcuts to assert HTTP calls was triggered (welcoMattic)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[FrameworkBundle] Add `HttpClientAssertionsTrait` which provide shortcuts to assert HTTP calls was triggered

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        | symfony/symfony-docs#18970

This PR brings a new collection (to enhance) of assertions to help users to ensure some HTTP calls has been triggered during a Request processing.

## Example

An example is better than thousands words:

HttpClientAssertionsTest.php
```php
public function testHttpClientAssertions()
{
    $client = static::createClient();
    $client->enableProfiler();

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

    $this->assertHttpClientRequest($client, 'symfony.http_client', 'https://symfony.com/');
    $this->assertHttpClientRequest($client, 'symfony.http_client', 'https://symfony.com/', 'POST', 'foo');
    $this->assertHttpClientRequest($client, 'symfony.http_client', 'https://symfony.com/', 'POST', ['foo' => 'bar']);
    $this->assertHttpClientRequest($client, 'symfony.http_client', 'https://symfony.com/', 'POST', ['foo' => 'bar']);
    $this->assertNotHttpClientRequest($client, 'symfony.http_client', 'https://laravel.com');

    $this->assertHttpClientRequestCount($client, 'symfony.http_client', 5);
}
```

FooController.php
```php
public function index(HttpClientInterface $symfonyHttpClient)
{
    $symfonyHttpClient->request('GET', '/');
    $symfonyHttpClient->request('POST', '/', ['body' => 'foo']);
    $symfonyHttpClient->request('POST', '/', ['body' => ['foo' => 'bar']]);
    $symfonyHttpClient->request('POST', '/', ['json' => ['foo' => 'bar']]);
    $symfonyHttpClient->request('GET', '/doc/current/index.html');

    return new Response();
}
```

http_client.yaml
```yaml
framework:
    http_client:
        scoped_clients:
            symfony.http_client:
                base_uri: 'https://symfony.com'
```

This is a first try to improve DX in test, to allow users to assert more complex things (here it's HTTP calls, but I think also about adding assertions on logs for example).

## Todo

- [x] Write doc

Commits
-------

59f5eec [FrameworkBundle] Add `HttpClientAssertionsTrait` which provide shortcuts to assert HTTP calls was triggered
symfony-splitter pushed a commit to symfony/framework-bundle that referenced this pull request Oct 2, 2023
…h provide shortcuts to assert HTTP calls was triggered (welcoMattic)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[FrameworkBundle] Add `HttpClientAssertionsTrait` which provide shortcuts to assert HTTP calls was triggered

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       |
| License       | MIT
| Doc PR        | symfony/symfony-docs#18970

This PR brings a new collection (to enhance) of assertions to help users to ensure some HTTP calls has been triggered during a Request processing.

## Example

An example is better than thousands words:

HttpClientAssertionsTest.php
```php
public function testHttpClientAssertions()
{
    $client = static::creat
8000
eClient();
    $client->enableProfiler();

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

    $this->assertHttpClientRequest($client, 'symfony.http_client', 'https://symfony.com/');
    $this->assertHttpClientRequest($client, 'symfony.http_client', 'https://symfony.com/', 'POST', 'foo');
    $this->assertHttpClientRequest($client, 'symfony.http_client', 'https://symfony.com/', 'POST', ['foo' => 'bar']);
    $this->assertHttpClientRequest($client, 'symfony.http_client', 'https://symfony.com/', 'POST', ['foo' => 'bar']);
    $this->assertNotHttpClientRequest($client, 'symfony.http_client', 'https://laravel.com');

    $this->assertHttpClientRequestCount($client, 'symfony.http_client', 5);
}
```

FooController.php
```php
public function index(HttpClientInterface $symfonyHttpClient)
{
    $symfonyHttpClient->request('GET', '/');
    $symfonyHttpClient->request('POST', '/', ['body' => 'foo']);
    $symfonyHttpClient->request('POST', '/', ['body' => ['foo' => 'bar']]);
    $symfonyHttpClient->request('POST', '/', ['json' => ['foo' => 'bar']]);
    $symfonyHttpClient->request('GET', '/doc/current/index.html');

    return new Response();
}
```

http_client.yaml
```yaml
framework:
    http_client:
        scoped_clients:
            symfony.http_client:
                base_uri: 'https://symfony.com'
```

This is a first try to improve DX in test, to allow users to assert more complex things (here it's HTTP calls, but I think also about adding assertions on logs for example).

## Todo

- [x] Write doc

Commits
-------

59f5eec6a4 [FrameworkBundle] Add `HttpClientAssertionsTrait` which provide shortcuts to assert HTTP calls was triggered
@OskarStark OskarStark removed the Waiting Code Merge Docs for features pending to be merged label Oct 2, 2023
@welcoMattic
Copy link
Member Author

@OskarStark I don't really understand the failure, is it related?

@OskarStark
Copy link
Contributor

Can be ignored

@javiereguiluz javiereguiluz modified the milestones: next, 6.4 Oct 4, 2023
@javiereguiluz javiereguiluz merged commit b331989 into symfony:6.4 Oct 4, 2023
@javiereguiluz
Copy link
Member

Thank you Mathieu!

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.

4 participants
0