Skip to content

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::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
-------

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