定义 Symfony 服务定位器
Defining a Symfony Service Locator
我想测试我的私人服务。我看到有人建议用 Locator 来获取它。
我正在尝试按照文档所述进行操作:https://symfony.com/doc/current/service_container/service_subscribers_locators.html#defining-a-service-locator
我的 services.yaml:
App\Tests\ServiceLocators\AuthServiceLocator:
arguments: !service_locator
App\Service\AuthManager: '@App\Service\AuthManager'
我得到:
Symfony\Component\Config\Exception\LoaderLoadException : "!service_locator" tag only accepts maps of "@service" references in "/var/www/api/src/../config/services.yaml" in /var/www/api/src/../config/services.yaml (which is being imported from "/var/www/api/src/Kernel.php").
/var/www/api/vendor/symfony/config/Loader/FileLoader.php:173
Symfony 文档是否包含无效的 yaml 或发生了什么?
我尝试解决的问题:
如果我这样定义标签,我不会收到错误:
App\Tests\ServiceLocators\AuthServiceLocator:
tags: ['container.service_locator']
arguments:
App\Service\AuthManager: '@App\Service\AuthManager'
但是当我 运行 我的测试时,我得到:
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException : Invalid definition for service "App\Tests\ServiceLocators\AuthServiceLocator": an array of references is expected as first argument when the "container.service_locator" tag is set.
我的定位器有这个构造函数:
class AuthServiceLocator
{
public function __construct(public array $handler, public AuthManager $authManager)
{
}
我尝试了不同的组合(仅数组,仅 AuthManager),但它不起作用
如果您想在扩展 WebTestCase 或 KernelTestCase 的测试中测试您的私人服务,您已经可以从专门准备的私人服务中获得您的私人服务。
class MyTestCase extends KernelTestCase
{
public function testMyService(): void
{
static::bootKernel();
$myService = static::$container->get(MyService::class);
// Symfony 5.3+
$myService = static::getContainer()->get(MyService::class);
...
}
}
注意,我们不调用 $kernel->getContainer()
而是直接从测试中获取容器。在内部这将调用以下代码:
static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
test.service_container
基本上是您尝试手动构建的定位器。您也可以将配置用作参考。参见:https://github.com/symfony/symfony/blob/5.x/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.php#L46-L56
我想测试我的私人服务。我看到有人建议用 Locator 来获取它。 我正在尝试按照文档所述进行操作:https://symfony.com/doc/current/service_container/service_subscribers_locators.html#defining-a-service-locator 我的 services.yaml:
App\Tests\ServiceLocators\AuthServiceLocator:
arguments: !service_locator
App\Service\AuthManager: '@App\Service\AuthManager'
我得到:
Symfony\Component\Config\Exception\LoaderLoadException : "!service_locator" tag only accepts maps of "@service" references in "/var/www/api/src/../config/services.yaml" in /var/www/api/src/../config/services.yaml (which is being imported from "/var/www/api/src/Kernel.php").
/var/www/api/vendor/symfony/config/Loader/FileLoader.php:173
Symfony 文档是否包含无效的 yaml 或发生了什么?
我尝试解决的问题: 如果我这样定义标签,我不会收到错误:
App\Tests\ServiceLocators\AuthServiceLocator:
tags: ['container.service_locator']
arguments:
App\Service\AuthManager: '@App\Service\AuthManager'
但是当我 运行 我的测试时,我得到:
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException : Invalid definition for service "App\Tests\ServiceLocators\AuthServiceLocator": an array of references is expected as first argument when the "container.service_locator" tag is set.
我的定位器有这个构造函数:
class AuthServiceLocator
{
public function __construct(public array $handler, public AuthManager $authManager)
{
}
我尝试了不同的组合(仅数组,仅 AuthManager),但它不起作用
如果您想在扩展 WebTestCase 或 KernelTestCase 的测试中测试您的私人服务,您已经可以从专门准备的私人服务中获得您的私人服务。
class MyTestCase extends KernelTestCase
{
public function testMyService(): void
{
static::bootKernel();
$myService = static::$container->get(MyService::class);
// Symfony 5.3+
$myService = static::getContainer()->get(MyService::class);
...
}
}
注意,我们不调用 $kernel->getContainer()
而是直接从测试中获取容器。在内部这将调用以下代码:
static::$container = $container->has('test.service_container') ? $container->get('test.service_container') : $container;
test.service_container
基本上是您尝试手动构建的定位器。您也可以将配置用作参考。参见:https://github.com/symfony/symfony/blob/5.x/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.php#L46-L56