This bundle integrates seamless with Symfony Http Client Fixtures from kununu/data-fixtures.
In the rest of the documentation we will assume that you are using the Symfony Http Client and Symfony Http Foundation and have configured an Http client service called app.my.http_client.
In your tests you can extend the classes FixturesAwareTestCase or WebTestCase which expose the following method:
protected function loadHttpClientFixtures(string $httpClientServiceId, OptionsInterface $options, string ...$classNames): void
$httpClientServiceId
- Name of your Symfony Http Client service$options
- Options for the fixtures load process$classNames
- Classes names of fixtures to load
Example of loading fixtures in an Integration Test
use Kununu\TestingBundle\Test\FixturesAwareTestCase;
use Kununu\TestingBundle\Test\Options\Options;
final class IntegrationTest extends FixturesAwareTestCase
{
public function testIntegration(): void
{
// Load mock responses of a Symfony Http Client from a Fixture class
$this->loadHttpClientFixtures(
'app.my.http_client',
Options::create(),
YourHttpClientFixtureClass::class
);
}
}
Since this bundle is using the kununu/data-fixtures package, it also has support for initializable features, allowing you to inject arguments into your feature classes (see documentation of the kununu/data-fixtures package).
In order to do that, your Fixtures classes must implement the InitializableFixtureInterface, and before loading the fixtures you will need to initialize the arguments.
use Kununu\TestingBundle\Test\FixturesAwareTestCase;
use Kununu\TestingBundle\Test\Options\Options;
final class IntegrationTest extends FixturesAwareTestCase
{
public function testIntegration(): void
{
$this->registerInitializableFixtureForHttpClient(
'app.my.http_client',
YourHttpClientFixtureClass::class,
$yourArg1,
//...,
$yourArgN
);
$this->loadHttpClientFixtures(
'app.my.http_client',
Options::create(),
YourHttpClientFixtureClass::class
);
}
}
Bellow you can find all configuration options for symfony http client fixtures and their defaults.
kununu_testing:
http_client:
clients:
- 'app.my.http_client'
Also make sure that in your test environment configuration you redefine your Http client instances to use the special Http Mock client provided by kununu/data-fixtures, otherwise the fixtures won't be loaded:
services:
app.my.http_client:
class: Kununu\DataFixtures\Tools\HttpClient
public: true
Also be mindful that if you inject the same client on several services you might have unwanted results.
Example: you are testing service A which uses component B. Component B is also injected with the same Http client but is calling totally different endpoints.
To solve those cases create dedicated Http clients services for the service you are testing.
- Try to create an alias of the Symfony interface to your client in your test environment configuration:
services:
http_client:
class: Kununu\DataFixtures\Tools\HttpClient
public: true
Symfony\Contracts\HttpClient\HttpClientInterface: '@http_client'
- If the mock Http client is still not loaded make sure to add to your test environment
framework.yaml
file:
framework:
http_client:
enabled: false
To disable Symfony Framework bundle creating its own Http clients.