我正在创建一个独立的 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。