1

我正在创建一个独立的 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] Doctrine\Common\Annotations\Reader

我不知道为什么没有正确注册 Doctrine Bundle。

4

1 回答 1

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');

    }
于 2021-03-31T06:48:31.470 回答