1
votes

I'm creating a standalone Symfony 4.4 bundle and I need to test it !

I've created an AppKernelTest which extends Kernel and I registered all my bundles :

class ServicesBundleTestingKernel extends Kernel
{

    /**
     * @inheritDoc
     */
    public function registerBundles()
    {
        return [
            new FrameworkBundle(),
            new MonologBundle(),
            new DoctrineBundle(),
            new DoctrineMigrationsBundle(),
            new MyServicesBundle(), // My custom bundle
        ];
    }

    /**
     * @inheritDoc
     */
    public function registerContainerConfiguration(LoaderInterface $loader)
    {
    }
}


In my bundle, I have a service which require the Doctrine Entity Manager, here is my service.xml (where I declare all services for my bundle)

<service id="ServicesBundle\Service\RequestHandler" class="ServicesBundle\Service\RequestHandler" public="false" >
            <argument key="$logger" type="service" id="monolog.logger"/>
            <argument key="$em" type="service" id="doctrine.orm.entity_manager"/>
        </service>

        <service id="services_request_handler" alias="ServicesBundle\Service\RequestHandler" public="true" />

My test class :


class DependencyTest extends WebTestCase
{
    //private $container;

    public function testServiceWiring()
    {
        self::bootKernel();

    }
}


I've configured my .env.test to use my custom kernel class for the tests but when I launch the test, I got this error :

1) ServicesBundle\Tests\DependencyTest::testServiceWiring Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: The service "services_request_handler" has a dependency on a non-existent service "doctrine.orm.entity_manager".

For the tests bellow, I removed my Bundle from the registerBundle() method.

I try the command : php bin/console debug:container doctrine.orm.entity_manager and the output is : "No service found"

I also tried to see all doctrine services in my container when the app is launched and I have only two services :

  • [0] cache.adapter.doctrine

  • [1] Doctrine\Common\Annotations\Reader

I don't know why the Doctrine Bundle is not correctly registered.

1

1 Answers

0
votes

I think that the problem is associated with the fact that you have to load the minimum doctrine configuration when you boot the ServicesBundleTestingKernel.

So you have to create a file under Resources/config/test/doctrine.yaml with the minimum Doctrine configuration, something like that:

doctrine:
  dbal:
    driver:  pdo_sqlite
    memory: true
    serverVersion=5.7'
    charset: UTF8
    override_url: true

  orm:
    auto_generate_proxy_classes: true
    naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
    auto_mapping: true
    mappings:
      App:
        is_bundle: false
        type: annotation
        dir: '%kernel.project_dir%/tests/Entity'
        prefix: 'PackageName\NameBundle\Tests\Entity'
        alias: App

and later load this config in your ServicesBundleTestingKernel, registerContainerConfiguration method:

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        //load config for orm
        $confDir = $this->getProjectDir().'/src/Resources/config';
        $loader->load($confDir . '/{test}/*' . '.yaml', 'glob');

    }