0

I created a Symfony bundle that allows me to manage users/permissions/profiles across multiple applications. This bundle provides a set of routes, forms, services, etc.

I already have unit tests that validate the application's functionality, but I would like to add functional tests to verify the overall behavior of the application by directly testing my routes.

How can I do this? Knowing that I don't have a Kernel, it doesn't seem possible to directly test the bundle. In the /tests folder, I created a /tests/TestApplication folder that contains a blank project. I'm not sure if I'm on the right track.

Should functional tests for a bundle be avoided? Do you know of any good technical documentation on this subject? I have already followed this tutorial, but unfortunately, it doesn't go far enough on the subject: https://symfonycasts.com/screencast/symfony-bundle.

Have a good day !

3
  • 1
    "I don't have a Kernel" - why not create one? For example, have a look at how the tests in the EasyAdminBundle work at github.com/EasyCorp/EasyAdminBundle/tree/4.x/tests
    – Nico Haase
    Commented Jun 28 at 13:15
  • I don't really understand how can you NOT have a kernel. If you've got a working application with routing, etc, there must be a kernel that manages it all.
    – ozahorulia
    Commented Jun 28 at 22:32
  • @ozahorulia if you try to craft a bundle, there's no need to have a kernel within that package
    – Nico Haase
    Commented Jun 29 at 18:05

1 Answer 1

1

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

1
  • Thanks for this, I'll give it a try ! :)
    – Alexandre
    Commented Jul 9 at 12:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.