Functionnal test in Symfony Bundle

I created 2 projects, one is a bundle that handles the requests and contains the code, the second is just a project that includes the bundle.

Why did I created a bundle ? Because this way, I can decorate the services / views depending on the customer I work with.

I already have functional tests, but at this moment, I created them in the main project. I would like to move them into the bundle one, because the controllers are in this bundle.

As I want to handle requests, I assume I need to have a Kernel.

So I created one :

namespace MyProjectMyBundleTests;

use SymfonyBundleFrameworkBundleKernelMicroKernelTrait;
use SymfonyComponentConfigLoaderLoaderInterface;
use SymfonyComponentConfigResourceFileResource;
use SymfonyComponentDependencyInjectionContainerBuilder;
use SymfonyComponentHttpKernelKernel as BaseKernel;
use SymfonyComponentRoutingRouteCollectionBuilder;

class TestKernel extends BaseKernel
{
    use MicroKernelTrait;

    private const CONFIG_EXTS = '.{xml,yaml}';

    public function registerBundles(): iterable
    {
        $contents = require $this->getProjectDir().'/config/bundles.php';
        foreach ($contents as $class => $envs) {
            if ($envs[$this->environment] ?? $envs['all'] ?? false) {
                yield new $class();
            }
        }
    }

    public function getProjectDir(): string
    {
        return dirname(__DIR__).'/tests';
    }

    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
    {
        $container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php'));
        $container->setParameter('container.dumper.inline_class_loader', PHP_VERSION_ID < 70400 || $this->debug);
        $container->setParameter('container.dumper.inline_factories', true);
        $confDir = $this->getProjectDir().'/config';

        $loader->load($this->getProjectDir().'/../Resources/config/my_project.yaml');

        $loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
        $loader->load($confDir.'/*'.self::CONFIG_EXTS, 'glob');
    }

    protected function configureRoutes(RouteCollectionBuilder $routes): void
    {
        $confDir = $this->getProjectDir().'/../Resources/config';

        $routes->import($confDir.'/routes*.yaml', '/', 'glob');
    }
}

Where I load the config files.

In my /test folder (in the bundle), I created sort of a copy of the config/ folder (from my main project), containing the values for the tests. I’m not fully convinced, but it works right now.

I’m digging, error line by error line to go forward but … I’m not sure it’s the right solution.

Is there a simpler solution to tests a bundle ? I develop with the SF4.4 version, we plan to upgrade just after the release of the tests.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *