Skip to content

Instantly share code, notes, and snippets.

@mnapoli
Last active January 29, 2020 16:09
Show Gist options
  • Save mnapoli/676eeeda246c738ba71574be66658b5e to your computer and use it in GitHub Desktop.
Save mnapoli/676eeeda246c738ba71574be66658b5e to your computer and use it in GitHub Desktop.
Configurable Symfony container for tests

TestKernel.php:

<?php
declare(strict_types = 1);

namespace Test\Fixture;

use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Loader\LoaderInterface;

/**
 * Kernel designed specifically for tests.
 */
class TestKernel extends \AppKernel
{
    /**
     * @var \Closure
     */
    private $containerConfigurator;

    /**
     * @param \Closure $containerConfigurator Closure that takes the ContainerBuilder and configures it. Example:
     *     function (ContainerBuilder $container) {
     *         $container->setParameter('foo', 'bar');
     *     }
     */
    public function __construct(\Closure $containerConfigurator, $environment = 'test', $debug = false)
    {
        $this->containerConfigurator = $containerConfigurator;

        parent::__construct($environment, $debug);
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        parent::registerContainerConfiguration($loader);
        $loader->load($this->containerConfigurator);
    }

    /**
     * Override the parent method to force recompiling the container.
     * For performance reasons the container is also not dumped to disk.
     */
    protected function initializeContainer()
    {
        $this->container = $this->buildContainer();

        // These parameter are created by the dumper, but we don't dump the container
        // So we put fake data because they are required and Symfony is happy
        // See https://github.com/symfony/symfony/blob/4a5e43eae8c820742e40aba81fa4e6b8dbc9442a/src/Symfony/Component/DependencyInjection/Dumper/PhpDumper.php#L240-L244
        $hash = substr(sha1(uniqid('', true)), 0, 7); // original length is 7
        $id = substr(sha1(uniqid('', true)), 0, 8); // original length is 8
        $this->container->setParameter('container.build_hash', $hash);
        $this->container->setParameter('container.build_id', $id);

        $this->container->compile(true);
        $this->container->set('kernel', $this);

        $cache = new ConfigCache($this->getCacheDir().'/'.$this->getContainerClass().'.php', $this->debug);
        if ($cache->isFresh()) {
            return;
        }

        if ($this->container->has('cache_warmer')) {
            $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'));
        }
    }
}

Usage:

$containerConfigurator = function (ContainerBuilder $container) use ($bbq) : void {
    $container->setParameter('feature.use_notification_system', false);
    $container->setParameter('queue.type', 'directory');
    $container->set('queue_manager', $bbq);w
};

$kernel = new TestKernel($containerConfigurator);
$kernel->boot();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment