【问题标题】:Testing Symfony Bundle : doctrine.orm.entity not found测试 Symfony 包:找不到教义.orm.entity
【发布时间】:2020-07-10 15:32:58
【问题描述】:

我正在创建一个独立的 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 没有正确注册。

【问题讨论】:

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


    【解决方案1】:

    我认为问题与您在启动 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');
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-06
      • 1970-01-01
      • 1970-01-01
      • 2013-08-05
      • 2023-04-05
      • 2012-01-17
      • 2012-09-10
      相关资源
      最近更新 更多