API平台/SYMFONY和PHPUNIT-如何将服务注入Apitestcase(多个构造函数)
我有一个扩展了phpunit的apitestcase。 现在,为了产生端点-URLS,我希望能够获得一个给定实体的IRI,因此,像我一样天真,我试图 autowire iriconverter进入基础。
这引发了错误,因为自动引导并未检测到第一个参数是注射服务。还试图手动将 $ conv 参数定义为 api_platform.iri_converter services.yaml。
public function __construct(IriConverter $conv, ?string $name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->iriConverter = $conv;
}
我不知道 API平台是否具有错误的设置器注射集成,但这也不起作用。我还尝试通过在Services.YAML中声明该服务的“呼叫”属性手动调用该功能,但没有运气。
/** @required */
public function setConverter(IriConverter $conv): void
{
$this->iriConverter = $conv;
}
由于 iriconverter 在集装箱被编译时被嵌入或删除,因此这也无法正常工作。相反,建议将服务注入构造函数,但是我该怎么做?
public function setUp(): void
{
self::bootKernel();
$this->iriConverter = self::$kernel->getContainer()->get('api_platform.iri_converter');
// this right here throws a ServiceNotFound Exception - "The "kernel" service is synthetic, it needs to be set at boot time before it can be used."
// $this->iriConverter = self::$container->get('api_platform.iri_converter');
}
感谢您阅读这本书!
I have a BaseClass that extends the ApiTestCase of PHPUnit.
Now in order to generate the Endpoint-URLs, I would like to be able to get the IRI of a given Entity, so, naive as I am, I tried to autowire the IriConverter into the BaseClass.
This throws an error, because autowiring does not detect the first argument to be an injected service. Also tried to manually define the $conv argument to be the api_platform.iri_converter in the services.yaml.
public function __construct(IriConverter $conv, ?string $name = null, array $data = [], $dataName = '')
{
parent::__construct($name, $data, $dataName);
$this->iriConverter = $conv;
}
I do not know if Api Platform has a faulty Setter-Injection integration, but this is not working either. I also tried manually calling that function by declaring the "call" property of that Service in services.yaml, but no luck.
/** @required */
public function setConverter(IriConverter $conv): void
{
$this->iriConverter = $conv;
}
Due to the IriConverter being inlined or removed when the container is compiled, this is not working either. Rather it is recommended to inject the service in the constructor, but how do I do that?
public function setUp(): void
{
self::bootKernel();
$this->iriConverter = self::$kernel->getContainer()->get('api_platform.iri_converter');
// this right here throws a ServiceNotFound Exception - "The "kernel" service is synthetic, it needs to be set at boot time before it can be used."
// $this->iriConverter = self::$container->get('api_platform.iri_converter');
}
Thank you for reading this far!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不必手动启动内核,当使用
self :: getContainer()
时,它会自动完成。我在我的Symfony 5.4测试之一中有这个。You don't have to boot the kernel manually, it is done automatically when using
self::getContainer()
. I have this in one of my Symfony 5.4 tests.好吧,我找到了一个解决方案。
问题在于在设置()函数中获取Iriconverter-Service,因为此时内核仍在启动,因此没有容器可用。
我仅在一个函数中使用iriconverter,看起来像这样:
iriconverter按需从容器中获取。
该解决方案有些晦涩,因为依赖对实例化或设定器的注射通常被称为正确的方法,但这也许只是我的感知。也许使用Phpunit测试环境的原因是?
无论如何,标记为解决
Alright I found a solution.
The issue lies with fetching the IriConverter-Service inside the setUp()-function, because at this point the kernel is still booting, so no container is available.
I use the IriConverter only in one function and it looks like this:
The IriConverter is fetched from the container on demand.
This solution is somewhat obscure, because Dependency Injection on instantiation or Setter-Injection is usually hailed as the correct way to do it, but maybe that is just my perception. Maybe the reason lies with using the PHPUnit Test environment?
Anyways, marked as solved