When I create integration or functional tests for a bundle to test how it works with other components, I create a test application:
At first, you need to install the requried components via Composer as dev-requirements.
Next, you can create a test kernel:
<?php
declare(strict_types=1);
namespace Tests\VarLabIT\LexofficeBundle\Integration\TestApp;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
use VarLabIT\LexofficeBundle\VarLabITLexofficeBundle;
class TestAppKernel extends Kernel
{
public function registerBundles(): iterable
{
$bundles = [];
if ('test' === $this->getEnvironment()) {
$bundles[] = new FrameworkBundle();
$bundles[] = new VarLabITLexofficeBundle();
}
return $bundles;
}
public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/config/config_test.yml');
}
public function getCacheDir(): string
{
return \sys_get_temp_dir() . '/VarLabITLexofficeBundle/cache';
}
public function getLogDir(): string
{
return \sys_get_temp_dir() . '/VarLabITLexofficeBundle/logs';
}
}
(from https://github.com/var-lab-it/lexoffice-bundle/blob/main/tests/Integration/TestApp/TestAppKernel.php)
And then, you can use the TestKernel in your test case:
<?php
declare(strict_types=1);
namespace Tests\VarLabIT\LexofficeBundle\Integration\Tests;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Tests\VarLabIT\LexofficeBundle\Integration\TestApp\TestAppKernel;
use VarLabIT\LexofficeBundle\LexofficeClient;
class ConfigurationTest extends KernelTestCase
{
protected ContainerInterface $container;
protected function setUp(): void
{
parent::setUp();
$kernel = new TestAppKernel('test', true);
$kernel->boot();
$this->container = $kernel->getContainer();
}
public function testConfiguration(): void
{
$apiKey = $this->container->getParameter('var_lab_it_lexoffice.api_key');
$apiEndpoint = $this->container->getParameter('var_lab_it_lexoffice.api_endpoint');
$apiVersion = $this->container->getParameter('var_lab_it_lexoffice.api_version');
self::assertEquals('test-key', $apiKey);
self::assertEquals('https://api.lexoffice.io', $apiEndpoint);
self::assertEquals('v1', $apiVersion);
}
public function testIfClientIsRegistered(): void
{
$client = $this->container->get(LexofficeClient::class);
self::assertInstanceOf(LexofficeClient::class, $client);
}
}
The whole code can be found here: https://github.com/var-lab-it/lexoffice-bundle/tree/main/tests/Integration