测试Symfony捆绑包:未找到doctrine.orm.entity

时间:2020-03-30 09:29:56

标签: php symfony doctrine-orm doctrine symfony-4.4

我正在创建一个独立的Symfony 4.4捆绑包,我需要对其进行测试!

我创建了一个扩展内核的AppKernelTest,并注册了所有捆绑包:

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)
    {
    }
}


在我的捆绑软件中,我有一个需要Doctrine Entity Manager的服务,这是我的service.xml(在其中声明捆绑软件的所有服务)

<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" />

我的测试课:


class DependencyTest extends WebTestCase
{
    //private $container;

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

    }
}


我已经将.env.test配置为使用自定义内核类进行测试,但是当我启动测试时,出现此错误:

1)ServicesBundle \ Tests \ DependencyTest :: testServiceWiring Symfony \ Component \ DependencyInjection \ Exception \ ServiceNotFoundException:服务“ services_request_handler”依赖于不存在的服务“ doctrine.orm.entity_manager”。

在下面的测试中,我从registerBundle()方法中删除了Bundle。

我尝试使用命令php bin/console debug:container doctrine.orm.entity_manager,输出为:“找不到服务”

启动应用程序时,我还尝试查看容器中的所有教义服务,而我只有两项服务:

  • [0] cache.adapter.doctrine

  • [1]教义\ Common \ Annotations \ Reader

我不知道为什么教义捆绑包未正确注册。

1 个答案:

答案 0 :(得分:0)

我认为这个问题与您在启动 ServicesBundleTestingKernel 时必须加载最小准则配置有关。

所以你必须在 Resources/config/test/doctrine.yaml 下创建一个文件,使用最小的 Doctrine 配置,类似这样:

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

然后在您的 ServicesBundleTestingKernel、registerContainerConfiguration 方法中加载此配置:

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

    }