fe4d92806b
This PR was squashed before being merged into the 5.2-dev branch.
Discussion
----------
[HttpClient][DI] Add an option to use the MockClient in functional tests
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| Deprecations? | no
| Tickets |
| License | MIT
| Doc PR | TODO
Let's say you want to mock HTTP responses in your test or dev environment:
First, you create an invokable class responsible of generating the response:
```php
namespace App\Tests;
use Symfony\Contracts\HttpClient\ResponseInterface;
class MyAwesomeCallback
{
public function __invoke(string $method, string $url, array $options = []): ResponseInterface
{
// load a fixture file or generate dynamic data
}
}
```
Then configure it:
```yaml
# config/services_test.yaml
services:
# ...
App\Tests\MyAwesomeCallback: ~
# config/packages/test/framework.yaml
framework:
http_client:
mock_response_factory: App\Tests\MyAwesomeCallback
```
The HttpClient will now be using MockHttpClient in your functional test:
```php
$client = static::createClient();
// No live HTTP connection made
$client->request('GET', '/path-with-http-call');
```
Commits
-------
|
||
---|---|---|
.. | ||
CacheWarmer | ||
Command | ||
Console | ||
Controller | ||
DataCollector | ||
DependencyInjection | ||
EventListener | ||
HttpCache | ||
Kernel | ||
Resources | ||
Routing | ||
Secrets | ||
Test | ||
Tests | ||
Translation | ||
.gitattributes | ||
.gitignore | ||
CHANGELOG.md | ||
composer.json | ||
FrameworkBundle.php | ||
KernelBrowser.php | ||
LICENSE | ||
phpunit.xml.dist | ||
README.md |